--- embedaddon/mpd/src/util.c 2013/07/22 08:44:29 1.1.1.2 +++ embedaddon/mpd/src/util.c 2016/11/01 09:56:12 1.1.1.3 @@ -19,6 +19,8 @@ #include #include #include +#include +#include /* * DEFINITIONS @@ -389,7 +391,7 @@ ReadFile(const char *filename, const char *target, /* Open file */ if ((fp = OpenConfFile(filename, &cf)) == NULL) - return(-1); + return(-2); /* Find label */ @@ -851,7 +853,8 @@ GenerateMagic(void) int PIDCheck(const char *filename, int killem) { - int fd = -1, n_tries; + int n_tries; + struct pidfh *pfh = NULL; /* Sanity */ @@ -859,88 +862,43 @@ PIDCheck(const char *filename, int killem) /* Atomically open and lock file */ - for (n_tries = 0; - n_tries < MAX_LOCK_ATTEMPTS - && (fd = open(filename, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0644)) < 0; - n_tries++) + for (n_tries = 0; n_tries < MAX_LOCK_ATTEMPTS; n_tries++) { - int nscan, old_pid; - FILE *fp; + pid_t old_pid; - /* Abort on any unexpected errors */ - - if (errno != EAGAIN) - { - Perror("%s: open(%s)", __FUNCTION__, filename); - return(-1); + pfh = pidfile_open(filename, 0644, &old_pid); + if (pfh == NULL) { + if (errno == EEXIST) { + 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); + } 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) { Log(LG_ERR, ("can't lock %s after %d attempts", filename, n_tries)); 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); } @@ -1019,7 +977,7 @@ TcpGetListenPort(struct u_addr *addr, in_port_t port, /* Make socket available for connections */ - if (listen(sock, 2) < 0) + if (listen(sock, -1) < 0) { Perror("%s: listen", __FUNCTION__); (void) close(sock); @@ -1531,3 +1489,19 @@ ppp_util_ascify(char *buf, size_t bsiz, const char *da *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