]>
git.proxmox.com Git - mirror_frr.git/blob - lib/pid_output.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
10 #include "lib/version.h"
12 #include "lib_errors.h"
14 #define PIDFILE_MASK 0644
16 pid_t
pid_output(const char *path
)
27 oldumask
= umask(0777 & ~PIDFILE_MASK
);
28 fd
= open(path
, O_RDWR
| O_CREAT
, PIDFILE_MASK
);
30 flog_err_sys(EC_LIB_SYSTEM_CALL
,
31 "Can't create pid lock file %s (%s), exiting",
32 path
, safe_strerror(errno
));
39 memset(&lock
, 0, sizeof(lock
));
43 lock
.l_type
= F_WRLCK
;
44 lock
.l_whence
= SEEK_SET
;
46 if (fcntl(fd
, F_SETLK
, &lock
) < 0) {
47 flog_err_sys(EC_LIB_SYSTEM_CALL
,
48 "Could not lock pid_file %s (%s), exiting. Please ensure that the daemon is not already running",
49 path
, safe_strerror(errno
));
53 snprintf(buf
, sizeof(buf
), "%d\n", (int)pid
);
54 pidsize
= strlen(buf
);
55 if ((tmp
= write(fd
, buf
, pidsize
)) != (int)pidsize
)
58 "Could not write pid %d to pid_file %s, rc was %d: %s",
59 (int)pid
, path
, tmp
, safe_strerror(errno
));
60 else if (ftruncate(fd
, pidsize
) < 0)
63 "Could not truncate pid_file %s to %u bytes: %s",
64 path
, (unsigned int)pidsize
,
65 safe_strerror(errno
));