paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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> |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <log.h> |
ajs | e5879ca | 2004-11-25 16:07:53 +0000 | [diff] [blame] | 26 | #include "version.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 27 | |
Denis Ovsienko | 7593fdd | 2007-10-04 15:09:19 +0000 | [diff] [blame] | 28 | #define PIDFILE_MASK 0644 |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 29 | #ifndef HAVE_FCNTL |
| 30 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | pid_t |
hasso | 6ad96ea | 2004-10-07 19:33:46 +0000 | [diff] [blame] | 32 | pid_output (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | { |
| 34 | FILE *fp; |
| 35 | pid_t pid; |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 36 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
| 38 | pid = getpid(); |
| 39 | |
Denis Ovsienko | 7593fdd | 2007-10-04 15:09:19 +0000 | [diff] [blame] | 40 | oldumask = umask(0777 & ~PIDFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | fp = fopen (path, "w"); |
| 42 | if (fp != NULL) |
| 43 | { |
| 44 | fprintf (fp, "%d\n", (int) pid); |
| 45 | fclose (fp); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 46 | umask(oldumask); |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 47 | return pid; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | } |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 49 | /* XXX Why do we continue instead of exiting? This seems incompatible |
| 50 | with the behavior of the fcntl version below. */ |
| 51 | zlog_warn("Can't fopen pid lock file %s (%s), continuing", |
| 52 | path, safe_strerror(errno)); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 53 | umask(oldumask); |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 54 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 57 | #else /* HAVE_FCNTL */ |
| 58 | |
| 59 | pid_t |
| 60 | pid_output (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 61 | { |
| 62 | int tmp; |
| 63 | int fd; |
| 64 | pid_t pid; |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 65 | char buf[16]; |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 66 | struct flock lock; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 67 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | |
| 69 | pid = getpid (); |
| 70 | |
Denis Ovsienko | 7593fdd | 2007-10-04 15:09:19 +0000 | [diff] [blame] | 71 | oldumask = umask(0777 & ~PIDFILE_MASK); |
| 72 | fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK); |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 73 | if (fd < 0) |
| 74 | { |
| 75 | zlog_err("Can't create pid lock file %s (%s), exiting", |
| 76 | path, safe_strerror(errno)); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 77 | umask(oldumask); |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 78 | exit(1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | } |
| 80 | else |
| 81 | { |
ajs | e5879ca | 2004-11-25 16:07:53 +0000 | [diff] [blame] | 82 | size_t pidsize; |
| 83 | |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 84 | umask(oldumask); |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 85 | memset (&lock, 0, sizeof(lock)); |
| 86 | |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 87 | lock.l_type = F_WRLCK; |
ajs | e5879ca | 2004-11-25 16:07:53 +0000 | [diff] [blame] | 88 | lock.l_whence = SEEK_SET; |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 89 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 90 | if (fcntl(fd, F_SETLK, &lock) < 0) |
| 91 | { |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 92 | zlog_err("Could not lock pid_file %s, exiting", path); |
| 93 | exit(1); |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | sprintf (buf, "%d\n", (int) pid); |
ajs | e5879ca | 2004-11-25 16:07:53 +0000 | [diff] [blame] | 97 | pidsize = strlen(buf); |
| 98 | if ((tmp = write (fd, buf, pidsize)) != (int)pidsize) |
| 99 | zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s", |
| 100 | (int)pid,path,tmp,safe_strerror(errno)); |
| 101 | else if (ftruncate(fd, pidsize) < 0) |
| 102 | zlog_err("Could not truncate pid_file %s to %u bytes: %s", |
| 103 | path,(u_int)pidsize,safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | return pid; |
| 106 | } |
ajs | 202d08c | 2004-12-17 20:50:00 +0000 | [diff] [blame] | 107 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 108 | #endif /* HAVE_FCNTL */ |