2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-07-02 12:23:36 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
2014-07-02 12:23:36 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "sd-path.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "log.h"
|
2014-07-02 12:23:36 +02:00
|
|
|
#include "macro.h"
|
2018-11-19 20:26:37 +01:00
|
|
|
#include "main-func.h"
|
2018-11-20 15:42:57 +01:00
|
|
|
#include "pretty-print.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2014-07-02 12:23:36 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static const char *arg_suffix = NULL;
|
|
|
|
|
|
|
|
static const char* const path_table[_SD_PATH_MAX] = {
|
sd-path: drop "-dir" and "-path" suffixes from path enums
Clean up the naming of the sd-path enums. Previously, the more recently
added fields where named in the form SD_PATH_xyz_DIR and
SD_PATH_xyz_PATH, while the older fields where called just SD_PATH_xyz
and SD_PATH_SEARCH_xyz. Let's clean this up, to come to a more unified
way how we name this stuff.
I opted to stick to the old naming, i.e. dropthe suffixes. It's a bit of
a bike-shedding question of course, but I think there's a good reason to
avoid the additional DIR and PATH suffixes: the enum prefix contains
"PATH" anyway (i.e. "SD_PATH_"), so including PATH twice in each name is
redundant. Moreover, the key difference between the enums with the "dir"
and the "path" in the name is that the latter are *seach* paths, and I
think this is better emphasized by sticking to the "SEARCH" in the name.
Moreover dropping the suffixes makes the identifiers a lot shorter, in
particular in the "systemd-path" list output. And that's always good.
This means the naming pkgconfig file and in sd-path slightly deviate
(though the mapping is very simple), but I think that's OK, given that
this is developer facing and not user facing.
2020-05-28 09:28:28 +02:00
|
|
|
[SD_PATH_TEMPORARY] = "temporary",
|
|
|
|
[SD_PATH_TEMPORARY_LARGE] = "temporary-large",
|
|
|
|
[SD_PATH_SYSTEM_BINARIES] = "system-binaries",
|
|
|
|
[SD_PATH_SYSTEM_INCLUDE] = "system-include",
|
|
|
|
[SD_PATH_SYSTEM_LIBRARY_PRIVATE] = "system-library-private",
|
|
|
|
[SD_PATH_SYSTEM_LIBRARY_ARCH] = "system-library-arch",
|
|
|
|
[SD_PATH_SYSTEM_SHARED] = "system-shared",
|
|
|
|
[SD_PATH_SYSTEM_CONFIGURATION_FACTORY] = "system-configuration-factory",
|
|
|
|
[SD_PATH_SYSTEM_STATE_FACTORY] = "system-state-factory",
|
|
|
|
[SD_PATH_SYSTEM_CONFIGURATION] = "system-configuration",
|
|
|
|
[SD_PATH_SYSTEM_RUNTIME] = "system-runtime",
|
|
|
|
[SD_PATH_SYSTEM_RUNTIME_LOGS] = "system-runtime-logs",
|
|
|
|
[SD_PATH_SYSTEM_STATE_PRIVATE] = "system-state-private",
|
|
|
|
[SD_PATH_SYSTEM_STATE_LOGS] = "system-state-logs",
|
|
|
|
[SD_PATH_SYSTEM_STATE_CACHE] = "system-state-cache",
|
|
|
|
[SD_PATH_SYSTEM_STATE_SPOOL] = "system-state-spool",
|
|
|
|
[SD_PATH_USER_BINARIES] = "user-binaries",
|
|
|
|
[SD_PATH_USER_LIBRARY_PRIVATE] = "user-library-private",
|
|
|
|
[SD_PATH_USER_LIBRARY_ARCH] = "user-library-arch",
|
|
|
|
[SD_PATH_USER_SHARED] = "user-shared",
|
|
|
|
[SD_PATH_USER_CONFIGURATION] = "user-configuration",
|
|
|
|
[SD_PATH_USER_RUNTIME] = "user-runtime",
|
|
|
|
[SD_PATH_USER_STATE_CACHE] = "user-state-cache",
|
|
|
|
[SD_PATH_USER] = "user",
|
|
|
|
[SD_PATH_USER_DOCUMENTS] = "user-documents",
|
|
|
|
[SD_PATH_USER_MUSIC] = "user-music",
|
|
|
|
[SD_PATH_USER_PICTURES] = "user-pictures",
|
|
|
|
[SD_PATH_USER_VIDEOS] = "user-videos",
|
|
|
|
[SD_PATH_USER_DOWNLOAD] = "user-download",
|
|
|
|
[SD_PATH_USER_PUBLIC] = "user-public",
|
|
|
|
[SD_PATH_USER_TEMPLATES] = "user-templates",
|
|
|
|
[SD_PATH_USER_DESKTOP] = "user-desktop",
|
|
|
|
[SD_PATH_SEARCH_BINARIES] = "search-binaries",
|
|
|
|
[SD_PATH_SEARCH_BINARIES_DEFAULT] = "search-binaries-default",
|
|
|
|
[SD_PATH_SEARCH_LIBRARY_PRIVATE] = "search-library-private",
|
|
|
|
[SD_PATH_SEARCH_LIBRARY_ARCH] = "search-library-arch",
|
|
|
|
[SD_PATH_SEARCH_SHARED] = "search-shared",
|
|
|
|
[SD_PATH_SEARCH_CONFIGURATION_FACTORY] = "search-configuration-factory",
|
|
|
|
[SD_PATH_SEARCH_STATE_FACTORY] = "search-state-factory",
|
|
|
|
[SD_PATH_SEARCH_CONFIGURATION] = "search-configuration",
|
path: show various systemd directories and search paths too
So far we had various ad hoc APIs to query search paths:
systemd-analyze unit-paths, lookup_paths_log(), the pkgconfig file,
debug logs emitted by systemd-analyze cat-config.
But answering a simple question "what is the search path for tmpfiles,
sysusers, .network files, ..." is surprisingly hard.
I think we should have an api that makes it easy to query this. Pkgconfig is
not bad, but it is primarily a development tool, so it's not available in many
context. Also it can't provide support for paths which are influenced by
environment variables, and I'd like to be able to answer the question "what is
the search path for ..., assuming that VAR_FOO=... is set?".
Extending sd-path to support more of our internal paths seems to be most
flexible solution. We already have systemd-path which provides a nice
way to query, and we can add stuff like optional descriptions later on.
We we essentially get a nice programmatic and commmandline apis for the price
of one.
2020-03-24 16:04:50 +01:00
|
|
|
|
sd-path: drop "-dir" and "-path" suffixes from path enums
Clean up the naming of the sd-path enums. Previously, the more recently
added fields where named in the form SD_PATH_xyz_DIR and
SD_PATH_xyz_PATH, while the older fields where called just SD_PATH_xyz
and SD_PATH_SEARCH_xyz. Let's clean this up, to come to a more unified
way how we name this stuff.
I opted to stick to the old naming, i.e. dropthe suffixes. It's a bit of
a bike-shedding question of course, but I think there's a good reason to
avoid the additional DIR and PATH suffixes: the enum prefix contains
"PATH" anyway (i.e. "SD_PATH_"), so including PATH twice in each name is
redundant. Moreover, the key difference between the enums with the "dir"
and the "path" in the name is that the latter are *seach* paths, and I
think this is better emphasized by sticking to the "SEARCH" in the name.
Moreover dropping the suffixes makes the identifiers a lot shorter, in
particular in the "systemd-path" list output. And that's always good.
This means the naming pkgconfig file and in sd-path slightly deviate
(though the mapping is very simple), but I think that's OK, given that
this is developer facing and not user facing.
2020-05-28 09:28:28 +02:00
|
|
|
[SD_PATH_SYSTEMD_UTIL] = "systemd-util",
|
|
|
|
[SD_PATH_SYSTEMD_SYSTEM_UNIT] = "systemd-system-unit",
|
|
|
|
[SD_PATH_SYSTEMD_SYSTEM_PRESET] = "systemd-system-preset",
|
|
|
|
[SD_PATH_SYSTEMD_SYSTEM_CONF] = "systemd-system-conf",
|
2020-09-23 02:21:27 +02:00
|
|
|
[SD_PATH_SYSTEMD_SEARCH_SYSTEM_UNIT] = "systemd-search-system-unit",
|
sd-path: drop "-dir" and "-path" suffixes from path enums
Clean up the naming of the sd-path enums. Previously, the more recently
added fields where named in the form SD_PATH_xyz_DIR and
SD_PATH_xyz_PATH, while the older fields where called just SD_PATH_xyz
and SD_PATH_SEARCH_xyz. Let's clean this up, to come to a more unified
way how we name this stuff.
I opted to stick to the old naming, i.e. dropthe suffixes. It's a bit of
a bike-shedding question of course, but I think there's a good reason to
avoid the additional DIR and PATH suffixes: the enum prefix contains
"PATH" anyway (i.e. "SD_PATH_"), so including PATH twice in each name is
redundant. Moreover, the key difference between the enums with the "dir"
and the "path" in the name is that the latter are *seach* paths, and I
think this is better emphasized by sticking to the "SEARCH" in the name.
Moreover dropping the suffixes makes the identifiers a lot shorter, in
particular in the "systemd-path" list output. And that's always good.
This means the naming pkgconfig file and in sd-path slightly deviate
(though the mapping is very simple), but I think that's OK, given that
this is developer facing and not user facing.
2020-05-28 09:28:28 +02:00
|
|
|
[SD_PATH_SYSTEMD_SYSTEM_GENERATOR] = "systemd-system-generator",
|
2020-09-23 02:21:27 +02:00
|
|
|
[SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR] = "systemd-search-system-generator",
|
sd-path: drop "-dir" and "-path" suffixes from path enums
Clean up the naming of the sd-path enums. Previously, the more recently
added fields where named in the form SD_PATH_xyz_DIR and
SD_PATH_xyz_PATH, while the older fields where called just SD_PATH_xyz
and SD_PATH_SEARCH_xyz. Let's clean this up, to come to a more unified
way how we name this stuff.
I opted to stick to the old naming, i.e. dropthe suffixes. It's a bit of
a bike-shedding question of course, but I think there's a good reason to
avoid the additional DIR and PATH suffixes: the enum prefix contains
"PATH" anyway (i.e. "SD_PATH_"), so including PATH twice in each name is
redundant. Moreover, the key difference between the enums with the "dir"
and the "path" in the name is that the latter are *seach* paths, and I
think this is better emphasized by sticking to the "SEARCH" in the name.
Moreover dropping the suffixes makes the identifiers a lot shorter, in
particular in the "systemd-path" list output. And that's always good.
This means the naming pkgconfig file and in sd-path slightly deviate
(though the mapping is very simple), but I think that's OK, given that
this is developer facing and not user facing.
2020-05-28 09:28:28 +02:00
|
|
|
[SD_PATH_SYSTEMD_USER_UNIT] = "systemd-user-unit",
|
|
|
|
[SD_PATH_SYSTEMD_USER_PRESET] = "systemd-user-preset",
|
|
|
|
[SD_PATH_SYSTEMD_USER_CONF] = "systemd-user-conf",
|
2020-09-23 02:21:27 +02:00
|
|
|
[SD_PATH_SYSTEMD_SEARCH_USER_UNIT] = "systemd-search-user-unit",
|
|
|
|
[SD_PATH_SYSTEMD_SEARCH_USER_GENERATOR] = "systemd-search-user-generator",
|
sd-path: drop "-dir" and "-path" suffixes from path enums
Clean up the naming of the sd-path enums. Previously, the more recently
added fields where named in the form SD_PATH_xyz_DIR and
SD_PATH_xyz_PATH, while the older fields where called just SD_PATH_xyz
and SD_PATH_SEARCH_xyz. Let's clean this up, to come to a more unified
way how we name this stuff.
I opted to stick to the old naming, i.e. dropthe suffixes. It's a bit of
a bike-shedding question of course, but I think there's a good reason to
avoid the additional DIR and PATH suffixes: the enum prefix contains
"PATH" anyway (i.e. "SD_PATH_"), so including PATH twice in each name is
redundant. Moreover, the key difference between the enums with the "dir"
and the "path" in the name is that the latter are *seach* paths, and I
think this is better emphasized by sticking to the "SEARCH" in the name.
Moreover dropping the suffixes makes the identifiers a lot shorter, in
particular in the "systemd-path" list output. And that's always good.
This means the naming pkgconfig file and in sd-path slightly deviate
(though the mapping is very simple), but I think that's OK, given that
this is developer facing and not user facing.
2020-05-28 09:28:28 +02:00
|
|
|
[SD_PATH_SYSTEMD_USER_GENERATOR] = "systemd-user-generator",
|
|
|
|
[SD_PATH_SYSTEMD_SLEEP] = "systemd-sleep",
|
|
|
|
[SD_PATH_SYSTEMD_SHUTDOWN] = "systemd-shutdown",
|
path: show various systemd directories and search paths too
So far we had various ad hoc APIs to query search paths:
systemd-analyze unit-paths, lookup_paths_log(), the pkgconfig file,
debug logs emitted by systemd-analyze cat-config.
But answering a simple question "what is the search path for tmpfiles,
sysusers, .network files, ..." is surprisingly hard.
I think we should have an api that makes it easy to query this. Pkgconfig is
not bad, but it is primarily a development tool, so it's not available in many
context. Also it can't provide support for paths which are influenced by
environment variables, and I'd like to be able to answer the question "what is
the search path for ..., assuming that VAR_FOO=... is set?".
Extending sd-path to support more of our internal paths seems to be most
flexible solution. We already have systemd-path which provides a nice
way to query, and we can add stuff like optional descriptions later on.
We we essentially get a nice programmatic and commmandline apis for the price
of one.
2020-03-24 16:04:50 +01:00
|
|
|
|
sd-path: drop "-dir" and "-path" suffixes from path enums
Clean up the naming of the sd-path enums. Previously, the more recently
added fields where named in the form SD_PATH_xyz_DIR and
SD_PATH_xyz_PATH, while the older fields where called just SD_PATH_xyz
and SD_PATH_SEARCH_xyz. Let's clean this up, to come to a more unified
way how we name this stuff.
I opted to stick to the old naming, i.e. dropthe suffixes. It's a bit of
a bike-shedding question of course, but I think there's a good reason to
avoid the additional DIR and PATH suffixes: the enum prefix contains
"PATH" anyway (i.e. "SD_PATH_"), so including PATH twice in each name is
redundant. Moreover, the key difference between the enums with the "dir"
and the "path" in the name is that the latter are *seach* paths, and I
think this is better emphasized by sticking to the "SEARCH" in the name.
Moreover dropping the suffixes makes the identifiers a lot shorter, in
particular in the "systemd-path" list output. And that's always good.
This means the naming pkgconfig file and in sd-path slightly deviate
(though the mapping is very simple), but I think that's OK, given that
this is developer facing and not user facing.
2020-05-28 09:28:28 +02:00
|
|
|
[SD_PATH_TMPFILES] = "tmpfiles",
|
|
|
|
[SD_PATH_SYSUSERS] = "sysusers",
|
|
|
|
[SD_PATH_SYSCTL] = "sysctl",
|
|
|
|
[SD_PATH_BINFMT] = "binfmt",
|
|
|
|
[SD_PATH_MODULES_LOAD] = "modules-load",
|
|
|
|
[SD_PATH_CATALOG] = "catalog",
|
2020-03-24 17:12:45 +01:00
|
|
|
|
sd-path: drop "-dir" and "-path" suffixes from path enums
Clean up the naming of the sd-path enums. Previously, the more recently
added fields where named in the form SD_PATH_xyz_DIR and
SD_PATH_xyz_PATH, while the older fields where called just SD_PATH_xyz
and SD_PATH_SEARCH_xyz. Let's clean this up, to come to a more unified
way how we name this stuff.
I opted to stick to the old naming, i.e. dropthe suffixes. It's a bit of
a bike-shedding question of course, but I think there's a good reason to
avoid the additional DIR and PATH suffixes: the enum prefix contains
"PATH" anyway (i.e. "SD_PATH_"), so including PATH twice in each name is
redundant. Moreover, the key difference between the enums with the "dir"
and the "path" in the name is that the latter are *seach* paths, and I
think this is better emphasized by sticking to the "SEARCH" in the name.
Moreover dropping the suffixes makes the identifiers a lot shorter, in
particular in the "systemd-path" list output. And that's always good.
This means the naming pkgconfig file and in sd-path slightly deviate
(though the mapping is very simple), but I think that's OK, given that
this is developer facing and not user facing.
2020-05-28 09:28:28 +02:00
|
|
|
[SD_PATH_SYSTEMD_SEARCH_NETWORK] = "systemd-search-network",
|
2014-07-02 12:23:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int list_homes(void) {
|
|
|
|
uint64_t i = 0;
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ELEMENTSOF(path_table); i++) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
int q;
|
|
|
|
|
2020-03-23 19:50:59 +01:00
|
|
|
q = sd_path_lookup(i, arg_suffix, &p);
|
2014-07-02 12:23:36 +02:00
|
|
|
if (q < 0) {
|
2020-03-26 17:33:43 +01:00
|
|
|
log_full_errno(q == -ENXIO ? LOG_DEBUG : LOG_ERR,
|
|
|
|
q, "Failed to query %s: %m", path_table[i]);
|
|
|
|
if (q != -ENXIO)
|
|
|
|
r = q;
|
2014-07-02 12:23:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s: %s\n", path_table[i], p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_home(const char *n) {
|
|
|
|
uint64_t i = 0;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
for (i = 0; i < ELEMENTSOF(path_table); i++) {
|
|
|
|
if (streq(path_table[i], n)) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
2020-03-23 19:50:59 +01:00
|
|
|
r = sd_path_lookup(i, arg_suffix, &p);
|
2014-11-28 18:50:43 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to query %s: %m", n);
|
2014-07-02 12:23:36 +02:00
|
|
|
|
|
|
|
printf("%s\n", p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
|
|
|
"Path %s not known.", n);
|
2014-07-02 12:23:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("systemd-path", "1", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
printf("%s [OPTIONS...] [NAME...]\n\n"
|
|
|
|
"Show system and user paths.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
" --suffix=SUFFIX Suffix to append to paths\n"
|
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2014-08-02 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2014-07-02 12:23:36 +02:00
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_SUFFIX,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "suffix", required_argument, NULL, ARG_SUFFIX },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
|
2014-07-02 12:23:36 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2014-07-02 12:23:36 +02:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2014-07-02 12:23:36 +02:00
|
|
|
|
|
|
|
case ARG_SUFFIX:
|
|
|
|
arg_suffix = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:54:20 +01:00
|
|
|
static int run(int argc, char* argv[]) {
|
2014-07-02 12:23:36 +02:00
|
|
|
int r;
|
|
|
|
|
2019-04-26 12:28:25 +02:00
|
|
|
log_show_color(true);
|
2014-07-02 12:23:36 +02:00
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
2018-11-16 14:54:20 +01:00
|
|
|
return r;
|
2014-07-02 12:23:36 +02:00
|
|
|
|
|
|
|
if (argc > optind) {
|
|
|
|
int i, q;
|
|
|
|
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
q = print_home(argv[i]);
|
|
|
|
if (q < 0)
|
|
|
|
r = q;
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:54:20 +01:00
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
return list_homes();
|
2014-07-02 12:23:36 +02:00
|
|
|
}
|
2018-11-16 14:54:20 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|