]>
Commit | Line | Data |
---|---|---|
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" |
718e3744 | 27 | |
7593fddf | 28 | #define PIDFILE_MASK 0644 |
202d08ca | 29 | |
30 | pid_t | |
31 | pid_output (const char *path) | |
718e3744 | 32 | { |
33 | int tmp; | |
34 | int fd; | |
35 | pid_t pid; | |
e92fbaf2 | 36 | char buf[16]; |
e4eaf1d5 | 37 | struct flock lock; |
aa593d5e | 38 | mode_t oldumask; |
718e3744 | 39 | |
40 | pid = getpid (); | |
41 | ||
7593fddf DO |
42 | oldumask = umask(0777 & ~PIDFILE_MASK); |
43 | fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK); | |
202d08ca | 44 | if (fd < 0) |
45 | { | |
46 | zlog_err("Can't create pid lock file %s (%s), exiting", | |
47 | path, safe_strerror(errno)); | |
aa593d5e | 48 | umask(oldumask); |
202d08ca | 49 | exit(1); |
718e3744 | 50 | } |
51 | else | |
52 | { | |
e5879ca1 | 53 | size_t pidsize; |
54 | ||
aa593d5e | 55 | umask(oldumask); |
e92fbaf2 | 56 | memset (&lock, 0, sizeof(lock)); |
57 | ||
2da59394 DL |
58 | set_cloexec(fd); |
59 | ||
e4eaf1d5 | 60 | lock.l_type = F_WRLCK; |
e5879ca1 | 61 | lock.l_whence = SEEK_SET; |
e4eaf1d5 | 62 | |
e92fbaf2 | 63 | if (fcntl(fd, F_SETLK, &lock) < 0) |
64 | { | |
202d08ca | 65 | zlog_err("Could not lock pid_file %s, exiting", path); |
66 | exit(1); | |
e92fbaf2 | 67 | } |
68 | ||
718e3744 | 69 | sprintf (buf, "%d\n", (int) pid); |
e5879ca1 | 70 | pidsize = strlen(buf); |
71 | if ((tmp = write (fd, buf, pidsize)) != (int)pidsize) | |
72 | zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s", | |
73 | (int)pid,path,tmp,safe_strerror(errno)); | |
74 | else if (ftruncate(fd, pidsize) < 0) | |
75 | zlog_err("Could not truncate pid_file %s to %u bytes: %s", | |
76 | path,(u_int)pidsize,safe_strerror(errno)); | |
718e3744 | 77 | } |
718e3744 | 78 | return pid; |
79 | } |