2 This file is part of systemd.
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "formats-util.h"
26 #include "install-printf.h"
29 #include "specifier.h"
30 #include "unit-name.h"
31 #include "user-util.h"
33 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
34 UnitFileInstallInfo
*i
= userdata
;
38 return unit_name_to_prefix_and_instance(i
->name
, ret
);
41 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
42 UnitFileInstallInfo
*i
= userdata
;
46 return unit_name_to_prefix(i
->name
, ret
);
49 static int specifier_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
50 UnitFileInstallInfo
*i
= userdata
;
56 r
= unit_name_to_instance(i
->name
, &instance
);
61 instance
= strdup("");
70 static int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
73 /* If we are UID 0 (root), this will not result in NSS,
74 * otherwise it might. This is good, as we want to be able to
75 * run this in PID 1, where our user ID is 0, but where NSS
76 * lookups are not allowed. */
78 t
= getusername_malloc();
86 static int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
88 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
94 int install_full_printf(UnitFileInstallInfo
*i
, const char *format
, char **ret
) {
96 /* This is similar to unit_full_printf() but does not support
97 * anything path-related.
99 * %n: the full id of the unit (foo@bar.waldo)
100 * %N: the id of the unit without the suffix (foo@bar)
101 * %p: the prefix (foo)
102 * %i: the instance (bar)
104 * %U the UID of the running user
105 * %u the username of running user
106 * %m the machine ID of the running system
107 * %H the host name of the running system
108 * %b the boot ID of the running system
109 * %v `uname -r` of the running system
112 const Specifier table
[] = {
113 { 'n', specifier_string
, i
->name
},
114 { 'N', specifier_prefix_and_instance
, NULL
},
115 { 'p', specifier_prefix
, NULL
},
116 { 'i', specifier_instance
, NULL
},
118 { 'U', specifier_user_id
, NULL
},
119 { 'u', specifier_user_name
, NULL
},
121 { 'm', specifier_machine_id
, NULL
},
122 { 'H', specifier_host_name
, NULL
},
123 { 'b', specifier_boot_id
, NULL
},
124 { 'v', specifier_kernel_release
, NULL
},
132 return specifier_printf(format
, table
, i
, ret
);