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 "string-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
34 static int specifier_prefix_and_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
35 UnitFileInstallInfo
*i
= userdata
;
39 return unit_name_to_prefix_and_instance(i
->name
, ret
);
42 static int specifier_prefix(char specifier
, void *data
, void *userdata
, char **ret
) {
43 UnitFileInstallInfo
*i
= userdata
;
47 return unit_name_to_prefix(i
->name
, ret
);
50 static int specifier_instance(char specifier
, void *data
, void *userdata
, char **ret
) {
51 const UnitFileInstallInfo
*i
= userdata
;
57 r
= unit_name_to_instance(i
->name
, &instance
);
61 if (isempty(instance
)) {
62 instance
= strdup(i
->default_instance
?: "");
71 static int specifier_user_name(char specifier
, void *data
, void *userdata
, char **ret
) {
74 /* If we are UID 0 (root), this will not result in NSS,
75 * otherwise it might. This is good, as we want to be able to
76 * run this in PID 1, where our user ID is 0, but where NSS
77 * lookups are not allowed. */
79 t
= getusername_malloc();
87 static int specifier_user_id(char specifier
, void *data
, void *userdata
, char **ret
) {
89 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
95 int install_full_printf(UnitFileInstallInfo
*i
, const char *format
, char **ret
) {
97 /* This is similar to unit_full_printf() but does not support
98 * anything path-related.
100 * %n: the full id of the unit (foo@bar.waldo)
101 * %N: the id of the unit without the suffix (foo@bar)
102 * %p: the prefix (foo)
103 * %i: the instance (bar)
105 * %U the UID of the running user
106 * %u the username of running user
107 * %m the machine ID of the running system
108 * %H the host name of the running system
109 * %b the boot ID of the running system
110 * %v `uname -r` 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_id
, 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
},
125 { 'v', specifier_kernel_release
, NULL
},
133 return specifier_printf(format
, table
, i
, ret
);