2 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3 * All rights reserved. See COPYRIGHT.
5 * Copyright (c) 1990,1993 Regents of The University of Michigan.
6 * All Rights Reserved. See COPYRIGHT.
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
24 #include <atalk/compat.h>
25 #include <atalk/util.h>
27 /* this creates an open lock file which hangs around until the program
28 * dies. it returns the pid. due to problems w/ solaris, this has
29 * been changed to do the kill() thing. */
30 pid_t server_lock(char *program, char *pidfile, int debug)
38 /* check for pid. this can get fooled by stale pid's. */
39 if ((pf = fopen(pidfile, "r"))) {
40 if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
41 fprintf( stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
49 if ((pf = fopen(pidfile, "w")) == NULL) {
50 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
57 * Disassociate from controlling tty.
62 switch (pid = fork()) {
69 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
70 (void)ioctl( i, TIOCNOTTY, 0 );
71 setpgid( 0, getpid());
75 setpgid( 0, getpid());
80 default : /* server */
86 fprintf(pf, "%d\n", getpid());