blob: 11e1243d9ac535fc30778b4e58e8d35a6f348188 [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
28pid_t
hasso6ad96ea2004-10-07 19:33:46 +000029pid_output (const char *path)
paul718e3742002-12-13 20:15:29 +000030{
paule92fbaf2003-10-24 04:10:16 +000031#ifndef HAVE_FCNTL
paul718e3742002-12-13 20:15:29 +000032 FILE *fp;
33 pid_t pid;
gdtaa593d52003-12-22 20:15:53 +000034 mask_t oldumask;
paul718e3742002-12-13 20:15:29 +000035
36 pid = getpid();
37
gdtaa593d52003-12-22 20:15:53 +000038 oldumask = umask(0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +000039 fp = fopen (path, "w");
40 if (fp != NULL)
41 {
42 fprintf (fp, "%d\n", (int) pid);
43 fclose (fp);
gdtaa593d52003-12-22 20:15:53 +000044 umask(oldumask);
paul718e3742002-12-13 20:15:29 +000045 return -1;
46 }
gdtaa593d52003-12-22 20:15:53 +000047 umask(oldumask);
paul718e3742002-12-13 20:15:29 +000048 return pid;
paule92fbaf2003-10-24 04:10:16 +000049#else
ajs887c44a2004-12-03 16:36:46 +000050 static pid_t pid_output_lock (const char *);
51
paule92fbaf2003-10-24 04:10:16 +000052 return pid_output_lock(path);
53#endif /* HAVE_FCNTL */
paul718e3742002-12-13 20:15:29 +000054}
55
paule92fbaf2003-10-24 04:10:16 +000056#ifdef HAVE_FCNTL
ajs887c44a2004-12-03 16:36:46 +000057static pid_t
hasso6ad96ea2004-10-07 19:33:46 +000058pid_output_lock (const char *path)
paul718e3742002-12-13 20:15:29 +000059{
60 int tmp;
61 int fd;
62 pid_t pid;
paule92fbaf2003-10-24 04:10:16 +000063 char buf[16];
paule4eaf1d2003-10-30 21:58:06 +000064 struct flock lock;
gdtaa593d52003-12-22 20:15:53 +000065 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +000066
67 pid = getpid ();
68
gdtaa593d52003-12-22 20:15:53 +000069 oldumask = umask(0777 & ~LOGFILE_MASK);
gdtaa593d52003-12-22 20:15:53 +000070 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 {
ajse5879ca2004-11-25 16:07:53 +000080 size_t pidsize;
81
gdtaa593d52003-12-22 20:15:53 +000082 umask(oldumask);
paule92fbaf2003-10-24 04:10:16 +000083 memset (&lock, 0, sizeof(lock));
84
paule4eaf1d2003-10-30 21:58:06 +000085 lock.l_type = F_WRLCK;
ajse5879ca2004-11-25 16:07:53 +000086 lock.l_whence = SEEK_SET;
paule4eaf1d2003-10-30 21:58:06 +000087
paule92fbaf2003-10-24 04:10:16 +000088 if (fcntl(fd, F_SETLK, &lock) < 0)
89 {
90 zlog_err("Could not lock pid_file %s, exit", path);
91 exit (-1);
92 }
93
paul718e3742002-12-13 20:15:29 +000094 sprintf (buf, "%d\n", (int) pid);
ajse5879ca2004-11-25 16:07:53 +000095 pidsize = strlen(buf);
96 if ((tmp = write (fd, buf, pidsize)) != (int)pidsize)
97 zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s",
98 (int)pid,path,tmp,safe_strerror(errno));
99 else if (ftruncate(fd, pidsize) < 0)
100 zlog_err("Could not truncate pid_file %s to %u bytes: %s",
101 path,(u_int)pidsize,safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000102 }
paul718e3742002-12-13 20:15:29 +0000103 return pid;
104}
paule92fbaf2003-10-24 04:10:16 +0000105#endif /* HAVE_FCNTL */