Method

GLibKeyFileload_unix_configurations

unstable since: 2.90

Declaration [src]

gboolean
g_key_file_load_unix_configurations (
  GKeyFile* key_file,
  const gchar* project,
  const gchar* etc_subdir,
  const gchar* run_subdir,
  const gchar* usr_subdir,
  const gchar* config_name,
  const gchar* config_suffix,
  GKeyFileFlags flags,
  GError** error
)

Description [src]

Evaluates and merges configuration key/values from multiple Unix directories into a single key file.

This function reads and merges all available configuration files based on the rules defined by the UAPI Configuration Files Specification (version 1).

This API is primarily intended for system daemons or CLI tools that need to load systemd-style configuration files spread across vendor and customization directories. User applications should generally use GSettings instead to manage user preferences.

Directory Layout Guidance

When choosing paths for etc_subdir and usr_subdir, you should prefer using your build system’s standard configuration variables (such as $sysconfdir and $libdir) rather than hard-coding absolute paths. For context, on a standard Linux layout, etc_subdir typically points to administrative overrides (e.g., /etc), run_subdir to /run while usr_subdir points to the vendor defaults (e.g., /usr/lib or /usr/share). Passing NULL will fall back to platform-specific defaults where appropriate.

Relationship to XDG Base Directory Specification

Note that this function operates independently of the XDG Base Directory Specification and g_get_system_config_dirs(). While XDG directories (like $XDG_CONFIG_DIRS) are intended to manage desktop session applications and user-facing environments, this API is strictly designed for low-level system-wide components following the UAPI specification. Mixing the two concepts should be avoided.

Note that this function is synchronous and blocking. Because it may load an arbitrary amount of files, it is best suited for application startup or non-interactive environments. If called from a user-interactive UI thread, you must handle asynchronicity yourself if needed.

If no file for parsing has been found, G_KEY_FILE_ERROR_NOT_FOUND is returned. If files have been found but the OS returns an error when opening or reading a file, a GFileError is returned. If there is a problem parsing files, a GKeyFileError is returned.

The following example parses files in following order:

  • <SYSCONFDIR>/project/mydaemon.conf
  • /run/project/mydaemon.conf (if /project/mydaemon.conf is not defined)
  • <LIBDIR>/project/mydaemon.conf (if <SYSCONFDIR>/project/mydaemon.conf and /run/project/mydaemon.conf are not defined)
  • valid drop-ins in <SYSCONFDIR>/project/mydaemon.conf.d/, /run/project/mydaemon.conf.d/, <LIBDIR>/project/mydaemon.conf.d/
g_autoptr(GKeyFile) kf = g_key_file_new ();
g_autoptr(GError) local_error = NULL;

// Using build-configured paths or defaults instead of hardcoded strings
gboolean success = g_key_file_load_unix_configurations (kf,
                                                        "my-daemon",
                                                        SYSCONFDIR,
                                                        RUNDIR,
                                                        LIBDIR,
                                                        "mydaemon",
                                                        "conf",
                                                        G_KEY_FILE_NONE,
                                                        &local_error);
if (!success)
  {
    g_warning ("Failed to load configuration: %s", local_error->message);
    return;
  }

g_autofree char *val = g_key_file_get_string (kf, "Management", "Setting", NULL);

Available since: 2.90

Parameters

project

Type: const gchar*

Name of the project used as subdirectory.

The argument can be NULL.
The data is owned by the caller of the method.
The value is a NUL terminated UTF-8 string.
etc_subdir

Type: const gchar*

Directory path for administrative configuration files.

The argument can be NULL.
The data is owned by the caller of the method.
The value is a platform-native string, using the preferred OS encoding on Unix and UTF-8 on Windows.
run_subdir

Type: const gchar*

Directory path for ephemeral overrides.

The argument can be NULL.
The data is owned by the caller of the method.
The value is a platform-native string, using the preferred OS encoding on Unix and UTF-8 on Windows.
usr_subdir

Type: const gchar*

Directory path for vendor-defined settings.

The argument can be NULL.
The data is owned by the caller of the method.
The value is a platform-native string, using the preferred OS encoding on Unix and UTF-8 on Windows.
config_name

Type: const gchar*

Basename of the configuration file.

The data is owned by the caller of the method.
The value is a platform-native string, using the preferred OS encoding on Unix and UTF-8 on Windows.
config_suffix

Type: const gchar*

Suffix of the configuration file.

The argument can be NULL.
The data is owned by the caller of the method.
The value is a platform-native string, using the preferred OS encoding on Unix and UTF-8 on Windows.
flags

Type: GKeyFileFlags

Flags from GKeyFileFlags.

error

Type: GError **

The return location for a recoverable error.

The argument can be NULL.
If the return location is not NULL, then you must initialize it to a NULL GError*.
The argument will be left initialized to NULL by the method if there are no errors.
In case of error, the argument will be set to a newly allocated GError; the caller will take ownership of the data, and be responsible for freeing it.

Return value

Type: gboolean

True on success, false otherwise.