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
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
31 pid_output (const char *path
)
39 oldumask
= umask(0777 & ~LOGFILE_MASK
);
40 fp
= fopen (path
, "w");
43 fprintf (fp
, "%d\n", (int) pid
);
48 /* 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
));
56 #else /* HAVE_FCNTL */
59 pid_output (const char *path
)
70 oldumask
= umask(0777 & ~LOGFILE_MASK
);
71 fd
= open (path
, O_RDWR
| O_CREAT
, LOGFILE_MASK
);
74 zlog_err("Can't create pid lock file %s (%s), exiting",
75 path
, safe_strerror(errno
));
84 memset (&lock
, 0, sizeof(lock
));
86 lock
.l_type
= F_WRLCK
;
87 lock
.l_whence
= SEEK_SET
;
89 if (fcntl(fd
, F_SETLK
, &lock
) < 0)
91 zlog_err("Could not lock pid_file %s, exiting", path
);
95 sprintf (buf
, "%d\n", (int) pid
);
96 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
));
107 #endif /* HAVE_FCNTL */