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