proc-cmdline.c revision 11c3a36649e5e5e77db499c92f3cdcbd619efd3a
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 2010 Lennart Poettering
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 <stdbool.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stddef.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <string.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#include "alloc-util.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "extract-word.h"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#include "fileio.h"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering#include "macro.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "parse-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "proc-cmdline.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "process-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "special.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "string-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "virt.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint proc_cmdline(char **ret) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(ret);
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek if (detect_container() > 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return get_process_cmdline(1, 0, false, ret);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return read_one_line_file("/proc/cmdline", ret);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint parse_proc_cmdline(int (*parse_item)(const char *key, const char *value)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *line = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *p;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(parse_item);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = proc_cmdline(&line);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering p = line;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (;;) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *word = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *value = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r == 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering break;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering /* Filter out arguments that are intended only for the
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * initrd */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!in_initrd() && startswith(word, "rd."))
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering value = strchr(word, '=');
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (value)
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering *(value++) = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = parse_item(word, value);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint get_proc_cmdline_key(const char *key, char **value) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *line = NULL, *ret = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool found = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *p;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(key);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = proc_cmdline(&line);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r < 0)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return r;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering p = line;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering for (;;) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering _cleanup_free_ char *word = NULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering const char *e;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r == 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek break;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Filter out arguments that are intended only for the
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * initrd */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!in_initrd() && startswith(word, "rd."))
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek continue;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (value) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek e = startswith(word, key);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!e)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek continue;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = free_and_strdup(&ret, e);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen found = true;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen } else {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (streq(word, key))
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen found = true;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen }
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (value) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering *value = ret;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen ret = NULL;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen }
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return found;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekint shall_restore_state(void) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *value = NULL;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek int r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = get_proc_cmdline_key("systemd.restore_state=", &value);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r == 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return true;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return parse_boolean(value);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekstatic const char * const rlmap[] = {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "emergency", SPECIAL_EMERGENCY_TARGET,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "-b", SPECIAL_EMERGENCY_TARGET,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "rescue", SPECIAL_RESCUE_TARGET,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "single", SPECIAL_RESCUE_TARGET,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek "-s", SPECIAL_RESCUE_TARGET,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "s", SPECIAL_RESCUE_TARGET,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "S", SPECIAL_RESCUE_TARGET,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "1", SPECIAL_RESCUE_TARGET,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "2", SPECIAL_MULTI_USER_TARGET,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen "3", SPECIAL_MULTI_USER_TARGET,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen "4", SPECIAL_MULTI_USER_TARGET,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen "5", SPECIAL_GRAPHICAL_TARGET,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen};
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenconst char* runlevel_to_target(const char *word) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen size_t i;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (!word)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return NULL;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen for (i = 0; i < ELEMENTSOF(rlmap); i += 2)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (streq(word, rlmap[i]))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return rlmap[i+1];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering