]>
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 | ||
7584d236 ZJS |
22 | #include <stdlib.h> |
23 | ||
b5efdb8a | 24 | #include "alloc-util.h" |
b1d4f8e1 LP |
25 | #include "formats-util.h" |
26 | #include "install-printf.h" | |
7584d236 ZJS |
27 | #include "specifier.h" |
28 | #include "unit-name.h" | |
b1d4f8e1 | 29 | #include "user-util.h" |
3f0b2f0f | 30 | #include "util.h" |
7584d236 | 31 | |
19f6d710 | 32 | static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) { |
cab6235f | 33 | UnitFileInstallInfo *i = userdata; |
19f6d710 | 34 | |
7584d236 ZJS |
35 | assert(i); |
36 | ||
7410616c | 37 | return unit_name_to_prefix_and_instance(i->name, ret); |
7584d236 ZJS |
38 | } |
39 | ||
19f6d710 | 40 | static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) { |
cab6235f | 41 | UnitFileInstallInfo *i = userdata; |
19f6d710 | 42 | |
7584d236 ZJS |
43 | assert(i); |
44 | ||
7410616c | 45 | return unit_name_to_prefix(i->name, ret); |
7584d236 ZJS |
46 | } |
47 | ||
19f6d710 | 48 | static int specifier_instance(char specifier, void *data, void *userdata, char **ret) { |
cab6235f | 49 | UnitFileInstallInfo *i = userdata; |
7584d236 ZJS |
50 | char *instance; |
51 | int r; | |
52 | ||
53 | assert(i); | |
54 | ||
55 | r = unit_name_to_instance(i->name, &instance); | |
56 | if (r < 0) | |
19f6d710 LP |
57 | return r; |
58 | ||
59 | if (!instance) { | |
60 | instance = strdup(""); | |
61 | if (!instance) | |
62 | return -ENOMEM; | |
63 | } | |
64 | ||
65 | *ret = instance; | |
66 | return 0; | |
7584d236 ZJS |
67 | } |
68 | ||
19f6d710 | 69 | static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) { |
79413b67 | 70 | char *t; |
3f0b2f0f | 71 | |
79413b67 LP |
72 | /* If we are UID 0 (root), this will not result in NSS, |
73 | * otherwise it might. This is good, as we want to be able to | |
74 | * run this in PID 1, where our user ID is 0, but where NSS | |
75 | * lookups are not allowed. */ | |
3f0b2f0f | 76 | |
79413b67 LP |
77 | t = getusername_malloc(); |
78 | if (!t) | |
79 | return -ENOMEM; | |
19f6d710 | 80 | |
79413b67 | 81 | *ret = t; |
19f6d710 | 82 | return 0; |
3f0b2f0f ZJS |
83 | } |
84 | ||
79413b67 LP |
85 | static int specifier_user_id(char specifier, void *data, void *userdata, char **ret) { |
86 | ||
87 | if (asprintf(ret, UID_FMT, getuid()) < 0) | |
88 | return -ENOMEM; | |
89 | ||
90 | return 0; | |
91 | } | |
3f0b2f0f | 92 | |
cab6235f | 93 | int install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) { |
7584d236 ZJS |
94 | |
95 | /* This is similar to unit_full_printf() but does not support | |
96 | * anything path-related. | |
97 | * | |
98 | * %n: the full id of the unit (foo@bar.waldo) | |
99 | * %N: the id of the unit without the suffix (foo@bar) | |
100 | * %p: the prefix (foo) | |
101 | * %i: the instance (bar) | |
102 | ||
79413b67 LP |
103 | * %U the UID of the running user |
104 | * %u the username of running user | |
7584d236 ZJS |
105 | * %m the machine ID of the running system |
106 | * %H the host name of the running system | |
107 | * %b the boot ID of the running system | |
6aaa8c2f | 108 | * %v `uname -r` of the running system |
7584d236 ZJS |
109 | */ |
110 | ||
111 | const Specifier table[] = { | |
112 | { 'n', specifier_string, i->name }, | |
113 | { 'N', specifier_prefix_and_instance, NULL }, | |
114 | { 'p', specifier_prefix, NULL }, | |
115 | { 'i', specifier_instance, NULL }, | |
116 | ||
79413b67 | 117 | { 'U', specifier_user_id, NULL }, |
3f0b2f0f | 118 | { 'u', specifier_user_name, NULL }, |
7584d236 ZJS |
119 | |
120 | { 'm', specifier_machine_id, NULL }, | |
121 | { 'H', specifier_host_name, NULL }, | |
122 | { 'b', specifier_boot_id, NULL }, | |
6aaa8c2f ZJS |
123 | { 'v', specifier_kernel_release, NULL }, |
124 | {} | |
7584d236 ZJS |
125 | }; |
126 | ||
127 | assert(i); | |
128 | assert(format); | |
19f6d710 | 129 | assert(ret); |
7584d236 | 130 | |
19f6d710 | 131 | return specifier_printf(format, table, i, ret); |
7584d236 | 132 | } |