]>
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" |
718e3744 | 27 | |
202d08ca | 28 | #ifndef HAVE_FCNTL |
29 | ||
718e3744 | 30 | pid_t |
6ad96ea1 | 31 | pid_output (const char *path) |
718e3744 | 32 | { |
33 | FILE *fp; | |
34 | pid_t pid; | |
202d08ca | 35 | mode_t oldumask; |
718e3744 | 36 | |
37 | pid = getpid(); | |
38 | ||
aa593d5e | 39 | oldumask = umask(0777 & ~LOGFILE_MASK); |
718e3744 | 40 | fp = fopen (path, "w"); |
41 | if (fp != NULL) | |
42 | { | |
43 | fprintf (fp, "%d\n", (int) pid); | |
44 | fclose (fp); | |
aa593d5e | 45 | umask(oldumask); |
202d08ca | 46 | return pid; |
718e3744 | 47 | } |
202d08ca | 48 | /* XXX Why do we continue instead of exiting? This seems incompatible |
49 | with the behavior of the fcntl version below. */ | |
50 | zlog_warn("Can't fopen pid lock file %s (%s), continuing", | |
51 | path, safe_strerror(errno)); | |
aa593d5e | 52 | umask(oldumask); |
202d08ca | 53 | return -1; |
718e3744 | 54 | } |
55 | ||
202d08ca | 56 | #else /* HAVE_FCNTL */ |
57 | ||
58 | pid_t | |
59 | pid_output (const char *path) | |
718e3744 | 60 | { |
61 | int tmp; | |
62 | int fd; | |
63 | pid_t pid; | |
e92fbaf2 | 64 | char buf[16]; |
e4eaf1d5 | 65 | struct flock lock; |
aa593d5e | 66 | mode_t oldumask; |
718e3744 | 67 | |
68 | pid = getpid (); | |
69 | ||
aa593d5e | 70 | oldumask = umask(0777 & ~LOGFILE_MASK); |
aa593d5e | 71 | fd = open (path, O_RDWR | O_CREAT, LOGFILE_MASK); |
202d08ca | 72 | if (fd < 0) |
73 | { | |
74 | zlog_err("Can't create pid lock file %s (%s), exiting", | |
75 | path, safe_strerror(errno)); | |
aa593d5e | 76 | umask(oldumask); |
202d08ca | 77 | exit(1); |
718e3744 | 78 | } |
79 | else | |
80 | { | |
e5879ca1 | 81 | size_t pidsize; |
82 | ||
aa593d5e | 83 | umask(oldumask); |
e92fbaf2 | 84 | memset (&lock, 0, sizeof(lock)); |
85 | ||
e4eaf1d5 | 86 | lock.l_type = F_WRLCK; |
e5879ca1 | 87 | lock.l_whence = SEEK_SET; |
e4eaf1d5 | 88 | |
e92fbaf2 | 89 | if (fcntl(fd, F_SETLK, &lock) < 0) |
90 | { | |
202d08ca | 91 | zlog_err("Could not lock pid_file %s, exiting", path); |
92 | exit(1); | |
e92fbaf2 | 93 | } |
94 | ||
718e3744 | 95 | sprintf (buf, "%d\n", (int) pid); |
e5879ca1 | 96 | pidsize = strlen(buf); |
97 | if ((tmp = write (fd, buf, pidsize)) != (int)pidsize) | |
98 | zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s", | |
99 | (int)pid,path,tmp,safe_strerror(errno)); | |
100 | else if (ftruncate(fd, pidsize) < 0) | |
101 | zlog_err("Could not truncate pid_file %s to %u bytes: %s", | |
102 | path,(u_int)pidsize,safe_strerror(errno)); | |
718e3744 | 103 | } |
718e3744 | 104 | return pid; |
105 | } | |
202d08ca | 106 | |
e92fbaf2 | 107 | #endif /* HAVE_FCNTL */ |