7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek/***
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek This file is part of systemd.
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek Copyright 2013 Zbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek (at your option) any later version.
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek***/
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <errno.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <stdio.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <string.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <unistd.h>
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "formats-util.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "install-printf.h"
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include "install.h"
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include "macro.h"
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek#include "specifier.h"
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek#include "unit-name.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "user-util.h"
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
cab6235f748e365198a7939f23c87ab3b8f59b2eLennart Poettering UnitFileInstallInfo *i = userdata;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek assert(i);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering return unit_name_to_prefix_and_instance(i->name, ret);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek}
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
cab6235f748e365198a7939f23c87ab3b8f59b2eLennart Poettering UnitFileInstallInfo *i = userdata;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek assert(i);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering return unit_name_to_prefix(i->name, ret);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek}
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic int specifier_instance(char specifier, void *data, void *userdata, char **ret) {
cab6235f748e365198a7939f23c87ab3b8f59b2eLennart Poettering UnitFileInstallInfo *i = userdata;
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek char *instance;
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek int r;
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek assert(i);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek r = unit_name_to_instance(i->name, &instance);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek if (r < 0)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return r;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (!instance) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering instance = strdup("");
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (!instance)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -ENOMEM;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering }
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering *ret = instance;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return 0;
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek}
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering char *t;
3f0b2f0f452e94444e4fb7b62030ea05738bb1b6Zbigniew Jędrzejewski-Szmek
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering /* If we are UID 0 (root), this will not result in NSS,
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering * otherwise it might. This is good, as we want to be able to
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering * run this in PID 1, where our user ID is 0, but where NSS
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering * lookups are not allowed. */
3f0b2f0f452e94444e4fb7b62030ea05738bb1b6Zbigniew Jędrzejewski-Szmek
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering t = getusername_malloc();
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (!t)
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return -ENOMEM;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering *ret = t;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return 0;
3f0b2f0f452e94444e4fb7b62030ea05738bb1b6Zbigniew Jędrzejewski-Szmek}
3f0b2f0f452e94444e4fb7b62030ea05738bb1b6Zbigniew Jędrzejewski-Szmek
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poetteringstatic int specifier_user_id(char specifier, void *data, void *userdata, char **ret) {
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (asprintf(ret, UID_FMT, getuid()) < 0)
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return -ENOMEM;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return 0;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering}
3f0b2f0f452e94444e4fb7b62030ea05738bb1b6Zbigniew Jędrzejewski-Szmek
cab6235f748e365198a7939f23c87ab3b8f59b2eLennart Poetteringint install_full_printf(UnitFileInstallInfo *i, const char *format, char **ret) {
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek /* This is similar to unit_full_printf() but does not support
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * anything path-related.
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek *
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * %n: the full id of the unit (foo@bar.waldo)
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * %N: the id of the unit without the suffix (foo@bar)
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * %p: the prefix (foo)
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * %i: the instance (bar)
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering * %U the UID of the running user
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering * %u the username of running user
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * %m the machine ID of the running system
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * %H the host name of the running system
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek * %b the boot ID of the running system
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek * %v `uname -r` of the running system
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek */
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek const Specifier table[] = {
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { 'n', specifier_string, i->name },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { 'N', specifier_prefix_and_instance, NULL },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { 'p', specifier_prefix, NULL },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { 'i', specifier_instance, NULL },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering { 'U', specifier_user_id, NULL },
3f0b2f0f452e94444e4fb7b62030ea05738bb1b6Zbigniew Jędrzejewski-Szmek { 'u', specifier_user_name, NULL },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { 'm', specifier_machine_id, NULL },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { 'H', specifier_host_name, NULL },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { 'b', specifier_boot_id, NULL },
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek { 'v', specifier_kernel_release, NULL },
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek {}
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek };
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek assert(i);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek assert(format);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering assert(ret);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return specifier_printf(format, table, i, ret);
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek}