]>
Commit | Line | Data |
---|---|---|
7584d236 ZJS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Zbigniew Jędrzejewski-Szmek | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <assert.h> | |
23 | #include <stdlib.h> | |
24 | ||
25 | #include "specifier.h" | |
26 | #include "unit-name.h" | |
3f0b2f0f | 27 | #include "util.h" |
7584d236 ZJS |
28 | #include "install-printf.h" |
29 | ||
30 | static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) { | |
31 | InstallInfo *i = userdata; | |
32 | assert(i); | |
33 | ||
34 | return unit_name_to_prefix_and_instance(i->name); | |
35 | } | |
36 | ||
37 | static char *specifier_prefix(char specifier, void *data, void *userdata) { | |
38 | InstallInfo *i = userdata; | |
39 | assert(i); | |
40 | ||
41 | return unit_name_to_prefix(i->name); | |
42 | } | |
43 | ||
44 | static char *specifier_instance(char specifier, void *data, void *userdata) { | |
45 | InstallInfo *i = userdata; | |
46 | char *instance; | |
47 | int r; | |
48 | ||
49 | assert(i); | |
50 | ||
51 | r = unit_name_to_instance(i->name, &instance); | |
52 | if (r < 0) | |
53 | return NULL; | |
54 | if (instance != NULL) | |
55 | return instance; | |
56 | else | |
57 | return strdup(""); | |
58 | } | |
59 | ||
3f0b2f0f ZJS |
60 | static char *specifier_user_name(char specifier, void *data, void *userdata) { |
61 | InstallInfo *i = userdata; | |
62 | const char *username; | |
7fd1b19b | 63 | _cleanup_free_ char *tmp = NULL; |
3f0b2f0f ZJS |
64 | char *printed = NULL; |
65 | ||
66 | assert(i); | |
67 | ||
68 | if (i->user) | |
69 | username = i->user; | |
70 | else | |
71 | /* get USER env from env or our own uid */ | |
72 | username = tmp = getusername_malloc(); | |
73 | ||
74 | switch (specifier) { | |
75 | case 'u': | |
76 | printed = strdup(username); | |
77 | break; | |
78 | case 'U': { | |
79 | /* fish username from passwd */ | |
80 | uid_t uid; | |
81 | int r; | |
82 | ||
83 | r = get_user_creds(&username, &uid, NULL, NULL, NULL); | |
84 | if (r < 0) | |
85 | return NULL; | |
86 | ||
87 | if (asprintf(&printed, "%d", uid) < 0) | |
88 | return NULL; | |
89 | break; | |
90 | }} | |
91 | ||
92 | return printed; | |
93 | } | |
94 | ||
95 | ||
7584d236 ZJS |
96 | char *install_full_printf(InstallInfo *i, const char *format) { |
97 | ||
98 | /* This is similar to unit_full_printf() but does not support | |
99 | * anything path-related. | |
100 | * | |
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) | |
105 | ||
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 | |
6aaa8c2f | 111 | * %v `uname -r` of the running system |
7584d236 ZJS |
112 | */ |
113 | ||
114 | const Specifier table[] = { | |
115 | { 'n', specifier_string, i->name }, | |
116 | { 'N', specifier_prefix_and_instance, NULL }, | |
117 | { 'p', specifier_prefix, NULL }, | |
118 | { 'i', specifier_instance, NULL }, | |
119 | ||
3f0b2f0f ZJS |
120 | { 'U', specifier_user_name, NULL }, |
121 | { 'u', specifier_user_name, NULL }, | |
7584d236 ZJS |
122 | |
123 | { 'm', specifier_machine_id, NULL }, | |
124 | { 'H', specifier_host_name, NULL }, | |
125 | { 'b', specifier_boot_id, NULL }, | |
6aaa8c2f ZJS |
126 | { 'v', specifier_kernel_release, NULL }, |
127 | {} | |
7584d236 ZJS |
128 | }; |
129 | ||
130 | assert(i); | |
131 | assert(format); | |
132 | ||
133 | return specifier_printf(format, table, i); | |
134 | } |