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 | |
| 28 | pid_t |
hasso | 6ad96ea | 2004-10-07 19:33:46 +0000 | [diff] [blame] | 29 | pid_output (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | { |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 31 | #ifndef HAVE_FCNTL |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 32 | FILE *fp; |
| 33 | pid_t pid; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 34 | mask_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | pid = getpid(); |
| 37 | |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 38 | oldumask = umask(0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 39 | fp = fopen (path, "w"); |
| 40 | if (fp != NULL) |
| 41 | { |
| 42 | fprintf (fp, "%d\n", (int) pid); |
| 43 | fclose (fp); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 44 | umask(oldumask); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | return -1; |
| 46 | } |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 47 | umask(oldumask); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | return pid; |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 49 | #else |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 50 | static pid_t pid_output_lock (const char *); |
| 51 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 52 | return pid_output_lock(path); |
| 53 | #endif /* HAVE_FCNTL */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | } |
| 55 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 56 | #ifdef HAVE_FCNTL |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 57 | static pid_t |
hasso | 6ad96ea | 2004-10-07 19:33:46 +0000 | [diff] [blame] | 58 | pid_output_lock (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 59 | { |
| 60 | int tmp; |
| 61 | int fd; |
| 62 | pid_t pid; |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 63 | char buf[16]; |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 64 | struct flock lock; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 65 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | |
| 67 | pid = getpid (); |
| 68 | |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 69 | oldumask = umask(0777 & ~LOGFILE_MASK); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 70 | fd = open (path, O_RDWR | O_CREAT, LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | if (fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | { |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 73 | zlog_err( "Can't creat pid lock file %s (%s), exit", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 74 | path, safe_strerror(errno)); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 75 | umask(oldumask); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | exit (-1); |
| 77 | } |
| 78 | else |
| 79 | { |
ajs | e5879ca | 2004-11-25 16:07:53 +0000 | [diff] [blame] | 80 | size_t pidsize; |
| 81 | |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 82 | umask(oldumask); |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 83 | memset (&lock, 0, sizeof(lock)); |
| 84 | |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 85 | lock.l_type = F_WRLCK; |
ajs | e5879ca | 2004-11-25 16:07:53 +0000 | [diff] [blame] | 86 | lock.l_whence = SEEK_SET; |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 87 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 88 | if (fcntl(fd, F_SETLK, &lock) < 0) |
| 89 | { |
| 90 | zlog_err("Could not lock pid_file %s, exit", path); |
| 91 | exit (-1); |
| 92 | } |
| 93 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | sprintf (buf, "%d\n", (int) pid); |
ajs | e5879ca | 2004-11-25 16:07:53 +0000 | [diff] [blame] | 95 | pidsize = strlen(buf); |
| 96 | if ((tmp = write (fd, buf, pidsize)) != (int)pidsize) |
| 97 | zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s", |
| 98 | (int)pid,path,tmp,safe_strerror(errno)); |
| 99 | else if (ftruncate(fd, pidsize) < 0) |
| 100 | zlog_err("Could not truncate pid_file %s to %u bytes: %s", |
| 101 | path,(u_int)pidsize,safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | return pid; |
| 104 | } |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 105 | #endif /* HAVE_FCNTL */ |