]> git.proxmox.com Git - mirror_frr.git/blame - lib/pid_output.c
debian: add pkg-config to build-depends
[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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac4d0be5 20 * 02111-1307, USA.
718e3744 21 */
22
23#include <zebra.h>
e92fbaf2 24#include <fcntl.h>
25#include <log.h>
e5879ca1 26#include "version.h"
2da59394 27#include "network.h"
718e3744 28
7593fddf 29#define PIDFILE_MASK 0644
202d08ca 30
ac4d0be5 31pid_t pid_output(const char *path)
718e3744 32{
ac4d0be5 33 int tmp;
34 int fd;
35 pid_t pid;
36 char buf[16];
37 struct flock lock;
38 mode_t oldumask;
718e3744 39
ac4d0be5 40 pid = getpid();
718e3744 41
ac4d0be5 42 oldumask = umask(0777 & ~PIDFILE_MASK);
43 fd = open(path, O_RDWR | O_CREAT, PIDFILE_MASK);
44 if (fd < 0) {
45 zlog_err("Can't create pid lock file %s (%s), exiting", path,
46 safe_strerror(errno));
47 umask(oldumask);
48 exit(1);
49 } else {
50 size_t pidsize;
e5879ca1 51
ac4d0be5 52 umask(oldumask);
53 memset(&lock, 0, sizeof(lock));
e92fbaf2 54
ac4d0be5 55 set_cloexec(fd);
2da59394 56
ac4d0be5 57 lock.l_type = F_WRLCK;
58 lock.l_whence = SEEK_SET;
e4eaf1d5 59
ac4d0be5 60 if (fcntl(fd, F_SETLK, &lock) < 0) {
61 zlog_err("Could not lock pid_file %s, exiting", path);
62 exit(1);
63 }
e92fbaf2 64
ac4d0be5 65 sprintf(buf, "%d\n", (int)pid);
66 pidsize = strlen(buf);
67 if ((tmp = write(fd, buf, pidsize)) != (int)pidsize)
68 zlog_err(
69 "Could not write pid %d to pid_file %s, rc was %d: %s",
70 (int)pid, path, tmp, safe_strerror(errno));
71 else if (ftruncate(fd, pidsize) < 0)
72 zlog_err(
73 "Could not truncate pid_file %s to %u bytes: %s",
74 path, (u_int)pidsize, safe_strerror(errno));
75 }
76 return pid;
718e3744 77}