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.
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
19 #include <atalk/compat.h>
20 #include <atalk/util.h>
22 /* this creates an open lock file which hangs around until the program
23 * dies. it returns the pid. due to problems w/ solaris, this has
24 * been changed to do the kill() thing. */
25 pid_t server_lock(char *program, char *pidfile, int debug)
33 /* check for pid. this can get fooled by stale pid's. */
34 if ((pf = fopen(pidfile, "r"))) {
35 if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
36 fprintf( stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
44 if ((pf = fopen(pidfile, "w")) == NULL) {
45 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
52 * Disassociate from controlling tty.
57 switch (pid = fork()) {
63 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
64 (void)ioctl( i, TIOCNOTTY, 0 );
65 setpgid( 0, getpid());
71 default : /* server */
77 fprintf(pf, "%d\n", getpid());