X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/hmcfgusb/blobdiff_plain/b0bf6ad2612d6459451e9a38cb1059b567990eb8..2a6eaefc7786a666d27c52a8e9bb0c1c4aeb2843:/hmland.c diff --git a/hmland.c b/hmland.c index 519e02b..eaf8521 100644 --- a/hmland.c +++ b/hmland.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -297,7 +298,7 @@ static int hmlan_parse_in(int fd, void *data) memset(out, 0, sizeof(out)); *outpos++ = *inpos++; - switch(buf[0]) { + switch(*instart) { case 'S': parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); parse_part_in(&inpos, (last-(inpos-instart)), &outpos, (sizeof(out)-(outpos-out)), 0); @@ -405,14 +406,36 @@ static int socket_server(char *iface, int port, int flags) FILE *pidfile = NULL; if (flags & FLAG_PID_FILE) { - mode_t old_umask; - - old_umask = umask(022); - pidfile = fopen(PID_FILE, "w"); - umask(old_umask); + int fd; + + fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644); + if (fd == -1) { + if (errno == EEXIST) { + pid_t old_pid; + pidfile = fopen(PID_FILE, "r"); + if (!pidfile) { + perror("PID file " PID_FILE " already exists, already running?"); + exit(EXIT_FAILURE); + } + + if (fscanf(pidfile, "%u", &old_pid) != 1) { + fclose(pidfile); + fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n"); + exit(EXIT_FAILURE); + } + + fclose(pidfile); + + fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid); + exit(EXIT_FAILURE); + } + perror("Can't create PID file " PID_FILE); + exit(EXIT_FAILURE); + } + pidfile = fdopen(fd, "w"); if (!pidfile) { - perror("Can't create PID file " PID_FILE); + perror("Can't reopen PID file fd"); exit(EXIT_FAILURE); }