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