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 int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
31 InstallInfo *i = userdata;
36 n = unit_name_to_prefix_and_instance(i->name);
44 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
45 InstallInfo *i = userdata;
50 n = unit_name_to_prefix(i->name);
58 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
59 InstallInfo *i = userdata;
65 r = unit_name_to_instance(i->name, &instance);
70 instance = strdup("");
79 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
80 InstallInfo *i = userdata;
82 _cleanup_free_ char *tmp = NULL;
90 /* get USER env from env or our own uid */
91 username = tmp = getusername_malloc();
95 printed = strdup(username);
98 /* fish username from passwd */
102 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
106 if (asprintf(&printed, "%d", uid) < 0)
117 int install_full_printf(InstallInfo *i, const char *format, char **ret) {
119 /* This is similar to unit_full_printf() but does not support
120 * anything path-related.
122 * %n: the full id of the unit (foo@bar.waldo)
123 * %N: the id of the unit without the suffix (foo@bar)
124 * %p: the prefix (foo)
125 * %i: the instance (bar)
127 * %U the UID of the configured user or running user
128 * %u the username of the configured user or running user
129 * %m the machine ID of the running system
130 * %H the host name of the running system
131 * %b the boot ID of the running system
132 * %v `uname -r` of the running system
135 const Specifier table[] = {
136 { 'n', specifier_string, i->name },
137 { 'N', specifier_prefix_and_instance, NULL },
138 { 'p', specifier_prefix, NULL },
139 { 'i', specifier_instance, NULL },
141 { 'U', specifier_user_name, NULL },
142 { 'u', specifier_user_name, NULL },
144 { 'm', specifier_machine_id, NULL },
145 { 'H', specifier_host_name, NULL },
146 { 'b', specifier_boot_id, NULL },
147 { 'v', specifier_kernel_release, NULL },
155 return specifier_printf(format, table, i, ret);