blob: a098c63ca8ad93950f47a9419f0d55546bb5de1e [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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>
paule92fbaf2003-10-24 04:10:16 +000024#include <fcntl.h>
25#include <log.h>
ajse5879ca2004-11-25 16:07:53 +000026#include "version.h"
paul718e3742002-12-13 20:15:29 +000027
ajs202d08c2004-12-17 20:50:00 +000028#ifndef HAVE_FCNTL
29
paul718e3742002-12-13 20:15:29 +000030pid_t
hasso6ad96ea2004-10-07 19:33:46 +000031pid_output (const char *path)
paul718e3742002-12-13 20:15:29 +000032{
33 FILE *fp;
34 pid_t pid;
ajs202d08c2004-12-17 20:50:00 +000035 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +000036
37 pid = getpid();
38
gdtaa593d52003-12-22 20:15:53 +000039 oldumask = umask(0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +000040 fp = fopen (path, "w");
41 if (fp != NULL)
42 {
43 fprintf (fp, "%d\n", (int) pid);
44 fclose (fp);
gdtaa593d52003-12-22 20:15:53 +000045 umask(oldumask);
ajs202d08c2004-12-17 20:50:00 +000046 return pid;
paul718e3742002-12-13 20:15:29 +000047 }
ajs202d08c2004-12-17 20:50:00 +000048 /* XXX Why do we continue instead of exiting? This seems incompatible
49 with the behavior of the fcntl version below. */
50 zlog_warn("Can't fopen pid lock file %s (%s), continuing",
51 path, safe_strerror(errno));
gdtaa593d52003-12-22 20:15:53 +000052 umask(oldumask);
ajs202d08c2004-12-17 20:50:00 +000053 return -1;
paul718e3742002-12-13 20:15:29 +000054}
55
ajs202d08c2004-12-17 20:50:00 +000056#else /* HAVE_FCNTL */
57
58pid_t
59pid_output (const char *path)
paul718e3742002-12-13 20:15:29 +000060{
61 int tmp;
62 int fd;
63 pid_t pid;
paule92fbaf2003-10-24 04:10:16 +000064 char buf[16];
paule4eaf1d2003-10-30 21:58:06 +000065 struct flock lock;
gdtaa593d52003-12-22 20:15:53 +000066 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +000067
68 pid = getpid ();
69
gdtaa593d52003-12-22 20:15:53 +000070 oldumask = umask(0777 & ~LOGFILE_MASK);
gdtaa593d52003-12-22 20:15:53 +000071 fd = open (path, O_RDWR | O_CREAT, LOGFILE_MASK);
ajs202d08c2004-12-17 20:50:00 +000072 if (fd < 0)
73 {
74 zlog_err("Can't create pid lock file %s (%s), exiting",
75 path, safe_strerror(errno));
gdtaa593d52003-12-22 20:15:53 +000076 umask(oldumask);
ajs202d08c2004-12-17 20:50:00 +000077 exit(1);
paul718e3742002-12-13 20:15:29 +000078 }
79 else
80 {
ajse5879ca2004-11-25 16:07:53 +000081 size_t pidsize;
82
gdtaa593d52003-12-22 20:15:53 +000083 umask(oldumask);
paule92fbaf2003-10-24 04:10:16 +000084 memset (&lock, 0, sizeof(lock));
85
paule4eaf1d2003-10-30 21:58:06 +000086 lock.l_type = F_WRLCK;
ajse5879ca2004-11-25 16:07:53 +000087 lock.l_whence = SEEK_SET;
paule4eaf1d2003-10-30 21:58:06 +000088
paule92fbaf2003-10-24 04:10:16 +000089 if (fcntl(fd, F_SETLK, &lock) < 0)
90 {
ajs202d08c2004-12-17 20:50:00 +000091 zlog_err("Could not lock pid_file %s, exiting", path);
92 exit(1);
paule92fbaf2003-10-24 04:10:16 +000093 }
94
paul718e3742002-12-13 20:15:29 +000095 sprintf (buf, "%d\n", (int) pid);
ajse5879ca2004-11-25 16:07:53 +000096 pidsize = strlen(buf);
97 if ((tmp = write (fd, buf, pidsize)) != (int)pidsize)
98 zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s",
99 (int)pid,path,tmp,safe_strerror(errno));
100 else if (ftruncate(fd, pidsize) < 0)
101 zlog_err("Could not truncate pid_file %s to %u bytes: %s",
102 path,(u_int)pidsize,safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000103 }
paul718e3742002-12-13 20:15:29 +0000104 return pid;
105}
ajs202d08c2004-12-17 20:50:00 +0000106
paule92fbaf2003-10-24 04:10:16 +0000107#endif /* HAVE_FCNTL */