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/>.
24 #include "specifier.h"
25 #include "unit-name.h"
27 #include "install-printf.h"
28 #include "formats-util.h"
30 static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
31 UnitFileInstallInfo *i = userdata;
35 return unit_name_to_prefix_and_instance(i->name, ret);
38 static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
39 UnitFileInstallInfo *i = userdata;
43 return unit_name_to_prefix(i->name, ret);
46 static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
47 UnitFileInstallInfo *i = userdata;
53 r = unit_name_to_instance(i->name, &instance);
58 instance = strdup("");
67 static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
68 UnitFileInstallInfo *i = userdata;
70 _cleanup_free_ char *tmp = NULL;
78 /* get USER env from env or our own uid */
79 username = tmp = getusername_malloc();
83 printed = strdup(username);
86 /* fish username from passwd */
90 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
94 if (asprintf(&printed, UID_FMT, uid) < 0)
105 int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
107 /* This is similar to unit_full_printf() but does not support
108 * anything path-related.
110 * %n: the full id of the unit (foo@bar.waldo)
111 * %N: the id of the unit without the suffix (foo@bar)
112 * %p: the prefix (foo)
113 * %i: the instance (bar)
115 * %U the UID of the configured user or running user
116 * %u the username of the configured user or running user
117 * %m the machine ID of the running system
118 * %H the host name of the running system
119 * %b the boot ID of the running system
120 * %v `uname -r` of the running system
123 const Specifier table[] = {
124 { 'n', specifier_string, i->name },
125 { 'N', specifier_prefix_and_instance, NULL },
126 { 'p', specifier_prefix, NULL },
127 { 'i', specifier_instance, NULL },
129 { 'U', specifier_user_name, NULL },
130 { 'u', specifier_user_name, NULL },
132 { 'm', specifier_machine_id, NULL },
133 { 'H', specifier_host_name, NULL },
134 { 'b', specifier_boot_id, NULL },
135 { 'v', specifier_kernel_release, NULL },
143 return specifier_printf(format, table, i, ret);