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

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

Samba Server: updated trunk to 3.6.0

File size: 4.5 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 *name)
34{
35 int fd;
36 char pidstr[20];
37 pid_t pid;
38 unsigned int ret;
39 char * pidFile;
40
41 if (asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name) == -1) {
42 return 0;
43 }
44
45 fd = sys_open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
46 if (fd == -1) {
47 SAFE_FREE(pidFile);
48 return 0;
49 }
50
51 ZERO_ARRAY(pidstr);
52
53 if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
54 goto noproc;
55 }
56
57 ret = atoi(pidstr);
58
59 if (ret == 0) {
60 /* Obviously we had some garbage in the pidfile... */
61 DEBUG(1, ("Could not parse contents of pidfile %s\n",
62 pidFile));
63 goto noproc;
64 }
65
66 pid = (pid_t)ret;
67 if (!process_exists_by_pid(pid)) {
68 goto noproc;
69 }
70
71#ifndef __OS2__ // if we lock the file, we won't be able to read it later on OS/2
72 if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_RDLCK)) {
73 /* we could get the lock - it can't be a Samba process */
74 goto noproc;
75 }
76#endif
77
78 SAFE_FREE(pidFile);
79 close(fd);
80 return (pid_t)ret;
81
82 noproc:
83 close(fd);
84 unlink(pidFile);
85 SAFE_FREE(pidFile);
86 return 0;
87}
88
89/* create a pid file in the pid directory. open it and leave it locked */
90void pidfile_create(const char *program_name)
91{
92 int fd;
93 char buf[20];
94 const char *short_configfile;
95 char *name;
96 pid_t pid;
97
98 /* Add a suffix to the program name if this is a process with a
99 * none default configuration file name. */
100 /* On OS/2, CONFIGFILE will always be different to dyn_CONFIGFILE
101 as dyn_CONFIGFILE dynamically looks up the system ETC directory */
102#ifndef __OS2__
103 if (strcmp( CONFIGFILE, get_dyn_CONFIGFILE()) == 0) {
104 name = SMB_STRDUP(program_name);
105 } else {
106 short_configfile = strrchr( get_dyn_CONFIGFILE(), '/');
107 if (short_configfile == NULL) {
108 /* conf file in current directory */
109 short_configfile = get_dyn_CONFIGFILE();
110 } else {
111 /* full/relative path provided */
112 short_configfile++;
113 }
114 if (asprintf(&name, "%s-%s", program_name,
115 short_configfile) == -1) {
116 smb_panic("asprintf failed");
117 }
118 }
119#else
120 name = SMB_STRDUP(program_name);
121#endif
122
123 if (asprintf(&pidFile_name, "%s/%s.pid", lp_piddir(), name) == -1) {
124 smb_panic("asprintf failed");
125 }
126
127 pid = pidfile_pid(name);
128 if (pid != 0) {
129 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
130 name, pidFile_name, (int)pid));
131 exit(1);
132 }
133
134 fd = sys_open(pidFile_name, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL,
135 0644);
136 if (fd == -1) {
137 DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile_name,
138 strerror(errno)));
139 exit(1);
140 }
141
142 if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) {
143 DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",
144 name, pidFile_name, strerror(errno)));
145 exit(1);
146 }
147
148 memset(buf, 0, sizeof(buf));
149 slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid());
150 if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
151 DEBUG(0,("ERROR: can't write to file %s: %s\n",
152 pidFile_name, strerror(errno)));
153 exit(1);
154 }
155 /* Leave pid file open & locked for the duration... */
156 SAFE_FREE(name);
157
158#ifdef __OS2__ // If we leave the file open & locked on OS/2 - we can't read it, so close the fd
159 close(fd);
160#else
161 /* set the close on exec so that we don't leak the fd */
162 fcntl(fd, F_SETFD, FD_CLOEXEC);
163#endif
164}
165
166void pidfile_unlink(void)
167{
168 if (pidFile_name == NULL) {
169 return;
170 }
171 unlink(pidFile_name);
172 SAFE_FREE(pidFile_name);
173}
Note: See TracBrowser for help on using the repository browser.