|
|
| version 1.1.1.2, 2013/07/22 08:44:29 | version 1.1.1.3, 2016/11/01 09:56:12 |
|---|---|
| Line 19 | Line 19 |
| #include <sys/sysctl.h> | #include <sys/sysctl.h> |
| #include <net/route.h> | #include <net/route.h> |
| #include <netinet/if_ether.h> | #include <netinet/if_ether.h> |
| #include <net/ethernet.h> | |
| #include <osreldate.h> | |
| /* | /* |
| * DEFINITIONS | * DEFINITIONS |
| Line 389 ReadFile(const char *filename, const char *target, | Line 391 ReadFile(const char *filename, const char *target, |
| /* Open file */ | /* Open file */ |
| if ((fp = OpenConfFile(filename, &cf)) == NULL) | if ((fp = OpenConfFile(filename, &cf)) == NULL) |
| return(-1); | return(-2); |
| /* Find label */ | /* Find label */ |
| Line 851 GenerateMagic(void) | Line 853 GenerateMagic(void) |
| int | int |
| PIDCheck(const char *filename, int killem) | PIDCheck(const char *filename, int killem) |
| { | { |
| int fd = -1, n_tries; | int n_tries; |
| struct pidfh *pfh = NULL; | |
| /* Sanity */ | /* Sanity */ |
| Line 859 PIDCheck(const char *filename, int killem) | Line 862 PIDCheck(const char *filename, int killem) |
| /* Atomically open and lock file */ | /* Atomically open and lock file */ |
| for (n_tries = 0; | for (n_tries = 0; n_tries < MAX_LOCK_ATTEMPTS; n_tries++) |
| n_tries < MAX_LOCK_ATTEMPTS | |
| && (fd = open(filename, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0644)) < 0; | |
| n_tries++) | |
| { | { |
| int nscan, old_pid; | pid_t old_pid; |
| FILE *fp; | |
| /* Abort on any unexpected errors */ | pfh = pidfile_open(filename, 0644, &old_pid); |
| if (pfh == NULL) { | |
| if (errno != EAGAIN) | if (errno == EEXIST) { |
| { | if (!killem) { |
| Perror("%s: open(%s)", __FUNCTION__, filename); | Log(LG_ERR, ("already running as process %d", old_pid)); |
| return(-1); | return(-1); |
| } | |
| if (kill(old_pid, SIGTERM) < 0) | |
| switch (errno) { | |
| case ESRCH: | |
| Log(LG_ERR, ("process %d no longer exists", old_pid)); | |
| break; | |
| default: | |
| Perror("%s: kill(%d)", __FUNCTION__, old_pid); | |
| return(-1); | |
| } | |
| /* Wait and try again */ | |
| Log(LG_ERR, ("waiting for process %d to die...", old_pid)); | |
| sleep(1); | |
| } else { | |
| Perror("cannot open pid file"); | |
| return(-1); | |
| } | |
| } else { | |
| pidfile_write(pfh); | |
| break; | |
| } | } |
| /* We're already running ... see who it is */ | |
| if ((fp = fopen(filename, "r")) == NULL) | |
| { | |
| Perror("%s: fopen(%s)", __FUNCTION__, filename); | |
| return(-1); | |
| } | |
| /* If there's a PID in there, sniff it out */ | |
| nscan = fscanf(fp, "%d", &old_pid); | |
| fclose(fp); | |
| if (nscan != 1) | |
| { | |
| Log(LG_ERR, ("%s: contents mangled", filename)); | |
| return(-1); | |
| } | |
| /* Maybe kill the other guy */ | |
| if (!killem) | |
| { | |
| Log(LG_ERR, ("already running as process %d", old_pid)); | |
| return(-1); | |
| } | |
| if (kill(old_pid, SIGTERM) < 0) | |
| switch (errno) | |
| { | |
| case ESRCH: | |
| Log(LG_ERR, ("process %d no longer exists", old_pid)); | |
| break; | |
| default: | |
| Perror("%s: kill(%d)", __FUNCTION__, old_pid); | |
| return(-1); | |
| } | |
| /* Wait and try again */ | |
| Log(LG_ERR, ("waiting for process %d to die...", old_pid)); | |
| sleep(1); | |
| } | } |
| if (n_tries == MAX_LOCK_ATTEMPTS) | if (n_tries == MAX_LOCK_ATTEMPTS) |
| { | { |
| Log(LG_ERR, ("can't lock %s after %d attempts", filename, n_tries)); | Log(LG_ERR, ("can't lock %s after %d attempts", filename, n_tries)); |
| return(-1); | return(-1); |
| } | } |
| /* Close on exec */ | |
| (void) fcntl(fd, F_SETFD, 1); | |
| /* Create a stream on top of file descriptor */ | |
| if ((lockFp = fdopen(fd, "r+")) == NULL) | |
| { | |
| Perror("%s: fdopen", __FUNCTION__); | |
| return(-1); | |
| } | |
| setbuf(lockFp, NULL); | |
| /* Write my PID in there */ | |
| rewind(lockFp); | |
| fprintf(lockFp, "%u\n", (u_int) gPid); | |
| fflush(lockFp); | |
| (void) ftruncate(fileno(lockFp), ftell(lockFp)); | |
| return(0); | return(0); |
| } | } |
| Line 1019 TcpGetListenPort(struct u_addr *addr, in_port_t port, | Line 977 TcpGetListenPort(struct u_addr *addr, in_port_t port, |
| /* Make socket available for connections */ | /* Make socket available for connections */ |
| if (listen(sock, 2) < 0) | if (listen(sock, -1) < 0) |
| { | { |
| Perror("%s: listen", __FUNCTION__); | Perror("%s: listen", __FUNCTION__); |
| (void) close(sock); | (void) close(sock); |
| Line 1531 ppp_util_ascify(char *buf, size_t bsiz, const char *da | Line 1489 ppp_util_ascify(char *buf, size_t bsiz, const char *da |
| *bp = '\0'; | *bp = '\0'; |
| } | } |
| #ifndef HAVE_NTOA_R | |
| /* | |
| * Convert a binary representation of an ethernet address to an ASCII string. | |
| */ | |
| char * | |
| ether_ntoa_r(const struct ether_addr *n, char *a) | |
| { | |
| int i; | |
| i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", n->octet[0], | |
| n->octet[1], n->octet[2], n->octet[3], n->octet[4], n->octet[5]); | |
| if (i < 17) | |
| return (NULL); | |
| return (a); | |
| } | |
| #endif |