core: move nm_main_utils_get_nm_[ug]id() to "nm-core-utils.h"

There is a hierarchy of how files include each other. "main-utils.h"
is pretty much at the bottom (one above "main.c"), in the sense that
it only includes other headers, but is not included itself (aside
"main.c").

Move the utils function to a place where its accessible from everywhere
and rename.
This commit is contained in:
Thomas Haller 2022-01-18 17:30:14 +01:00
parent f755c7b1db
commit b1a14e3398
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
6 changed files with 50 additions and 47 deletions

View file

@ -209,42 +209,6 @@ nm_main_utils_ensure_not_running_pidfile(const char *pidfile)
}
}
uid_t nm_uid;
/**
* nm_main_utils_get_nm_uid:
*
* Checks what EUID NetworkManager is running as.
* Saves the EUID so it can be reused without making many syscalls.
*/
uid_t
nm_main_utils_get_nm_uid(void)
{
static uint8_t nm_uid_flag = 0;
if (!nm_uid_flag) {
nm_uid = geteuid();
nm_uid_flag = 1;
}
return nm_uid;
}
gid_t nm_gid;
/**
* nm_main_utils_get_nm_gid:
*
* Checks what EGID NetworkManager is running as.
* Saves the EGID so it can be reused without making many syscalls.
*/
gid_t
nm_main_utils_get_nm_gid(void)
{
static uint8_t nm_gid_flag = 0;
if (!nm_gid_flag) {
nm_gid = getegid();
nm_gid_flag = 1;
}
return nm_gid;
}
gboolean
nm_main_utils_early_setup(const char *progname,
int *argc,

View file

@ -6,10 +6,6 @@
#ifndef __MAIN_UTILS_H__
#define __MAIN_UTILS_H__
uid_t nm_main_utils_get_nm_uid(void);
gid_t nm_main_utils_get_nm_gid(void);
void nm_main_utils_setup_signals(GMainLoop *main_loop);
void nm_main_utils_ensure_statedir(void);

View file

@ -5145,3 +5145,43 @@ nm_utils_spawn_helper_finish(GAsyncResult *result, GError **error)
return g_task_propagate_pointer(task, error);
}
/*****************************************************************************/
/**
* nm_utils_get_nm_uid:
*
* Checks what EUID NetworkManager is running as.
* Saves the EUID so it can be reused without making many syscalls.
*/
uid_t
nm_utils_get_nm_uid(void)
{
static uint8_t nm_uid_flag = 0;
static uid_t nm_uid;
if (!nm_uid_flag) {
nm_uid = geteuid();
nm_uid_flag = 1;
}
return nm_uid;
}
/**
* nm_utils_get_nm_gid:
*
* Checks what EGID NetworkManager is running as.
* Saves the EGID so it can be reused without making many syscalls.
*/
gid_t
nm_utils_get_nm_gid(void)
{
static uint8_t nm_gid_flag = 0;
static gid_t nm_gid;
if (!nm_gid_flag) {
nm_gid = getegid();
nm_gid_flag = 1;
}
return nm_gid;
}

View file

@ -9,6 +9,7 @@
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include "nm-connection.h"
@ -470,4 +471,10 @@ void nm_utils_spawn_helper(const char *const *args,
char *nm_utils_spawn_helper_finish(GAsyncResult *result, GError **error);
/*****************************************************************************/
uid_t nm_utils_get_nm_uid(void);
gid_t nm_utils_get_nm_gid(void);
#endif /* __NM_CORE_UTILS_H__ */

View file

@ -9,7 +9,6 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "libnm-glib-aux/nm-uuid.h"
#include "libnm-glib-aux/nm-io-utils.h"
@ -19,7 +18,6 @@
#include "nm-setting-wireless.h"
#include "nm-setting-wireless-security.h"
#include "nm-config.h"
#include "src/core/main-utils.h"
/*****************************************************************************/
@ -339,7 +337,7 @@ nms_keyfile_utils_check_file_permissions_stat(NMSKeyfileFiletype filetype,
g_return_val_if_reached(FALSE);
if (!NM_FLAGS_HAS(nm_utils_get_testing(), NM_UTILS_TEST_NO_KEYFILE_OWNER_CHECK)) {
if (st->st_uid != nm_main_utils_get_nm_uid()) {
if (st->st_uid != nm_utils_get_nm_uid()) {
g_set_error(error,
NM_SETTINGS_ERROR,
NM_SETTINGS_ERROR_INVALID_CONNECTION,

View file

@ -10,7 +10,6 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "libnm-core-intern/nm-keyfile-internal.h"
@ -19,7 +18,6 @@
#include "nms-keyfile-reader.h"
#include "libnm-glib-aux/nm-io-utils.h"
#include "src/core/main-utils.h"
/*****************************************************************************/
@ -446,8 +444,8 @@ nms_keyfile_writer_connection(NMConnection *connection,
keyfile_dir,
profile_dir,
TRUE,
nm_main_utils_get_nm_uid(),
nm_main_utils_get_nm_gid(),
nm_utils_get_nm_uid(),
nm_utils_get_nm_gid(),
existing_path,
existing_path_read_only,
force_rename,