mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-09 03:38:09 +02:00
main: use helper function to write pid file in nm_main_utils_write_pidfile()
On the surface, writing a file seams simple enough. But there are many pitfalls: - we should retry on EINTR. - we should check for incomplete writes and loop. - we possibly should check errors from close. - we possibly should write to a temporary file and do atomic rename. Use nm_utils_file_set_contents() to get this right.
This commit is contained in:
parent
70417fda9e
commit
d98553e9e7
1 changed files with 7 additions and 21 deletions
|
|
@ -18,6 +18,7 @@
|
||||||
#include "main-utils.h"
|
#include "main-utils.h"
|
||||||
#include "NetworkManagerUtils.h"
|
#include "NetworkManagerUtils.h"
|
||||||
#include "nm-config.h"
|
#include "nm-config.h"
|
||||||
|
#include "libnm-glib-aux/nm-io-utils.h"
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
sighup_handler(gpointer user_data)
|
sighup_handler(gpointer user_data)
|
||||||
|
|
@ -76,31 +77,16 @@ nm_main_utils_setup_signals(GMainLoop *main_loop)
|
||||||
gboolean
|
gboolean
|
||||||
nm_main_utils_write_pidfile(const char *pidfile)
|
nm_main_utils_write_pidfile(const char *pidfile)
|
||||||
{
|
{
|
||||||
char pid[16];
|
gs_free_error GError *error = NULL;
|
||||||
int fd;
|
char pid[16];
|
||||||
int errsv;
|
|
||||||
gboolean success = FALSE;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
if ((fd = open(pidfile, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 00644)) < 0) {
|
nm_sprintf_buf(pid, "%lld", (long long) getpid());
|
||||||
errsv = errno;
|
if (!nm_utils_file_set_contents(pidfile, pid, -1, 00644, NULL, NULL, &error)) {
|
||||||
fprintf(stderr, _("Opening %s failed: %s\n"), pidfile, nm_strerror_native(errsv));
|
fprintf(stderr, _("Writing to %s failed: %s\n"), pidfile, error->message);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_snprintf(pid, sizeof(pid), "%d", getpid());
|
return TRUE;
|
||||||
if (write(fd, pid, strlen(pid)) < 0) {
|
|
||||||
errsv = errno;
|
|
||||||
fprintf(stderr, _("Writing to %s failed: %s\n"), pidfile, nm_strerror_native(errsv));
|
|
||||||
} else
|
|
||||||
success = TRUE;
|
|
||||||
|
|
||||||
r = nm_close_with_error(fd);
|
|
||||||
if (r < 0) {
|
|
||||||
fprintf(stderr, _("Closing %s failed: %s\n"), pidfile, nm_strerror_native(-r));
|
|
||||||
}
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue