]> git.proxmox.com Git - mirror_frr.git/blob - lib/pid_output.c
Make more strings const.
[mirror_frr.git] / lib / pid_output.c
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>
24 #include <fcntl.h>
25 #include <log.h>
26
27 pid_t pid_output_lock(const char *);
28
29 pid_t
30 pid_output (const char *path)
31 {
32 #ifndef HAVE_FCNTL
33 FILE *fp;
34 pid_t pid;
35 mask_t oldumask;
36
37 pid = getpid();
38
39 oldumask = umask(0777 & ~LOGFILE_MASK);
40 fp = fopen (path, "w");
41 if (fp != NULL)
42 {
43 fprintf (fp, "%d\n", (int) pid);
44 fclose (fp);
45 umask(oldumask);
46 return -1;
47 }
48 umask(oldumask);
49 return pid;
50 #else
51 return pid_output_lock(path);
52 #endif /* HAVE_FCNTL */
53 }
54
55 #ifdef HAVE_FCNTL
56 pid_t
57 pid_output_lock (const char *path)
58 {
59 int tmp;
60 int fd;
61 pid_t pid;
62 char buf[16];
63 struct flock lock;
64 mode_t oldumask;
65
66 pid = getpid ();
67
68 oldumask = umask(0777 & ~LOGFILE_MASK);
69 zlog_err( "old umask %d %d", oldumask, 0777 & ~LOGFILE_MASK);
70 fd = open (path, O_RDWR | O_CREAT, LOGFILE_MASK);
71 if (fd < 0)
72 {
73 zlog_err( "Can't creat pid lock file %s (%s), exit",
74 path, strerror(errno));
75 umask(oldumask);
76 exit (-1);
77 }
78 else
79 {
80 umask(oldumask);
81 memset (&lock, 0, sizeof(lock));
82
83 lock.l_type = F_WRLCK;
84 lock.l_whence = SEEK_END;
85
86 if (fcntl(fd, F_SETLK, &lock) < 0)
87 {
88 zlog_err("Could not lock pid_file %s, exit", path);
89 exit (-1);
90 }
91
92 sprintf (buf, "%d\n", (int) pid);
93 tmp = write (fd, buf, strlen (buf));
94 }
95 return pid;
96 }
97 #endif /* HAVE_FCNTL */