source: trunk/server/source3/lib/pidfile.c

Last change on this file was 755, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.6: remove pid when exiting

File size: 5.4 KB
Line 
1/* this code is broken - there is a race condition with the unlink (tridge) */
2
3/*
4 Unix SMB/CIFS implementation.
5 pidfile handling
6 Copyright (C) Andrew Tridgell 1998
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "system/filesys.h"
24
25#ifndef O_NONBLOCK
26#define O_NONBLOCK
27#endif
28
29static char *pidFile_name = NULL;
30
31/* return the pid in a pidfile. return 0 if the process (or pidfile)
32 does not exist */
33pid_t pidfile_pid(const char *program_name)
34{
35 int fd;
36 char pidstr[20];
37 pid_t pid;
38 unsigned int ret;
39 char *name;
40 const char *short_configfile;
41 char * pidFile;
42
43 /* Add a suffix to the program name if this is a process with a
44 * none default configuration file name. */
45 /* On OS/2, CONFIGFILE will always be different to dyn_CONFIGFILE
46 as dyn_CONFIGFILE dynamically looks up the system ETC directory */
47#ifndef __OS2__
48 if (strcmp( CONFIGFILE, get_dyn_CONFIGFILE()) == 0) {
49 name = SMB_STRDUP(program_name);
50 } else {
51 short_configfile = strrchr( get_dyn_CONFIGFILE(), '/');
52 if (short_configfile == NULL) {
53 /* conf file in current directory */
54 short_configfile = get_dyn_CONFIGFILE();
55 } else {
56 /* full/relative path provided */
57 short_configfile++;
58 }
59 if (asprintf(&name, "%s-%s", program_name,
60 short_configfile) == -1) {
61 smb_panic("asprintf failed");
62 }
63 }
64#else
65 name = SMB_STRDUP(program_name);
66#endif
67
68 if (asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name) == -1) {
69 SAFE_FREE(name);
70 return 0;
71 }
72
73 SAFE_FREE(name);
74
75 fd = sys_open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
76 if (fd == -1) {
77 SAFE_FREE(pidFile);
78 return 0;
79 }
80
81 ZERO_ARRAY(pidstr);
82
83 if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
84 goto noproc;
85 }
86
87 ret = atoi(pidstr);
88
89 if (ret == 0) {
90 /* Obviously we had some garbage in the pidfile... */
91 DEBUG(1, ("Could not parse contents of pidfile %s\n",
92 pidFile));
93 goto noproc;
94 }
95
96 pid = (pid_t)ret;
97 if (!process_exists_by_pid(pid)) {
98 goto noproc;
99 }
100
101#ifndef __OS2__ // if we lock the file, we won't be able to read it later on OS/2
102 if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_RDLCK)) {
103 /* we could get the lock - it can't be a Samba process */
104 goto noproc;
105 }
106#endif
107
108 SAFE_FREE(pidFile);
109 close(fd);
110 return (pid_t)ret;
111
112 noproc:
113 close(fd);
114 unlink(pidFile);
115 SAFE_FREE(pidFile);
116 return 0;
117}
118
119/* create a pid file in the pid directory. open it and leave it locked */
120void pidfile_create(const char *program_name)
121{
122 int fd;
123 char buf[20];
124 const char *short_configfile;
125 char *name;
126 pid_t pid;
127
128 /* Add a suffix to the program name if this is a process with a
129 * none default configuration file name. */
130 /* On OS/2, CONFIGFILE will always be different to dyn_CONFIGFILE
131 as dyn_CONFIGFILE dynamically looks up the system ETC directory */
132#ifndef __OS2__
133 if (strcmp( CONFIGFILE, get_dyn_CONFIGFILE()) == 0) {
134 name = SMB_STRDUP(program_name);
135 } else {
136 short_configfile = strrchr( get_dyn_CONFIGFILE(), '/');
137 if (short_configfile == NULL) {
138 /* conf file in current directory */
139 short_configfile = get_dyn_CONFIGFILE();
140 } else {
141 /* full/relative path provided */
142 short_configfile++;
143 }
144 if (asprintf(&name, "%s-%s", program_name,
145 short_configfile) == -1) {
146 smb_panic("asprintf failed");
147 }
148 }
149#else
150 name = SMB_STRDUP(program_name);
151#endif
152
153 if (asprintf(&pidFile_name, "%s/%s.pid", lp_piddir(), name) == -1) {
154 smb_panic("asprintf failed");
155 }
156
157 pid = pidfile_pid(name);
158 if (pid != 0) {
159 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
160 name, pidFile_name, (int)pid));
161 exit(1);
162 }
163
164 fd = sys_open(pidFile_name, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL,
165 0644);
166 if (fd == -1) {
167 DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile_name,
168 strerror(errno)));
169 exit(1);
170 }
171
172 if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) {
173 DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",
174 name, pidFile_name, strerror(errno)));
175 exit(1);
176 }
177
178 memset(buf, 0, sizeof(buf));
179 slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid());
180 if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
181 DEBUG(0,("ERROR: can't write to file %s: %s\n",
182 pidFile_name, strerror(errno)));
183 exit(1);
184 }
185 /* Leave pid file open & locked for the duration... */
186 SAFE_FREE(name);
187
188#ifdef __OS2__ // If we leave the file open & locked on OS/2 - we can't read it, so close the fd
189 close(fd);
190#else
191 /* set the close on exec so that we don't leak the fd */
192 fcntl(fd, F_SETFD, FD_CLOEXEC);
193#endif
194}
195
196void pidfile_unlink(void)
197{
198 if (pidFile_name == NULL) {
199 return;
200 }
201 unlink(pidFile_name);
202 SAFE_FREE(pidFile_name);
203}
Note: See TracBrowser for help on using the repository browser.