blob: 2782b9d43a4cfed407170c399c19976e39dbbfe0 [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>
26
hasso6ad96ea2004-10-07 19:33:46 +000027pid_t pid_output_lock(const char *);
paul718e3742002-12-13 20:15:29 +000028
29pid_t
hasso6ad96ea2004-10-07 19:33:46 +000030pid_output (const char *path)
paul718e3742002-12-13 20:15:29 +000031{
paule92fbaf2003-10-24 04:10:16 +000032#ifndef HAVE_FCNTL
paul718e3742002-12-13 20:15:29 +000033 FILE *fp;
34 pid_t pid;
gdtaa593d52003-12-22 20:15:53 +000035 mask_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);
paul718e3742002-12-13 20:15:29 +000046 return -1;
47 }
gdtaa593d52003-12-22 20:15:53 +000048 umask(oldumask);
paul718e3742002-12-13 20:15:29 +000049 return pid;
paule92fbaf2003-10-24 04:10:16 +000050#else
51 return pid_output_lock(path);
52#endif /* HAVE_FCNTL */
paul718e3742002-12-13 20:15:29 +000053}
54
paule92fbaf2003-10-24 04:10:16 +000055#ifdef HAVE_FCNTL
paul718e3742002-12-13 20:15:29 +000056pid_t
hasso6ad96ea2004-10-07 19:33:46 +000057pid_output_lock (const char *path)
paul718e3742002-12-13 20:15:29 +000058{
59 int tmp;
60 int fd;
61 pid_t pid;
paule92fbaf2003-10-24 04:10:16 +000062 char buf[16];
paule4eaf1d2003-10-30 21:58:06 +000063 struct flock lock;
gdtaa593d52003-12-22 20:15:53 +000064 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +000065
66 pid = getpid ();
67
gdtaa593d52003-12-22 20:15:53 +000068 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);
paul718e3742002-12-13 20:15:29 +000071 if (fd < 0)
paul718e3742002-12-13 20:15:29 +000072 {
paule92fbaf2003-10-24 04:10:16 +000073 zlog_err( "Can't creat pid lock file %s (%s), exit",
ajs6099b3b2004-11-20 02:06:59 +000074 path, safe_strerror(errno));
gdtaa593d52003-12-22 20:15:53 +000075 umask(oldumask);
paul718e3742002-12-13 20:15:29 +000076 exit (-1);
77 }
78 else
79 {
gdtaa593d52003-12-22 20:15:53 +000080 umask(oldumask);
paule92fbaf2003-10-24 04:10:16 +000081 memset (&lock, 0, sizeof(lock));
82
paule4eaf1d2003-10-30 21:58:06 +000083 lock.l_type = F_WRLCK;
84 lock.l_whence = SEEK_END;
85
paule92fbaf2003-10-24 04:10:16 +000086 if (fcntl(fd, F_SETLK, &lock) < 0)
87 {
88 zlog_err("Could not lock pid_file %s, exit", path);
89 exit (-1);
90 }
91
paul718e3742002-12-13 20:15:29 +000092 sprintf (buf, "%d\n", (int) pid);
93 tmp = write (fd, buf, strlen (buf));
paul718e3742002-12-13 20:15:29 +000094 }
paul718e3742002-12-13 20:15:29 +000095 return pid;
96}
paule92fbaf2003-10-24 04:10:16 +000097#endif /* HAVE_FCNTL */