d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek/***
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek This file is part of systemd.
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek Copyright 2015 Zbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek (at your option) any later version.
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek***/
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <errno.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <mntent.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <stdio.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <stdlib.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include <string.h>
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering#include "device-nodes.h"
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#include "fstab-util.h"
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include "macro.h"
4349cd7c1d153c4ffa23cf1cff1644e0afa9bcf0Lennart Poettering#include "mount-util.h"
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering#include "parse-util.h"
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers#include "path-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek#include "strv.h"
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek#include "util.h"
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sieversbool fstab_is_mount_point(const char *mount) {
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers _cleanup_endmntent_ FILE *f = NULL;
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers struct mntent *m;
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers f = setmntent("/etc/fstab", "r");
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers if (!f)
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers return false;
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers while ((m = getmntent(f)))
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers if (path_equal(m->mnt_dir, mount))
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers return true;
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers return false;
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers}
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmekint fstab_filter_options(const char *opts, const char *names,
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek const char **namefound, char **value, char **filtered) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek const char *name, *n = NULL, *x;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek _cleanup_strv_free_ char **stor = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek _cleanup_free_ char *v = NULL, **strv = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek assert(names && *names);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!opts)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek goto answer;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek /* If !value and !filtered, this function is not allowed to fail. */
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!filtered) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek const char *word, *state;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek size_t l;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek FOREACH_WORD_SEPARATOR(word, l, opts, ",", state)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek NULSTR_FOREACH(name, names) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (l < strlen(name))
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek continue;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!strneq(word, name, strlen(name)))
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek continue;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek /* we know that the string is NUL
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek * terminated, so *x is valid */
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek x = word + strlen(name);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (IN_SET(*x, '\0', '=', ',')) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek n = name;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (value) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek free(v);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (IN_SET(*x, '\0', ','))
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek v = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek else {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek assert(*x == '=');
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek x++;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek v = strndup(x, l - strlen(name) - 1);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!v)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return -ENOMEM;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek } else {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek char **t, **s;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek stor = strv_split(opts, ",");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!stor)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return -ENOMEM;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek strv = memdup(stor, sizeof(char*) * (strv_length(stor) + 1));
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!strv)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return -ENOMEM;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek for (s = t = strv; *s; s++) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek NULSTR_FOREACH(name, names) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek x = startswith(*s, name);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (x && IN_SET(*x, '\0', '='))
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek goto found;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek *t = *s;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek t++;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek continue;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek found:
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek /* Keep the last occurence found */
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek n = name;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (value) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek free(v);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (*x == '\0')
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek v = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek else {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek assert(*x == '=');
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek x++;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek v = strdup(x);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!v)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return -ENOMEM;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek *t = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmekanswer:
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (namefound)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek *namefound = n;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (filtered) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek char *f;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek f = strv_join(strv, ",");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (!f)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return -ENOMEM;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek *filtered = f;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (value) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek *value = v;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek v = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek }
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return !!n;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek}
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zakint fstab_extract_values(const char *opts, const char *name, char ***values) {
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak _cleanup_strv_free_ char **optsv = NULL, **res = NULL;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak char **s;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak assert(opts);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak assert(name);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak assert(values);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak optsv = strv_split(opts, ",");
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak if (!optsv)
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak return -ENOMEM;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak STRV_FOREACH(s, optsv) {
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak char *arg;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak int r;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak arg = startswith(*s, name);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak if (!arg || *arg != '=')
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak continue;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak r = strv_extend(&res, arg + 1);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak if (r < 0)
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak return r;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak }
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak *values = res;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak res = NULL;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak return !!*values;
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak}
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmekint fstab_find_pri(const char *options, int *ret) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek _cleanup_free_ char *opt = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek int r;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek unsigned pri;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek assert(ret);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = fstab_filter_options(options, "pri\0", NULL, &opt, NULL);
a75f4e2a02e287294b21ae9e5b1f28b2f8faea39Zbigniew Jędrzejewski-Szmek if (r < 0)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return r;
a75f4e2a02e287294b21ae9e5b1f28b2f8faea39Zbigniew Jędrzejewski-Szmek if (r == 0 || !opt)
a75f4e2a02e287294b21ae9e5b1f28b2f8faea39Zbigniew Jędrzejewski-Szmek return 0;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = safe_atou(opt, &pri);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (r < 0)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return r;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek
c5e04d51277994cca29234b33a6b8fc90a183cf3Zbigniew Jędrzejewski-Szmek if ((int) pri < 0)
c5e04d51277994cca29234b33a6b8fc90a183cf3Zbigniew Jędrzejewski-Szmek return -ERANGE;
c5e04d51277994cca29234b33a6b8fc90a183cf3Zbigniew Jędrzejewski-Szmek
a75f4e2a02e287294b21ae9e5b1f28b2f8faea39Zbigniew Jędrzejewski-Szmek *ret = (int) pri;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return 1;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek}
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poetteringstatic char *unquote(const char *s, const char* quotes) {
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering size_t l;
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering assert(s);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering /* This is rather stupid, simply removes the heading and
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering * trailing quotes if there is one. Doesn't care about
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering * escaping or anything.
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering *
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering * DON'T USE THIS FOR NEW CODE ANYMORE!*/
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering l = strlen(s);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (l < 2)
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return strdup(s);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (strchr(quotes, s[0]) && s[l-1] == s[0])
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return strndup(s+1, l-2);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return strdup(s);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering}
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poetteringstatic char *tag_to_udev_node(const char *tagvalue, const char *by) {
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering _cleanup_free_ char *t = NULL, *u = NULL;
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering size_t enc_len;
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering u = unquote(tagvalue, QUOTES);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (!u)
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return NULL;
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering enc_len = strlen(u) * 4 + 1;
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering t = new(char, enc_len);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (!t)
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return NULL;
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (encode_devnode_name(u, t, enc_len) < 0)
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return NULL;
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return strjoin("/dev/disk/by-", by, "/", t, NULL);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering}
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poetteringchar *fstab_node_to_udev_node(const char *p) {
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering assert(p);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (startswith(p, "LABEL="))
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return tag_to_udev_node(p+6, "label");
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (startswith(p, "UUID="))
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return tag_to_udev_node(p+5, "uuid");
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (startswith(p, "PARTUUID="))
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return tag_to_udev_node(p+9, "partuuid");
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering if (startswith(p, "PARTLABEL="))
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return tag_to_udev_node(p+10, "partlabel");
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering return strdup(p);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering}