install-printf.c revision cab6235f748e365198a7939f23c87ab3b8f59b2e
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdlib.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "specifier.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "unit-name.h"
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#include "util.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "install-printf.h"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#include "formats-util.h"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering UnitFileInstallInfo *i = userdata;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(i);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return unit_name_to_prefix_and_instance(i->name, ret);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering}
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering UnitFileInstallInfo *i = userdata;
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack assert(i);
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return unit_name_to_prefix(i->name, ret);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering}
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering UnitFileInstallInfo *i = userdata;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering char *instance;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering int r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering assert(i);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = unit_name_to_instance(i->name, &instance);
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering if (r < 0)
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering return r;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering
e0240c64b76ba8f0c9219feb23a5783f23100216Lennart Poettering if (!instance) {
e0240c64b76ba8f0c9219feb23a5783f23100216Lennart Poettering instance = strdup("");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (!instance)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return -ENOMEM;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering }
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering *ret = instance;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return 0;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering}
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering UnitFileInstallInfo *i = userdata;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering const char *username;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering _cleanup_free_ char *tmp = NULL;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering char *printed = NULL;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering assert(i);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering if (i->user)
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering username = i->user;
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering else
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering /* get USER env from env or our own uid */
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering username = tmp = getusername_malloc();
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering switch (specifier) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 'u':
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering printed = strdup(username);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering break;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case 'U': {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* fish username from passwd */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uid_t uid;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering int r;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering r = get_user_creds(&username, &uid, NULL, NULL, NULL);
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (r < 0)
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return r;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (asprintf(&printed, UID_FMT, uid) < 0)
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return -ENOMEM;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering break;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering }}
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering *ret = printed;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* This is similar to unit_full_printf() but does not support
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * anything path-related.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering * %n: the full id of the unit (foo@bar.waldo)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * %N: the id of the unit without the suffix (foo@bar)
ee3d6aff9bd73c1b23e29d1fa1fa6f7a1ef0533bLennart Poettering * %p: the prefix (foo)
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering * %i: the instance (bar)
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering * %U the UID of the configured user or running user
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering * %u the username of the configured user or running user
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering * %m the machine ID of the running system
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * %H the host name of the running system
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * %b the boot ID of the running system
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * %v `uname -r` of the running system
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering */
d75acfb059ece4512278b8820a9103664996f1e5Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const Specifier table[] = {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering { 'n', specifier_string, i->name },
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering { 'N', specifier_prefix_and_instance, NULL },
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering { 'p', specifier_prefix, NULL },
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering { 'i', specifier_instance, NULL },
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering { 'U', specifier_user_name, NULL },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering { 'u', specifier_user_name, NULL },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering { 'm', specifier_machine_id, NULL },
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering { 'H', specifier_host_name, NULL },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering { 'b', specifier_boot_id, NULL },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering { 'v', specifier_kernel_release, NULL },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering {}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(i);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek assert(format);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(ret);
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return specifier_printf(format, table, i, ret);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering