2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Amitay Isaccs 2016
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
24 #include "lib/util/blocking.h"
25 #include "lib/util/debug.h"
26 #include "lib/util/samba_util.h" /* For process_exists_by_pid() */
28 #include "lib/util/pidfile.h"
30 int pidfile_path_create(const char *path
, int *outfd
)
41 fd
= open(path
, O_CREAT
|O_WRONLY
|O_NONBLOCK
, 0644);
46 if (! set_close_on_exec(fd
)) {
51 lck
= (struct flock
) {
57 ret
= fcntl(fd
, F_SETLK
, &lck
);
58 } while ((ret
== -1) && (errno
== EINTR
));
67 * PID file is locked by us so from here on we should unlink
72 ret
= ftruncate(fd
, 0);
73 } while ((ret
== -1) && (errno
== EINTR
));
80 len
= snprintf(tmp
, sizeof(tmp
), "%u\n", pid
);
85 if (len
>= sizeof(tmp
)) {
91 nwritten
= write(fd
, tmp
, len
);
92 } while ((nwritten
== -1) && (errno
== EINTR
));
94 if ((nwritten
== -1) || (nwritten
!= len
)) {
110 void pidfile_fd_close(int fd
)
114 .l_whence
= SEEK_SET
,
119 ret
= fcntl(fd
, F_SETLK
, &lck
);
120 } while ((ret
== -1) && (errno
== EINTR
));
124 } while ((ret
== -1) && (errno
== EINTR
));
129 * return the pid in a pidfile. return 0 if the process (or pidfile)
132 pid_t
pidfile_pid(const char *piddir
, const char *name
)
134 size_t len
= strlen(piddir
) + strlen(name
) + 6;
140 snprintf(pidFile
, sizeof(pidFile
), "%s/%s.pid", piddir
, name
);
142 fd
= open(pidFile
, O_NONBLOCK
| O_RDONLY
, 0644);
150 if (read(fd
, pidstr
, sizeof(pidstr
)-1) <= 0) {
154 ret
= (pid_t
)atoi(pidstr
);
156 DEBUG(1, ("Could not parse contents of pidfile %s\n",
161 if (!process_exists_by_pid(ret
)) {
162 DEBUG(10, ("Process with PID=%d does not exist.\n", (int)ret
));
166 if (fcntl_lock(fd
,F_SETLK
,0,1,F_RDLCK
)) {
167 /* we could get the lock - it can't be a Samba process */
168 DEBUG(10, ("Process with PID=%d is not a Samba process.\n",
174 DEBUG(10, ("Process with PID=%d is running.\n", (int)ret
));
183 * create a pid file in the pid directory. open it and leave it locked
185 void pidfile_create(const char *piddir
, const char *name
)
187 size_t len
= strlen(piddir
) + strlen(name
) + 6;
192 snprintf(pidFile
, sizeof(pidFile
), "%s/%s.pid", piddir
, name
);
194 pid
= pidfile_pid(piddir
, name
);
196 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
197 name
, pidFile
, (int)pid
));
201 ret
= pidfile_path_create(pidFile
, NULL
);
203 DBG_ERR("ERROR: Failed to create PID file %s (%s)\n",
204 pidFile
, strerror(ret
));
208 /* Leave pid file open & locked for the duration... */
211 void pidfile_unlink(const char *piddir
, const char *name
)
214 char *pidFile
= NULL
;
216 if (asprintf(&pidFile
, "%s/%s.pid", piddir
, name
) < 0) {
217 DEBUG(0,("ERROR: Out of memory\n"));
220 ret
= unlink(pidFile
);
222 DEBUG(0,("Failed to delete pidfile %s. Error was %s\n",
223 pidFile
, strerror(errno
)));