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 if ( !debug ) { 42 mask = umask(022); 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", 47 program, pid); 48 fclose(pf); 49 return -1; 50 } 51 fclose(pf); 52 } 53 54 if ((pf = fopen(pidfile, "w")) == NULL) { 55 fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program, 56 pidfile); 57 return -1; 58 } 59 umask(mask); 60 61 /* 62 * Disassociate from controlling tty. 63 */ 64 65 int i; 66 67 getitimer(ITIMER_PROF, &itimer); 68 switch (pid = fork()) { 69 case 0 : 70 setitimer(ITIMER_PROF, &itimer, NULL); 71 fclose(stdin); 72 fclose(stdout); 73 fclose(stderr); 74 i = open( "/dev/null", O_RDWR ); 75 i = open( "/dev/null", O_RDWR ); 76 i = open( "/dev/null", O_RDWR ); 77 78#ifdef TIOCNOTTY 79 if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) { 80 (void)ioctl( i, TIOCNOTTY, 0 ); 81 setpgid( 0, getpid()); 82 (void) close(i); 83 } 84#else 85 setpgid( 0, getpid()); 86#endif 87 break; 88 case -1 : /* error */ 89 perror( "fork" ); 90 default : /* server */ 91 fclose(pf); 92 return pid; 93 } 94 95 fprintf(pf, "%d\n", getpid()); 96 fclose(pf); 97 } 98 99 return 0; 100} 101 102/*! 103 * Check lockfile 104 */ 105int check_lockfile(const char *program, const char *pidfile) 106{ 107 char buf[10]; 108 FILE *pf; 109 pid_t pid; 110 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", 115 program, pid); 116 fclose(pf); 117 return -1; 118 } 119 fclose(pf); 120 } 121 return 0; 122} 123 124/*! 125 * Check and create lockfile 126 */ 127int create_lockfile(const char *program, const char *pidfile) 128{ 129 FILE *pf; 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