1/* 2 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu) 3 * All rights reserved. See COPYRIGHT. 4 * 5 * Copyright (c) 1990,1993 Regents of The University of Michigan. 6 * All Rights Reserved. See COPYRIGHT. 7 */ 8 9#ifdef HAVE_CONFIG_H 10#include "config.h" 11#endif 12 13#include <stdio.h> 14#include <stdlib.h> 15#include <string.h> 16#include <unistd.h> 17#include <fcntl.h> 18#include <termios.h> 19#include <sys/ioctl.h> 20#include <sys/types.h> 21#include <sys/stat.h> 22#include <errno.h> 23 24#include <sys/time.h> 25 26#include <atalk/compat.h> 27#include <atalk/util.h> 28 29static struct itimerval itimer; 30 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. */ 34pid_t server_lock(char *program, char *pidfile, int debug) 35{ 36 char buf[10]; 37 FILE *pf; 38 pid_t pid; 39 int mask; 40 41 mask = umask(022); 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", 46 program, pid); 47 fclose(pf); 48 return -1; 49 } 50 fclose(pf); 51 } 52 53 if ((pf = fopen(pidfile, "w")) == NULL) { 54 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program, 55 pidfile); 56 return -1; 57 } 58 umask(mask); 59 60 /* 61 * Disassociate from controlling tty. 62 */ 63 if ( !debug ) { 64 int i; 65 66 getitimer(ITIMER_PROF, &itimer); 67 switch (pid = fork()) { 68 case 0 : 69 setitimer(ITIMER_PROF, &itimer, NULL); 70 fclose(stdin); 71 fclose(stdout); 72 fclose(stderr); 73 i = open( "/dev/null", O_RDWR ); 74 i = open( "/dev/null", O_RDWR ); 75 i = open( "/dev/null", O_RDWR ); 76 77#ifdef TIOCNOTTY 78 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) { 79 (void)ioctl( i, TIOCNOTTY, 0 ); 80 setpgid( 0, getpid()); 81 (void) close(i); 82 } 83#else 84 setpgid( 0, getpid()); 85#endif 86 break; 87 case -1 : /* error */ 88 perror( "fork" ); 89 default : /* server */ 90 fclose(pf); 91 return pid; 92 } 93 } 94 95 fprintf(pf, "%d\n", getpid()); 96 fclose(pf); 97 return 0; 98} 99 100/*! 101 * Check lockfile 102 */ 103int check_lockfile(const char *program, const char *pidfile) 104{ 105 char buf[10]; 106 FILE *pf; 107 pid_t pid; 108 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", 113 program, pid); 114 fclose(pf); 115 return -1; 116 } 117 fclose(pf); 118 } 119 return 0; 120} 121 122/*! 123 * Check and create lockfile 124 */ 125int create_lockfile(const char *program, const char *pidfile) 126{ 127 char buf[10]; 128 FILE *pf; 129 pid_t pid; 130 int mask; 131 132 if (check_lockfile(program, pidfile) != 0) 133 return -1; 134 135 /* Write PID to pidfile */ 136 mask = umask(022); 137 if ((pf = fopen(pidfile, "w")) == NULL) { 138 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program, 139 pidfile); 140 return -1; 141 } 142 umask(mask); 143 fprintf(pf, "%d\n", getpid()); 144 fclose(pf); 145 146 return 0; 147} 148