s3/service: use lp_const_servicename() where possible
[Samba.git] / lib / util / pidfile.c
blob5cd09cee75ce5d73f4dab7bba0a5f460a5eb1c86
1 /*
2 Unix SMB/CIFS implementation.
3 pidfile handling
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/>.
21 #include "replace.h"
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)
32 struct flock lck;
33 char tmp[64] = { 0 };
34 pid_t pid;
35 int fd, ret = 0;
36 int len;
37 ssize_t nwritten;
39 pid = getpid();
41 fd = open(path, O_CREAT|O_WRONLY|O_NONBLOCK, 0644);
42 if (fd == -1) {
43 return errno;
46 if (! set_close_on_exec(fd)) {
47 close(fd);
48 return EIO;
51 lck = (struct flock) {
52 .l_type = F_WRLCK,
53 .l_whence = SEEK_SET,
56 do {
57 ret = fcntl(fd, F_SETLK, &lck);
58 } while ((ret == -1) && (errno == EINTR));
60 if (ret != 0) {
61 ret = errno;
62 close(fd);
63 return ret;
67 * PID file is locked by us so from here on we should unlink
68 * on failure
71 do {
72 ret = ftruncate(fd, 0);
73 } while ((ret == -1) && (errno == EINTR));
75 if (ret == -1) {
76 ret = EIO;
77 goto fail_unlink;
80 len = snprintf(tmp, sizeof(tmp), "%u\n", pid);
81 if (len < 0) {
82 ret = errno;
83 goto fail_unlink;
85 if (len >= sizeof(tmp)) {
86 ret = ENOSPC;
87 goto fail_unlink;
90 do {
91 nwritten = write(fd, tmp, len);
92 } while ((nwritten == -1) && (errno == EINTR));
94 if ((nwritten == -1) || (nwritten != len)) {
95 ret = EIO;
96 goto fail_unlink;
99 if (outfd != NULL) {
100 *outfd = fd;
102 return 0;
104 fail_unlink:
105 unlink(path);
106 close(fd);
107 return ret;
110 void pidfile_fd_close(int fd)
112 struct flock lck = {
113 .l_type = F_UNLCK,
114 .l_whence = SEEK_SET,
116 int ret;
118 do {
119 ret = fcntl(fd, F_SETLK, &lck);
120 } while ((ret == -1) && (errno == EINTR));
122 do {
123 ret = close(fd);
124 } while ((ret == -1) && (errno == EINTR));
129 * return the pid in a pidfile. return 0 if the process (or pidfile)
130 * does not exist
132 pid_t pidfile_pid(const char *piddir, const char *name)
134 size_t len = strlen(piddir) + strlen(name) + 6;
135 char pidFile[len];
136 int fd;
137 char pidstr[20];
138 pid_t ret = -1;
140 snprintf(pidFile, sizeof(pidFile), "%s/%s.pid", piddir, name);
142 fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
144 if (fd == -1) {
145 return 0;
148 ZERO_STRUCT(pidstr);
150 if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
151 goto noproc;
154 ret = (pid_t)atoi(pidstr);
155 if (ret <= 0) {
156 DEBUG(1, ("Could not parse contents of pidfile %s\n",
157 pidFile));
158 goto noproc;
161 if (!process_exists_by_pid(ret)) {
162 DEBUG(10, ("Process with PID=%d does not exist.\n", (int)ret));
163 goto noproc;
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",
169 (int)ret));
170 goto noproc;
173 close(fd);
174 DEBUG(10, ("Process with PID=%d is running.\n", (int)ret));
175 return ret;
177 noproc:
178 close(fd);
179 return 0;
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;
188 char pidFile[len];
189 pid_t pid;
190 int ret;
192 snprintf(pidFile, sizeof(pidFile), "%s/%s.pid", piddir, name);
194 pid = pidfile_pid(piddir, name);
195 if (pid != 0) {
196 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
197 name, pidFile, (int)pid));
198 exit(1);
201 ret = pidfile_path_create(pidFile, NULL);
202 if (ret != 0) {
203 DBG_ERR("ERROR: Failed to create PID file %s (%s)\n",
204 pidFile, strerror(ret));
205 exit(1);
208 /* Leave pid file open & locked for the duration... */
211 void pidfile_unlink(const char *piddir, const char *name)
213 int ret;
214 char *pidFile = NULL;
216 if (asprintf(&pidFile, "%s/%s.pid", piddir, name) < 0) {
217 DEBUG(0,("ERROR: Out of memory\n"));
218 exit(1);
220 ret = unlink(pidFile);
221 if (ret == -1) {
222 DEBUG(0,("Failed to delete pidfile %s. Error was %s\n",
223 pidFile, strerror(errno)));