ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/lib/pidfile.c
Revision: 1.3
Committed: Thu Mar 3 16:54:34 2005 UTC (19 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_9, rel-1_8, rel-2_01, rel-3_0, rel-2_2, rel-2_0, rel-2_21, rel-2_22, rel-2_25, HEAD
Changes since 1.2: +4 -2 lines
Log Message:
*** empty log message ***

File Contents

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