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> |
| 26 | |
| 27 | pid_t pid_output_lock(char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | |
| 29 | pid_t |
| 30 | pid_output (char *path) |
| 31 | { |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 32 | #ifndef HAVE_FCNTL |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | FILE *fp; |
| 34 | pid_t pid; |
| 35 | |
| 36 | pid = getpid(); |
| 37 | |
| 38 | fp = fopen (path, "w"); |
| 39 | if (fp != NULL) |
| 40 | { |
| 41 | fprintf (fp, "%d\n", (int) pid); |
| 42 | fclose (fp); |
| 43 | return -1; |
| 44 | } |
| 45 | return pid; |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 46 | #else |
| 47 | return pid_output_lock(path); |
| 48 | #endif /* HAVE_FCNTL */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 49 | } |
| 50 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 51 | #ifdef HAVE_FCNTL |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | pid_t |
| 53 | pid_output_lock (char *path) |
| 54 | { |
| 55 | int tmp; |
| 56 | int fd; |
| 57 | pid_t pid; |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 58 | char buf[16]; |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 59 | struct flock lock; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | |
| 61 | pid = getpid (); |
| 62 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 63 | fd = open (path, O_RDWR | O_CREAT, 0644); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 64 | if (fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 65 | { |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 66 | zlog_err( "Can't creat pid lock file %s (%s), exit", |
| 67 | path, strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | exit (-1); |
| 69 | } |
| 70 | else |
| 71 | { |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 72 | memset (&lock, 0, sizeof(lock)); |
| 73 | |
paul | e4eaf1d | 2003-10-30 21:58:06 +0000 | [diff] [blame] | 74 | lock.l_type = F_WRLCK; |
| 75 | lock.l_whence = SEEK_END; |
| 76 | |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 77 | if (fcntl(fd, F_SETLK, &lock) < 0) |
| 78 | { |
| 79 | zlog_err("Could not lock pid_file %s, exit", path); |
| 80 | exit (-1); |
| 81 | } |
| 82 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | sprintf (buf, "%d\n", (int) pid); |
| 84 | tmp = write (fd, buf, strlen (buf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 86 | return pid; |
| 87 | } |
paul | e92fbaf | 2003-10-24 04:10:16 +0000 | [diff] [blame] | 88 | #endif /* HAVE_FCNTL */ |