]> git.proxmox.com Git - mirror_frr.git/blame - lib/pid_output.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / pid_output.c
CommitLineData
718e3744 1/*
2 * Process id output.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
e92fbaf2 23#include <fcntl.h>
24#include <log.h>
e5879ca1 25#include "version.h"
2da59394 26#include "network.h"
481bc15f 27#include "lib_errors.h"
718e3744 28
7593fddf 29#define PIDFILE_MASK 0644
202d08ca 30
d62a17ae 31pid_t pid_output(const char *path)
718e3744 32{
d62a17ae 33 int tmp;
34 int fd;
35 pid_t pid;
36 char buf[16];
37 struct flock lock;
38 mode_t oldumask;
718e3744 39
d62a17ae 40 pid = getpid();
718e3744 41
d62a17ae 42 oldumask = umask(0777 & ~PIDFILE_MASK);
43 fd = open(path, O_RDWR | O_CREAT, PIDFILE_MASK);
44 if (fd < 0) {
450971aa 45 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3
QY
46 "Can't create pid lock file %s (%s), exiting",
47 path, safe_strerror(errno));
d62a17ae 48 umask(oldumask);
49 exit(1);
50 } else {
51 size_t pidsize;
e5879ca1 52
d62a17ae 53 umask(oldumask);
54 memset(&lock, 0, sizeof(lock));
e92fbaf2 55
d62a17ae 56 set_cloexec(fd);
2da59394 57
d62a17ae 58 lock.l_type = F_WRLCK;
59 lock.l_whence = SEEK_SET;
e4eaf1d5 60
d62a17ae 61 if (fcntl(fd, F_SETLK, &lock) < 0) {
450971aa 62 flog_err_sys(EC_LIB_SYSTEM_CALL,
8c5ff531 63 "Could not lock pid_file %s (%s), exiting. Please ensure that the daemon is not already running",
09c866e3 64 path, safe_strerror(errno));
d62a17ae 65 exit(1);
66 }
e92fbaf2 67
d62a17ae 68 sprintf(buf, "%d\n", (int)pid);
69 pidsize = strlen(buf);
70 if ((tmp = write(fd, buf, pidsize)) != (int)pidsize)
09c866e3 71 flog_err_sys(
450971aa 72 EC_LIB_SYSTEM_CALL,
09c866e3
QY
73 "Could not write pid %d to pid_file %s, rc was %d: %s",
74 (int)pid, path, tmp, safe_strerror(errno));
d62a17ae 75 else if (ftruncate(fd, pidsize) < 0)
09c866e3 76 flog_err_sys(
450971aa 77 EC_LIB_SYSTEM_CALL,
09c866e3
QY
78 "Could not truncate pid_file %s to %u bytes: %s",
79 path, (unsigned int)pidsize,
80 safe_strerror(errno));
d62a17ae 81 }
82 return pid;
718e3744 83}