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 *pfd
, pid_t
*existing_pid
)
39 fd
= open(path
, O_CREAT
|O_WRONLY
|O_NONBLOCK
, 0644);
44 if (! set_close_on_exec(fd
)) {
50 lck
= (struct flock
) {
56 ret
= fcntl(fd
, F_SETLK
, &lck
);
57 } while ((ret
== -1) && (errno
== EINTR
));
62 if ((ret
== EACCES
) || (ret
== EAGAIN
)) {
64 ret
= fcntl(fd
, F_GETLK
, &lck
);
65 } while ((ret
== -1) && (errno
== EINTR
));
72 if (lck
.l_type
== F_UNLCK
) {
74 /* Lock holder died, retry once */
78 /* Something badly wrong */
83 if (existing_pid
!= NULL
) {
84 *existing_pid
= lck
.l_pid
;
92 * PID file is locked by us so from here on we should unlink
95 len
= snprintf(tmp
, sizeof(tmp
), "%u\n", getpid());
100 if ((size_t)len
>= sizeof(tmp
)) {
106 nwritten
= write(fd
, tmp
, len
);
107 } while ((nwritten
== -1) && (errno
== EINTR
));
109 if ((nwritten
== -1) || (nwritten
!= len
)) {
115 ret
= ftruncate(fd
, len
);
116 } while ((ret
== -1) && (errno
== EINTR
));
133 void pidfile_fd_close(int fd
)
137 .l_whence
= SEEK_SET
,
142 ret
= fcntl(fd
, F_SETLK
, &lck
);
143 } while ((ret
== -1) && (errno
== EINTR
));
147 } while ((ret
== -1) && (errno
== EINTR
));
152 * return the pid in a pidfile. return 0 if the process (or pidfile)
155 pid_t
pidfile_pid(const char *piddir
, const char *name
)
157 size_t len
= strlen(piddir
) + strlen(name
) + 6;
160 char pidstr
[20] = { 0, };
163 snprintf(pidFile
, sizeof(pidFile
), "%s/%s.pid", piddir
, name
);
165 fd
= open(pidFile
, O_NONBLOCK
| O_RDONLY
, 0644);
171 if (read(fd
, pidstr
, sizeof(pidstr
)-1) <= 0) {
175 ret
= (pid_t
)atoi(pidstr
);
177 DEBUG(1, ("Could not parse contents of pidfile %s\n",
182 if (!process_exists_by_pid(ret
)) {
183 DEBUG(10, ("Process with PID=%d does not exist.\n", (int)ret
));
187 if (fcntl_lock(fd
,F_SETLK
,0,1,F_RDLCK
)) {
188 /* we could get the lock - it can't be a Samba process */
189 DEBUG(10, ("Process with PID=%d is not a Samba process.\n",
195 DEBUG(10, ("Process with PID=%d is running.\n", (int)ret
));
204 * create a pid file in the pid directory. open it and leave it locked
206 void pidfile_create(const char *piddir
, const char *name
)
208 size_t len
= strlen(piddir
) + strlen(name
) + 6;
210 pid_t pid
= (pid_t
)-1;
213 snprintf(pidFile
, sizeof(pidFile
), "%s/%s.pid", piddir
, name
);
215 ret
= pidfile_path_create(pidFile
, &fd
, &pid
);
217 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
218 name
, pidFile
, (int)pid
));
222 /* Leave pid file open & locked for the duration... */
225 void pidfile_unlink(const char *piddir
, const char *name
)
227 size_t len
= strlen(piddir
) + strlen(name
) + 6;
231 snprintf(pidFile
, sizeof(pidFile
), "%s/%s.pid", piddir
, name
);
233 ret
= unlink(pidFile
);
235 DEBUG(0,("Failed to delete pidfile %s. Error was %s\n",
236 pidFile
, strerror(errno
)));