Big header file cleanup: everything that has to do with standard system
[tinc] / lib / pidfile.c
1 /*
2     pidfile.c - interact with pidfiles
3     Copyright (c) 1995  Martin Schulze <Martin.Schulze@Linux.DE>
4
5     This file is part of the sysklogd package, a kernel and system log daemon.
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA
20 */
21
22 /* left unaltered for tinc -- Ivo Timmermans */
23 /*
24  * Sat Aug 19 13:24:33 MET DST 1995: Martin Schulze
25  *      First version (v0.2) released
26  */
27
28 #include "system.h"
29
30 /* read_pid
31  *
32  * Reads the specified pidfile and returns the read pid.
33  * 0 is returned if either there's no pidfile, it's empty
34  * or no pid can be read.
35  */
36 int read_pid (char *pidfile)
37 {
38   FILE *f;
39   int pid;
40
41   if (!(f=fopen(pidfile,"r")))
42     return 0;
43   fscanf(f,"%d", &pid);
44   fclose(f);
45   return pid;
46 }
47
48 /* check_pid
49  *
50  * Reads the pid using read_pid and looks up the pid in the process
51  * table (using /proc) to determine if the process already exists. If
52  * so 1 is returned, otherwise 0.
53  */
54 int check_pid (char *pidfile)
55 {
56   int pid = read_pid(pidfile);
57
58   /* Amazing ! _I_ am already holding the pid file... */
59   if ((!pid) || (pid == getpid ()))
60     return 0;
61
62   /*
63    * The 'standard' method of doing this is to try and do a 'fake' kill
64    * of the process.  If an ESRCH error is returned the process cannot
65    * be found -- GW
66    */
67   /* But... errno is usually changed only on error.. */
68   errno = 0;
69   if (kill(pid, 0) && errno == ESRCH)
70           return(0);
71
72   return pid;
73 }
74
75 /* write_pid
76  *
77  * Writes the pid to the specified file. If that fails 0 is
78  * returned, otherwise the pid.
79  */
80 int write_pid (char *pidfile)
81 {
82   FILE *f;
83   int fd;
84   int pid;
85
86   if ( ((fd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1)
87        || ((f = fdopen(fd, "r+")) == NULL) ) {
88       fprintf(stderr, "Can't open or create %s.\n", pidfile);
89       return 0;
90   }
91   
92 #ifdef HAVE_FLOCK
93   if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
94       fscanf(f, "%d", &pid);
95       fclose(f);
96       printf("Can't lock, lock is held by pid %d.\n", pid);
97       return 0;
98   }
99 #endif
100
101   pid = getpid();
102   if (!fprintf(f,"%d\n", pid)) {
103       printf("Can't write pid , %s.\n", strerror(errno));
104       close(fd);
105       return 0;
106   }
107   fflush(f);
108
109 #ifdef HAVE_FLOCK
110   if (flock(fd, LOCK_UN) == -1) {
111       printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
112       close(fd);
113       return 0;
114   }
115 #endif
116   close(fd);
117
118   return pid;
119 }
120
121 /* remove_pid
122  *
123  * Remove the the specified file. The result from unlink(2)
124  * is returned
125  */
126 int remove_pid (char *pidfile)
127 {
128   return unlink (pidfile);
129 }
130