MCPcopy Create free account
hub / github.com/F-Stack/f-stack / pidfile_write

Function pidfile_write

tools/libutil/pidfile.c:221–259  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

219}
220
221int
222pidfile_write(struct pidfh *pfh)
223{
224 char pidstr[16];
225 int error, fd;
226
227 /*
228 * Check remembered descriptor, so we don't overwrite some other
229 * file if pidfile was closed and descriptor reused.
230 */
231 errno = pidfile_verify(pfh);
232 if (errno != 0) {
233 /*
234 * Don't close descriptor, because we are not sure if it's ours.
235 */
236 return (-1);
237 }
238 fd = pfh->pf_fd;
239
240 /*
241 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
242 */
243 if (ftruncate(fd, 0) == -1) {
244 error = errno;
245 _pidfile_remove(pfh, 0);
246 errno = error;
247 return (-1);
248 }
249
250 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
251 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
252 error = errno;
253 _pidfile_remove(pfh, 0);
254 errno = error;
255 return (-1);
256 }
257
258 return (0);
259}
260
261int
262pidfile_close(struct pidfh *pfh)

Callers 4

test_pidfile_uncontestedFunction · 0.85
test_pidfile_selfFunction · 0.85
test_pidfile_relativeFunction · 0.85

Calls 3

pidfile_verifyFunction · 0.85
_pidfile_removeFunction · 0.85
snprintfFunction · 0.85

Tested by 4

test_pidfile_uncontestedFunction · 0.68
test_pidfile_selfFunction · 0.68
test_pidfile_relativeFunction · 0.68