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>
26 #include <atalk/compat.h>
27 #include <atalk/util.h>
30 #define fork aTaC_fork
32 static struct itimerval itimer;
34 /* this creates an open lock file which hangs around until the program
35 * dies. it returns the pid. due to problems w/ solaris, this has
36 * been changed to do the kill() thing. */
37 pid_t server_lock(char *program, char *pidfile, int debug)
45 /* check for pid. this can get fooled by stale pid's. */
46 if ((pf = fopen(pidfile, "r"))) {
47 if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
48 fprintf( stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
56 if ((pf = fopen(pidfile, "w")) == NULL) {
57 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
64 * Disassociate from controlling tty.
69 getitimer(ITIMER_PROF, &itimer);
70 switch (pid = fork()) {
72 setitimer(ITIMER_PROF, &itimer, NULL);
76 i = open( "/dev/null", O_RDWR );
77 i = open( "/dev/null", O_RDWR );
78 i = open( "/dev/null", O_RDWR );
81 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
82 (void)ioctl( i, TIOCNOTTY, 0 );
83 setpgid( 0, getpid());
87 setpgid( 0, getpid());
92 default : /* server */
98 fprintf(pf, "%d\n", getpid());