"Fossies" - the Fresh Open Source Software Archive

Member "gvm-libs-11.0.1/base/pidfile.c" (12 May 2020, 2676 Bytes) of package /linux/misc/openvas/gvm-libs-11.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pidfile.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.0-beta2_vs_1.0.0.

    1 /* Copyright (C) 2009-2019 Greenbone Networks GmbH
    2  *
    3  * SPDX-License-Identifier: GPL-2.0-or-later
    4  *
    5  * This program is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU General Public License
    7  * as published by the Free Software Foundation; either version 2
    8  * of the License, or (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program; if not, write to the Free Software
   17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   18  */
   19 
   20 /**
   21  * @file
   22  * @brief PID-file management.
   23  */
   24 
   25 #include "pidfile.h"
   26 
   27 #include <errno.h>       /* for errno */
   28 #include <glib.h>        /* for g_free, gchar, g_build_filename, g_strconcat */
   29 #include <glib/gstdio.h> /* for g_unlink, g_fopen */
   30 #include <stdio.h>       /* for fclose, FILE */
   31 #include <stdlib.h>      /* for atoi */
   32 #include <string.h>      /* for strerror */
   33 #include <unistd.h>      /* for getpid */
   34 
   35 /**
   36  * @brief GLib log domain.
   37  */
   38 #undef G_LOG_DOMAIN
   39 #define G_LOG_DOMAIN "base pidfile"
   40 
   41 /**
   42  * @brief Create a PID-file.
   43  *
   44  * A standard PID file will be created for the
   45  * given daemon name.
   46  *
   47  * @param[in]  daemon_name The name of the daemon
   48  *
   49  * @return 0 for success, anything else indicates an error.
   50  */
   51 int
   52 pidfile_create (gchar *daemon_name)
   53 {
   54   gchar *name_pid = g_strconcat (daemon_name, ".pid", NULL);
   55   gchar *pidfile_name = g_build_filename (GVM_PID_DIR, name_pid, NULL);
   56   FILE *pidfile = g_fopen (pidfile_name, "w");
   57 
   58   g_free (name_pid);
   59 
   60   if (pidfile == NULL)
   61     {
   62       g_critical ("%s: failed to open pidfile: %s\n", __FUNCTION__,
   63                   strerror (errno));
   64       return 1;
   65     }
   66   else
   67     {
   68       g_fprintf (pidfile, "%d\n", getpid ());
   69       fclose (pidfile);
   70       g_free (pidfile_name);
   71     }
   72   return 0;
   73 }
   74 
   75 /**
   76  * @brief Remove PID file.
   77  *
   78  * @param[in]  daemon_name The name of the daemon
   79  */
   80 void
   81 pidfile_remove (gchar *daemon_name)
   82 {
   83   gchar *name_pid = g_strconcat (daemon_name, ".pid", NULL);
   84   gchar *pidfile_name = g_build_filename (GVM_PID_DIR, name_pid, NULL);
   85   gchar *pidfile_contents;
   86 
   87   g_free (name_pid);
   88 
   89   if (g_file_get_contents (pidfile_name, &pidfile_contents, NULL, NULL))
   90     {
   91       int pid = atoi (pidfile_contents);
   92 
   93       if (pid == getpid ())
   94         {
   95           g_unlink (pidfile_name);
   96         }
   97       g_free (pidfile_contents);
   98     }
   99 
  100   g_free (pidfile_name);
  101 }