Greenbone Vulnerability Management Libraries  22.8.0
pidfile.c File Reference

PID-file management. More...

#include "pidfile.h"
#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <libgen.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   "libgvm base"
 GLib log domain. More...
 

Functions

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

Detailed Description

PID-file management.

Definition in file pidfile.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm base"

GLib log domain.

Definition at line 26 of file pidfile.c.

Function Documentation

◆ pidfile_create()

int pidfile_create ( gchar *  pid_file_path)

Create a PID-file.

A standard PID file will be created for the given path.

Parameters
[in]pid_file_pathThe full path of the pid file. E.g. "/tmp/service1.pid"
Returns
0 for success, anything else indicates an error.

Definition at line 40 of file pidfile.c.

41 {
42  FILE *pidfile;
43  gchar *copy, *dir;
44 
45  if (pid_file_path == NULL)
46  return -1;
47 
48  copy = g_strdup (pid_file_path);
49  dir = dirname (copy);
50 
51  /* Ensure directory exists. */
52 
53  if (g_mkdir_with_parents (dir, 0755)) /* "rwxr-xr-x" */
54  {
55  g_free (copy);
56  g_warning ("Failed to create PID file directory %s: %s", dir,
57  strerror (errno));
58  return 1;
59  }
60  g_free (copy);
61 
62  /* Create file. */
63 
64  pidfile = g_fopen (pid_file_path, "w");
65 
66  if (pidfile == NULL)
67  {
68  g_critical ("%s: failed to open pidfile %s: %s\n", __func__,
69  pid_file_path, strerror (errno));
70  return 1;
71  }
72  else
73  {
74  g_fprintf (pidfile, "%d\n", getpid ());
75  fclose (pidfile);
76  }
77  return 0;
78 }

◆ pidfile_remove()

void pidfile_remove ( gchar *  pid_file_path)

Remove PID file.

Parameters
[in]pid_file_pathThe full path of the pid file. E.g. "/tmp/service1.pid"

Definition at line 87 of file pidfile.c.

88 {
89  gchar *pidfile_contents;
90 
91  if (g_file_get_contents (pid_file_path, &pidfile_contents, NULL, NULL))
92  {
93  int pid = atoi (pidfile_contents);
94 
95  if (pid == getpid ())
96  {
97  g_unlink (pid_file_path);
98  }
99  g_free (pidfile_contents);
100  }
101 }