1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "specifier.h"
26 #include "unit-name.h"
28 #include "install-printf.h"
30 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
31 InstallInfo *i = userdata;
34 return unit_name_to_prefix_and_instance(i->name);
37 static char *specifier_prefix(char specifier, void *data, void *userdata) {
38 InstallInfo *i = userdata;
41 return unit_name_to_prefix(i->name);
44 static char *specifier_instance(char specifier, void *data, void *userdata) {
45 InstallInfo *i = userdata;
51 r = unit_name_to_instance(i->name, &instance);
60 static char *specifier_user_name(char specifier, void *data, void *userdata) {
61 InstallInfo *i = userdata;
63 _cleanup_free_ char *tmp = NULL;
71 /* get USER env from env or our own uid */
72 username = tmp = getusername_malloc();
76 printed = strdup(username);
79 /* fish username from passwd */
83 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
87 if (asprintf(&printed, "%d", uid) < 0)
96 char *install_full_printf(InstallInfo *i, const char *format) {
98 /* This is similar to unit_full_printf() but does not support
99 * anything path-related.
101 * %n: the full id of the unit (foo@bar.waldo)
102 * %N: the id of the unit without the suffix (foo@bar)
103 * %p: the prefix (foo)
104 * %i: the instance (bar)
106 * %U the UID of the configured user or running user
107 * %u the username of the configured user or running user
108 * %m the machine ID of the running system
109 * %H the host name of the running system
110 * %b the boot ID of the running system
113 const Specifier table[] = {
114 { 'n', specifier_string, i->name },
115 { 'N', specifier_prefix_and_instance, NULL },
116 { 'p', specifier_prefix, NULL },
117 { 'i', specifier_instance, NULL },
119 { 'U', specifier_user_name, NULL },
120 { 'u', specifier_user_name, NULL },
122 { 'm', specifier_machine_id, NULL },
123 { 'H', specifier_host_name, NULL },
124 { 'b', specifier_boot_id, NULL },
131 return specifier_printf(format, table, i);