PID-file management.
More...
#include <glib.h>
Go to the source code of this file.
PID-file management.
Definition in file pidfile.h.
◆ 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_path | The 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
52
53 if (g_mkdir_with_parents (dir, 0755))
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
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_path | The 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}