blob: 5261babc6d7d59d339fb28f4b36f09cbee442c3a [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
Denis Ovsienko7593fdd2007-10-04 15:09:19 +000028#define PIDFILE_MASK 0644
ajs202d08c2004-12-17 20:50:00 +000029#ifndef HAVE_FCNTL
30
paul718e3742002-12-13 20:15:29 +000031pid_t
hasso6ad96ea2004-10-07 19:33:46 +000032pid_output (const char *path)
paul718e3742002-12-13 20:15:29 +000033{
34 FILE *fp;
35 pid_t pid;
ajs202d08c2004-12-17 20:50:00 +000036 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +000037
38 pid = getpid();
39
Denis Ovsienko7593fdd2007-10-04 15:09:19 +000040 oldumask = umask(0777 & ~PIDFILE_MASK);
paul718e3742002-12-13 20:15:29 +000041 fp = fopen (path, "w");
42 if (fp != NULL)
43 {
44 fprintf (fp, "%d\n", (int) pid);
45 fclose (fp);
gdtaa593d52003-12-22 20:15:53 +000046 umask(oldumask);
ajs202d08c2004-12-17 20:50:00 +000047 return pid;
paul718e3742002-12-13 20:15:29 +000048 }
ajs202d08c2004-12-17 20:50:00 +000049 /* 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));
gdtaa593d52003-12-22 20:15:53 +000053 umask(oldumask);
ajs202d08c2004-12-17 20:50:00 +000054 return -1;
paul718e3742002-12-13 20:15:29 +000055}
56
ajs202d08c2004-12-17 20:50:00 +000057#else /* HAVE_FCNTL */
58
59pid_t
60pid_output (const char *path)
paul718e3742002-12-13 20:15:29 +000061{
62 int tmp;
63 int fd;
64 pid_t pid;
paule92fbaf2003-10-24 04:10:16 +000065 char buf[16];
paule4eaf1d2003-10-30 21:58:06 +000066 struct flock lock;
gdtaa593d52003-12-22 20:15:53 +000067 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +000068
69 pid = getpid ();
70
Denis Ovsienko7593fdd2007-10-04 15:09:19 +000071 oldumask = umask(0777 & ~PIDFILE_MASK);
72 fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK);
ajs202d08c2004-12-17 20:50:00 +000073 if (fd < 0)
74 {
75 zlog_err("Can't create pid lock file %s (%s), exiting",
76 path, safe_strerror(errno));
gdtaa593d52003-12-22 20:15:53 +000077 umask(oldumask);
ajs202d08c2004-12-17 20:50:00 +000078 exit(1);
paul718e3742002-12-13 20:15:29 +000079 }
80 else
81 {
ajse5879ca2004-11-25 16:07:53 +000082 size_t pidsize;
83
gdtaa593d52003-12-22 20:15:53 +000084 umask(oldumask);
paule92fbaf2003-10-24 04:10:16 +000085 memset (&lock, 0, sizeof(lock));
86
paule4eaf1d2003-10-30 21:58:06 +000087 lock.l_type = F_WRLCK;
ajse5879ca2004-11-25 16:07:53 +000088 lock.l_whence = SEEK_SET;
paule4eaf1d2003-10-30 21:58:06 +000089
paule92fbaf2003-10-24 04:10:16 +000090 if (fcntl(fd, F_SETLK, &lock) < 0)
91 {
ajs202d08c2004-12-17 20:50:00 +000092 zlog_err("Could not lock pid_file %s, exiting", path);
93 exit(1);
paule92fbaf2003-10-24 04:10:16 +000094 }
95
paul718e3742002-12-13 20:15:29 +000096 sprintf (buf, "%d\n", (int) pid);
ajse5879ca2004-11-25 16:07:53 +000097 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));
paul718e3742002-12-13 20:15:29 +0000104 }
paul718e3742002-12-13 20:15:29 +0000105 return pid;
106}
ajs202d08c2004-12-17 20:50:00 +0000107
paule92fbaf2003-10-24 04:10:16 +0000108#endif /* HAVE_FCNTL */