--- src/minilogd.c +++ src/minilogd.c @@ -19,6 +19,8 @@ #include #include +#define LOG_LOCK "/var/lock/subsys/minilogd" + static int we_own_log=0; static char **buffer=NULL; static int buflines=0; @@ -139,22 +141,49 @@ int main(int argc, char **argv) { struct sockaddr_un addr; int sock; int pid; + int we_hold_lock = 0; + int cannot_lock = 0; + int i; /* option processing made simple... */ if (argc>1) debug=1; - /* just in case */ - sock = open("/dev/null",O_RDWR); - dup2(sock,0); - dup2(sock,1); - dup2(sock,2); - bzero(&addr, sizeof(addr)); addr.sun_family = AF_LOCAL; strncpy(addr.sun_path,_PATH_LOG,sizeof(addr.sun_path)-1); sock = socket(AF_LOCAL, SOCK_STREAM,0); + + for (i = 0; i < 10; i++) { + if (open(LOG_LOCK, O_WRONLY | O_CREAT | O_EXCL, 0) == -1) { + if (errno == EEXIST) { + sleep(1); + continue; + } else { + cannot_lock = 1; + break; + } + } else { + we_hold_lock = 1; + break; + } + } + + if (!cannot_lock && !we_hold_lock) { + fprintf(stderr, "minilogd: cannot get lock for /dev/log\n"); + exit(6); + } + unlink(_PATH_LOG); + + /* just in case */ + sock = open("/dev/null",O_RDWR); + dup2(sock,0); + dup2(sock,1); + dup2(sock,2); + /* Bind socket before forking, so we know if the server started */ if (!bind(sock,(struct sockaddr *) &addr, sizeof(addr))) { + if (we_hold_lock) + unlink(LOG_LOCK); we_own_log = 1; listen(sock,5); if ((pid=fork())==-1) { @@ -169,6 +198,8 @@ int main(int argc, char **argv) { exit(4); } } else { + if (we_hold_lock) + unlink(LOG_LOCK); exit(5); } }