]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/install-printf.c
core: rename SystemdRunningAs to ManagerRunningAs
[thirdparty/systemd.git] / src / shared / install-printf.c
CommitLineData
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
24#include "specifier.h"
25#include "unit-name.h"
3f0b2f0f 26#include "util.h"
7584d236 27#include "install-printf.h"
6482f626 28#include "formats-util.h"
7584d236 29
19f6d710 30static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
7584d236 31 InstallInfo *i = userdata;
19f6d710 32
7584d236
ZJS
33 assert(i);
34
7410616c 35 return unit_name_to_prefix_and_instance(i->name, ret);
7584d236
ZJS
36}
37
19f6d710 38static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
7584d236 39 InstallInfo *i = userdata;
19f6d710 40
7584d236
ZJS
41 assert(i);
42
7410616c 43 return unit_name_to_prefix(i->name, ret);
7584d236
ZJS
44}
45
19f6d710 46static int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
7584d236
ZJS
47 InstallInfo *i = userdata;
48 char *instance;
49 int r;
50
51 assert(i);
52
53 r = unit_name_to_instance(i->name, &instance);
54 if (r < 0)
19f6d710
LP
55 return r;
56
57 if (!instance) {
58 instance = strdup("");
59 if (!instance)
60 return -ENOMEM;
61 }
62
63 *ret = instance;
64 return 0;
7584d236
ZJS
65}
66
19f6d710 67static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
3f0b2f0f
ZJS
68 InstallInfo *i = userdata;
69 const char *username;
7fd1b19b 70 _cleanup_free_ char *tmp = NULL;
3f0b2f0f
ZJS
71 char *printed = NULL;
72
73 assert(i);
74
75 if (i->user)
76 username = i->user;
77 else
78 /* get USER env from env or our own uid */
79 username = tmp = getusername_malloc();
80
81 switch (specifier) {
82 case 'u':
83 printed = strdup(username);
84 break;
85 case 'U': {
86 /* fish username from passwd */
87 uid_t uid;
88 int r;
89
90 r = get_user_creds(&username, &uid, NULL, NULL, NULL);
91 if (r < 0)
19f6d710 92 return r;
3f0b2f0f 93
1fa2f38f 94 if (asprintf(&printed, UID_FMT, uid) < 0)
19f6d710 95 return -ENOMEM;
3f0b2f0f
ZJS
96 break;
97 }}
98
19f6d710
LP
99
100 *ret = printed;
101 return 0;
3f0b2f0f
ZJS
102}
103
104
19f6d710 105int install_full_printf(InstallInfo *i, const char *format, char **ret) {
7584d236
ZJS
106
107 /* This is similar to unit_full_printf() but does not support
108 * anything path-related.
109 *
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)
114
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
6aaa8c2f 120 * %v `uname -r` of the running system
7584d236
ZJS
121 */
122
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 },
128
3f0b2f0f
ZJS
129 { 'U', specifier_user_name, NULL },
130 { 'u', specifier_user_name, NULL },
7584d236
ZJS
131
132 { 'm', specifier_machine_id, NULL },
133 { 'H', specifier_host_name, NULL },
134 { 'b', specifier_boot_id, NULL },
6aaa8c2f
ZJS
135 { 'v', specifier_kernel_release, NULL },
136 {}
7584d236
ZJS
137 };
138
139 assert(i);
140 assert(format);
19f6d710 141 assert(ret);
7584d236 142
19f6d710 143 return specifier_printf(format, table, i, ret);
7584d236 144}