Index: daemon.c =================================================================== --- daemon.c (revision 1) +++ daemon.c (working copy) @@ -25,7 +25,7 @@ #include #include #include - +#include #if HAVE_RPC_SVC_SOC_H == 1 # include #endif @@ -54,6 +54,7 @@ int opt_expire_writers = FALSE; int opt_detach = TRUE; char *opt_exports = "/etc/exports"; +char *opt_pidfile = "/var/run/unfsd.pid"; int opt_cluster = FALSE; char *opt_cluster_path = "/"; int opt_tcponly = FALSE; @@ -114,7 +115,7 @@ static void parse_options(int argc, char **argv) { int opt = 0; - char *optstring = "bcC:de:hl:m:n:pstuw"; + char *optstring = "bcC:de:hl:m:n:pstuwi:"; while (opt != -1) { opt = getopt(argc, argv, optstring); @@ -210,6 +211,9 @@ case '?': exit(1); break; + case 'i': + opt_pidfile = optarg; + break; } } } @@ -256,6 +260,12 @@ backend_shutdown(); + // killpid + fprintf(stderr, "Deleting pidfile %s\n", opt_pidfile); // ok, if fork done this mesage goes to /dev/null + if (unlink(opt_pidfile) == -1) { + logmsg(LOG_EMERG,"Failed to delete pidfile"); + } + exit(1); } @@ -770,6 +780,21 @@ /* no umask to not screw up create modes */ umask(0); + // pidfile always created here - by parent if !opt_detach or by child + int pid_fd = open(opt_pidfile, O_RDWR | O_CREAT, 0640); + if (pid_fd < 0) { + fprintf(stderr, "Failed to open pidfile %s: %s!\n", opt_pidfile, strerror(errno)); + exit(0); + } + if (lockf(pid_fd, F_TLOCK, 0) < 0) { + fprintf(stderr, "Failed to get a lock on pidfile %s: %s!\n", opt_pidfile, strerror(errno)); + exit(0); + } + char str[16]; + + snprintf(str, sizeof(str), "%d\n", getpid()); + write(pid_fd, str, strlen(str)); + /* detach from terminal */ if (opt_detach) { setsid(); Index: daemon.h =================================================================== --- daemon.h (revision 1) +++ daemon.h (working copy) @@ -33,6 +33,7 @@ extern int opt_expire_writers; extern int opt_detach; extern char *opt_exports; +extern char *opt_pidfile; extern int opt_cluster; extern char *opt_cluster_path; extern int opt_singleuser;