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)
42 /* check for pid. this can get fooled by stale pid's. */
43 if ((pf = fopen(pidfile, "r"))) {
44 if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
45 fprintf( stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
53 if ((pf = fopen(pidfile, "w")) == NULL) {
54 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
61 * Disassociate from controlling tty.
66 getitimer(ITIMER_PROF, &itimer);
67 switch (pid = fork()) {
69 setitimer(ITIMER_PROF, &itimer, NULL);
73 i = open( "/dev/null", O_RDWR );
74 i = open( "/dev/null", O_RDWR );
75 i = open( "/dev/null", O_RDWR );
78 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
79 (void)ioctl( i, TIOCNOTTY, 0 );
80 setpgid( 0, getpid());
84 setpgid( 0, getpid());
89 default : /* server */
95 fprintf(pf, "%d\n", getpid());
103 int check_lockfile(const char *program, const char *pidfile)
109 /* check for pid. this can get fooled by stale pid's. */
110 if ((pf = fopen(pidfile, "r"))) {
111 if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
112 fprintf(stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
123 * Check and create lockfile
125 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());