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>
29 static struct itimerval itimer;
31 /* this creates an open lock file which hangs around until the program
32 * dies. it returns the pid. due to problems w/ solaris, this has
33 * been changed to do the kill() thing. */
34 pid_t server_lock(char *program, char *pidfile, int debug)
43 /* check for pid. this can get fooled by stale pid's. */
44 if ((pf = fopen(pidfile, "r"))) {
45 if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
46 fprintf( stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
54 if ((pf = fopen(pidfile, "w")) == NULL) {
55 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
62 * Disassociate from controlling tty.
67 getitimer(ITIMER_PROF, &itimer);
68 switch (pid = fork()) {
70 setitimer(ITIMER_PROF, &itimer, NULL);
74 i = open( "/dev/null", O_RDWR );
75 i = open( "/dev/null", O_RDWR );
76 i = open( "/dev/null", O_RDWR );
79 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
80 (void)ioctl( i, TIOCNOTTY, 0 );
81 setpgid( 0, getpid());
85 setpgid( 0, getpid());
90 default : /* server */
95 fprintf(pf, "%d\n", getpid());
105 int check_lockfile(const char *program, const char *pidfile)
111 /* check for pid. this can get fooled by stale pid's. */
112 if ((pf = fopen(pidfile, "r"))) {
113 if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
114 fprintf(stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
125 * Check and create lockfile
127 int create_lockfile(const char *program, const char *pidfile)
132 if (check_lockfile(program, pidfile) != 0)
135 /* Write PID to pidfile */
137 if ((pf = fopen(pidfile, "w")) == NULL) {
138 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
143 fprintf(pf, "%d\n", getpid());