K&R style braces
[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 #ifndef HAVE_MINGW
31 /* read_pid
32  *
33  * Reads the specified pidfile and returns the read pid.
34  * 0 is returned if either there's no pidfile, it's empty
35  * or no pid can be read.
36  */
37 pid_t read_pid (char *pidfile) {
38   FILE *f;
39   long pid;
40
41   if (!(f=fopen(pidfile,"r")))
42     return 0;
43   fscanf(f,"%ld", &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 the pid is returned, otherwise 0.
53  */
54 pid_t check_pid (char *pidfile) {
55   pid_t pid = read_pid(pidfile);
56
57   /* Amazing ! _I_ am already holding the pid file... */
58   if ((!pid) || (pid == getpid ()))
59     return 0;
60
61   /*
62    * The 'standard' method of doing this is to try and do a 'fake' kill
63    * of the process.  If an ESRCH error is returned the process cannot
64    * be found -- GW
65    */
66   /* But... errno is usually changed only on error.. */
67   errno = 0;
68   if (kill(pid, 0) && errno == ESRCH)
69           return 0;
70
71   return pid;
72 }
73
74 /* write_pid
75  *
76  * Writes the pid to the specified file. If that fails 0 is
77  * returned, otherwise the pid.
78  */
79 pid_t write_pid (char *pidfile) {
80   FILE *f;
81   int fd;
82   pid_t pid;
83
84   if ((fd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1) {
85       return 0;
86   }
87
88   if ((f = fdopen(fd, "r+")) == NULL) {
89       close(fd);
90       return 0;
91   }
92   
93 #ifdef HAVE_FLOCK
94   if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
95       fclose(f);
96       return 0;
97   }
98 #endif
99
100   pid = getpid();
101   if (!fprintf(f,"%ld\n", (long)pid)) {
102       fclose(f);
103       return 0;
104   }
105   fflush(f);
106
107 #ifdef HAVE_FLOCK
108   if (flock(fd, LOCK_UN) == -1) {
109       fclose(f);
110       return 0;
111   }
112 #endif
113   fclose(f);
114
115   return pid;
116 }
117
118 /* remove_pid
119  *
120  * Remove the the specified file. The result from unlink(2)
121  * is returned
122  */
123 int remove_pid (char *pidfile) {
124   return unlink (pidfile);
125 }
126 #endif