]>
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> | |
26 | ||
27 | pid_t pid_output_lock(char *); | |
718e3744 | 28 | |
29 | pid_t | |
30 | pid_output (char *path) | |
31 | { | |
e92fbaf2 | 32 | #ifndef HAVE_FCNTL |
718e3744 | 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; | |
e92fbaf2 | 46 | #else |
47 | return pid_output_lock(path); | |
48 | #endif /* HAVE_FCNTL */ | |
718e3744 | 49 | } |
50 | ||
e92fbaf2 | 51 | #ifdef HAVE_FCNTL |
718e3744 | 52 | pid_t |
53 | pid_output_lock (char *path) | |
54 | { | |
55 | int tmp; | |
56 | int fd; | |
57 | pid_t pid; | |
e92fbaf2 | 58 | char buf[16]; |
59 | struct flock lock = { .l_type = F_WRLCK, | |
60 | .l_whence = SEEK_END }; | |
718e3744 | 61 | |
62 | pid = getpid (); | |
63 | ||
e92fbaf2 | 64 | fd = open (path, O_RDWR | O_CREAT, 0644); |
718e3744 | 65 | if (fd < 0) |
718e3744 | 66 | { |
e92fbaf2 | 67 | zlog_err( "Can't creat pid lock file %s (%s), exit", |
68 | path, strerror(errno)); | |
718e3744 | 69 | exit (-1); |
70 | } | |
71 | else | |
72 | { | |
e92fbaf2 | 73 | memset (&lock, 0, sizeof(lock)); |
74 | ||
75 | if (fcntl(fd, F_SETLK, &lock) < 0) | |
76 | { | |
77 | zlog_err("Could not lock pid_file %s, exit", path); | |
78 | exit (-1); | |
79 | } | |
80 | ||
718e3744 | 81 | sprintf (buf, "%d\n", (int) pid); |
82 | tmp = write (fd, buf, strlen (buf)); | |
718e3744 | 83 | } |
718e3744 | 84 | return pid; |
85 | } | |
e92fbaf2 | 86 | #endif /* HAVE_FCNTL */ |