Releasing 1.0.22.
[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 along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include "pidfile.h"
31
32 #ifndef HAVE_MINGW
33 /* read_pid
34  *
35  * Reads the specified pidfile and returns the read pid.
36  * 0 is returned if either there's no pidfile, it's empty
37  * or no pid can be read.
38  */
39 pid_t read_pid (const char *pidfile)
40 {
41   FILE *f;
42   long pid;
43
44   if (!(f=fopen(pidfile,"r")))
45     return 0;
46   if(fscanf(f,"%20ld", &pid) != 1)
47     pid = 0;
48   fclose(f);
49   return pid;
50 }
51
52 /* check_pid
53  *
54  * Reads the pid using read_pid and looks up the pid in the process
55  * table (using /proc) to determine if the process already exists. If
56  * so the pid is returned, otherwise 0.
57  */
58 pid_t check_pid (const char *pidfile)
59 {
60   pid_t pid = read_pid(pidfile);
61
62   /* Amazing ! _I_ am already holding the pid file... */
63   if ((!pid) || (pid == getpid ()))
64     return 0;
65
66   /*
67    * The 'standard' method of doing this is to try and do a 'fake' kill
68    * of the process.  If an ESRCH error is returned the process cannot
69    * be found -- GW
70    */
71   /* But... errno is usually changed only on error.. */
72   errno = 0;
73   if (kill(pid, 0) && errno == ESRCH)
74           return 0;
75
76   return pid;
77 }
78
79 /* write_pid
80  *
81  * Writes the pid to the specified file. If that fails 0 is
82  * returned, otherwise the pid.
83  */
84 pid_t write_pid (const char *pidfile)
85 {
86   FILE *f;
87   int fd;
88   pid_t pid;
89
90   if ((fd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1) {
91       return 0;
92   }
93
94   if ((f = fdopen(fd, "r+")) == NULL) {
95       close(fd);
96       return 0;
97   }
98   
99 #ifdef HAVE_FLOCK
100   if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
101       fclose(f);
102       return 0;
103   }
104 #endif
105
106   pid = getpid();
107   if (!fprintf(f,"%ld\n", (long)pid)) {
108       fclose(f);
109       return 0;
110   }
111   fflush(f);
112
113 #ifdef HAVE_FLOCK
114   if (flock(fd, LOCK_UN) == -1) {
115       fclose(f);
116       return 0;
117   }
118 #endif
119   fclose(f);
120
121   return pid;
122 }
123
124 /* remove_pid
125  *
126  * Remove the the specified file. The result from unlink(2)
127  * is returned
128  */
129 int remove_pid (const char *pidfile)
130 {
131   return unlink (pidfile);
132 }
133 #endif