Skip to content

Commit

Permalink
Change: Create pid file by specifying full path
Browse files Browse the repository at this point in the history
Delete GVM_PID_DIR define.
Change pid file functions to receive the full path
to the pid file instead of just the name and
using GVM_PID_DIR.
  • Loading branch information
ArnoStiefvater committed Nov 15, 2021
1 parent 20a2c92 commit 8eb2eef
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ if (NOT GVM_RUN_DIR)
set (GVM_RUN_DIR "/run/gvm")
endif (NOT GVM_RUN_DIR)

if (NOT GVM_PID_DIR)
set (GVM_PID_DIR "${GVM_RUN_DIR}")
endif (NOT GVM_PID_DIR)

if (NOT GVM_SYSCONF_DIR)
set (GVM_SYSCONF_DIR "${SYSCONFDIR}/gvm")
endif (NOT GVM_SYSCONF_DIR)
Expand Down
6 changes: 1 addition & 5 deletions base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ set (LIBGVM_BASE_NAME
${LIBGVM_BASE_NAME}
PARENT_SCOPE)

if (GVM_PID_DIR)
add_definitions (-DGVM_PID_DIR="${GVM_PID_DIR}")
endif (GVM_PID_DIR)

if (GVM_SYSCONF_DIR)
add_definitions (-DGVM_SYSCONF_DIR="${GVM_SYSCONF_DIR}")
endif (GVM_SYSCONF_DIR)
Expand Down Expand Up @@ -143,7 +139,7 @@ endif (BUILD_TESTS)

configure_file (libgvm_base.pc.in ${CMAKE_BINARY_DIR}/libgvm_base.pc @ONLY)

install (DIRECTORY DESTINATION ${GVM_PID_DIR})
install (DIRECTORY DESTINATION ${GVM_RUN_DIR})

install (FILES ${CMAKE_BINARY_DIR}/libgvm_base.pc
DESTINATION ${LIBDIR}/pkgconfig)
Expand Down
31 changes: 11 additions & 20 deletions base/pidfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "pidfile.h"

#include <errno.h> /* for errno */
#include <glib.h> /* for g_free, gchar, g_build_filename, g_strconcat */
#include <glib.h> /* for g_free, gchar */
#include <glib/gstdio.h> /* for g_unlink, g_fopen */
#include <stdio.h> /* for fclose, FILE */
#include <stdlib.h> /* for atoi */
Expand All @@ -42,20 +42,17 @@
* @brief Create a PID-file.
*
* A standard PID file will be created for the
* given daemon name.
* given path.
*
* @param[in] daemon_name The name of the daemon
* @param[in] pid_file_path The full path of the pid file. E.g.
* "/tmp/service1.pid"
*
* @return 0 for success, anything else indicates an error.
*/
int
pidfile_create (gchar *daemon_name)
pidfile_create (gchar *pid_file_path)
{
gchar *name_pid = g_strconcat (daemon_name, ".pid", NULL);
gchar *pidfile_name = g_build_filename (GVM_PID_DIR, name_pid, NULL);
FILE *pidfile = g_fopen (pidfile_name, "w");

g_free (name_pid);
FILE *pidfile = g_fopen (pid_file_path, "w");

if (pidfile == NULL)
{
Expand All @@ -67,35 +64,29 @@ pidfile_create (gchar *daemon_name)
{
g_fprintf (pidfile, "%d\n", getpid ());
fclose (pidfile);
g_free (pidfile_name);
}
return 0;
}

/**
* @brief Remove PID file.
*
* @param[in] daemon_name The name of the daemon
* @param[in] pid_file_path The full path of the pid file. E.g.
* "/tmp/service1.pid"
*/
void
pidfile_remove (gchar *daemon_name)
pidfile_remove (gchar *pid_file_path)
{
gchar *name_pid = g_strconcat (daemon_name, ".pid", NULL);
gchar *pidfile_name = g_build_filename (GVM_PID_DIR, name_pid, NULL);
gchar *pidfile_contents;

g_free (name_pid);

if (g_file_get_contents (pidfile_name, &pidfile_contents, NULL, NULL))
if (g_file_get_contents (pid_file_path, &pidfile_contents, NULL, NULL))
{
int pid = atoi (pidfile_contents);

if (pid == getpid ())
{
g_unlink (pidfile_name);
g_unlink (pid_file_path);
}
g_free (pidfile_contents);
}

g_free (pidfile_name);
}

0 comments on commit 8eb2eef

Please sign in to comment.