Greenbone Vulnerability Management Libraries  10.0.0
pidfile.c File Reference

PID-file management. More...

#include "pidfile.h"
#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Include dependency graph for pidfile.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "base pidfile"
 GLib log domain. More...
 

Functions

int pidfile_create (gchar *daemon_name)
 Create a PID-file. More...
 
void pidfile_remove (gchar *daemon_name)
 Remove PID file. More...
 

Detailed Description

PID-file management.

Definition in file pidfile.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "base pidfile"

GLib log domain.

Definition at line 39 of file pidfile.c.

Function Documentation

◆ pidfile_create()

int pidfile_create ( gchar *  daemon_name)

Create a PID-file.

A standard PID file will be created for the given daemon name.

Parameters
[in]daemon_nameThe name of the daemon
Returns
0 for success, anything else indicates an error.

Definition at line 52 of file pidfile.c.

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 }

◆ pidfile_remove()

void pidfile_remove ( gchar *  daemon_name)

Remove PID file.

Parameters
[in]daemon_nameThe name of the daemon

Definition at line 81 of file pidfile.c.

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 }