back to topotato report
topotato coverage report
Current view: top level - lib - pid_output.c (source / functions) Hit Total Coverage
Test: test_pim_cbsr.py::PIMCandidateBSRTest Lines: 23 30 76.7 %
Date: 2023-02-16 02:09:14 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       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 along
      18             :  * with this program; see the file COPYING; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include <zebra.h>
      23             : #include <fcntl.h>
      24             : #include <log.h>
      25             : #include "lib/version.h"
      26             : #include "network.h"
      27             : #include "lib_errors.h"
      28             : 
      29             : #define PIDFILE_MASK 0644
      30             : 
      31           9 : pid_t pid_output(const char *path)
      32             : {
      33           9 :         int tmp;
      34           9 :         int fd;
      35           9 :         pid_t pid;
      36           9 :         char buf[16];
      37           9 :         struct flock lock;
      38           9 :         mode_t oldumask;
      39             : 
      40           9 :         pid = getpid();
      41             : 
      42           9 :         oldumask = umask(0777 & ~PIDFILE_MASK);
      43           9 :         fd = open(path, O_RDWR | O_CREAT, PIDFILE_MASK);
      44           9 :         if (fd < 0) {
      45           0 :                 flog_err_sys(EC_LIB_SYSTEM_CALL,
      46             :                              "Can't create pid lock file %s (%s), exiting",
      47             :                              path, safe_strerror(errno));
      48           0 :                 umask(oldumask);
      49           0 :                 exit(1);
      50             :         } else {
      51           9 :                 size_t pidsize;
      52             : 
      53           9 :                 umask(oldumask);
      54           9 :                 memset(&lock, 0, sizeof(lock));
      55             : 
      56           9 :                 set_cloexec(fd);
      57             : 
      58           9 :                 lock.l_type = F_WRLCK;
      59           9 :                 lock.l_whence = SEEK_SET;
      60             : 
      61           9 :                 if (fcntl(fd, F_SETLK, &lock) < 0) {
      62           0 :                         flog_err_sys(EC_LIB_SYSTEM_CALL,
      63             :                                      "Could not lock pid_file %s (%s), exiting.  Please ensure that the daemon is not already running",
      64             :                                      path, safe_strerror(errno));
      65           0 :                         exit(1);
      66             :                 }
      67             : 
      68           9 :                 snprintf(buf, sizeof(buf), "%d\n", (int)pid);
      69           9 :                 pidsize = strlen(buf);
      70           9 :                 if ((tmp = write(fd, buf, pidsize)) != (int)pidsize)
      71           0 :                         flog_err_sys(
      72             :                                 EC_LIB_SYSTEM_CALL,
      73             :                                 "Could not write pid %d to pid_file %s, rc was %d: %s",
      74             :                                 (int)pid, path, tmp, safe_strerror(errno));
      75           9 :                 else if (ftruncate(fd, pidsize) < 0)
      76           0 :                         flog_err_sys(
      77             :                                 EC_LIB_SYSTEM_CALL,
      78             :                                 "Could not truncate pid_file %s to %u bytes: %s",
      79             :                                 path, (unsigned int)pidsize,
      80             :                                 safe_strerror(errno));
      81             :         }
      82           9 :         return pid;
      83             : }

Generated by: LCOV version v1.16-topotato