unit-printf.c revision 41f9172f427bdbb8221c64029f78364b8dd4e527
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering This file is part of systemd.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Copyright 2010 Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering systemd is free software; you can redistribute it and/or modify it
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering under the terms of the GNU Lesser General Public License as published by
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering (at your option) any later version.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering systemd is distributed in the hope that it will be useful, but
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Lesser General Public License for more details.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering You should have received a copy of the GNU Lesser General Public License
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return unit_name_to_prefix_and_instance(u->id);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_prefix(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_filename(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return unit_name_path_unescape(u->instance);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_cgroup(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_cgroup_root(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return strdup(u->manager->cgroup_hierarchy);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (path_get_parent(u->manager->cgroup_hierarchy, &p) < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_runtime(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (u->manager->running_as == MANAGER_USER) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering const char *e;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_user_name(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* get USER env from our own env if set */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* fish username from passwd */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = get_user_creds(&username, NULL, NULL, NULL, NULL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_user_home(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* return HOME if set, otherwise from passwd */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = get_user_creds(&username, NULL, NULL, &home, NULL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic char *specifier_user_shell(char specifier, void *data, void *userdata) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* return HOME if set, otherwise from passwd */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = get_user_creds(&username, NULL, NULL, NULL, &shell);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringchar *unit_name_printf(Unit *u, const char* format) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * This will use the passed string as format string and
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * replace the following specifiers:
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %n: the full id of the unit (foo@bar.waldo)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %N: the id of the unit without the suffix (foo@bar)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %p: the prefix (foo)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %i: the instance (bar)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { 'N', specifier_prefix_and_instance, NULL },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return specifier_printf(format, table, u);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringchar *unit_full_printf(Unit *u, const char *format) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* This is similar to unit_name_printf() but also supports
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * unescaping. Also, adds a couple of additional codes:
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %f the the instance if set, otherwise the id
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %c cgroup path of unit
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %r root cgroup path of this systemd instance (e.g. "/user/lennart/shared/systemd-4711")
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %R parent of root cgroup path (e.g. "/usr/lennart/shared")
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %t the runtime directory to place sockets in (e.g. "/run" or $XDG_RUNTIME_DIR)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %u the username of the configured user or running user
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %h the homedir of the configured user or running user
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * %s the shell of the configured user or running user
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { 'N', specifier_prefix_and_instance, NULL },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { 'P', specifier_prefix_unescaped, NULL },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { 'I', specifier_instance_unescaped, NULL },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return specifier_printf(format, table, u);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringchar **unit_full_printf_strv(Unit *u, char **l) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering char **r, **i, **j;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* Applies unit_full_printf to every entry in l */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering for (i = l, j = r; *i; i++, j++) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering for (j--; j >= r; j--)