ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / lib / pid_output.c
blob5261babc6d7d59d339fb28f4b36f09cbee442c3a
1 /*
2 * Process id output.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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.
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.
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.
23 #include <zebra.h>
24 #include <fcntl.h>
25 #include <log.h>
26 #include "version.h"
28 #define PIDFILE_MASK 0644
29 #ifndef HAVE_FCNTL
31 pid_t
32 pid_output (const char *path)
34 FILE *fp;
35 pid_t pid;
36 mode_t oldumask;
38 pid = getpid();
40 oldumask = umask(0777 & ~PIDFILE_MASK);
41 fp = fopen (path, "w");
42 if (fp != NULL)
44 fprintf (fp, "%d\n", (int) pid);
45 fclose (fp);
46 umask(oldumask);
47 return pid;
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));
53 umask(oldumask);
54 return -1;
57 #else /* HAVE_FCNTL */
59 pid_t
60 pid_output (const char *path)
62 int tmp;
63 int fd;
64 pid_t pid;
65 char buf[16];
66 struct flock lock;
67 mode_t oldumask;
69 pid = getpid ();
71 oldumask = umask(0777 & ~PIDFILE_MASK);
72 fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK);
73 if (fd < 0)
75 zlog_err("Can't create pid lock file %s (%s), exiting",
76 path, safe_strerror(errno));
77 umask(oldumask);
78 exit(1);
80 else
82 size_t pidsize;
84 umask(oldumask);
85 memset (&lock, 0, sizeof(lock));
87 lock.l_type = F_WRLCK;
88 lock.l_whence = SEEK_SET;
90 if (fcntl(fd, F_SETLK, &lock) < 0)
92 zlog_err("Could not lock pid_file %s, exiting", path);
93 exit(1);
96 sprintf (buf, "%d\n", (int) pid);
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));
105 return pid;
108 #endif /* HAVE_FCNTL */