chiark / gitweb /
Classify processes from sessions into cgroups
[elogind.git] / src / shared / fstab-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2015 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
22 #include "fstab-util.h"
23 #include "strv.h"
24 #include "util.h"
25
26 int fstab_filter_options(const char *opts, const char *names,
27                          const char **namefound, char **value, char **filtered) {
28         const char *name, *n = NULL, *x;
29         _cleanup_strv_free_ char **stor = NULL;
30         _cleanup_free_ char *v = NULL, **strv = NULL;
31
32         assert(names && *names);
33
34         if (!opts)
35                 goto answer;
36
37         /* If !value and !filtered, this function is not allowed to fail. */
38
39         if (!filtered) {
40                 const char *word, *state;
41                 size_t l;
42
43                 FOREACH_WORD_SEPARATOR(word, l, opts, ",", state)
44                         NULSTR_FOREACH(name, names) {
45                                 if (l < strlen(name))
46                                         continue;
47                                 if (!strneq(word, name, strlen(name)))
48                                         continue;
49
50                                 /* we know that the string is NUL
51                                  * terminated, so *x is valid */
52                                 x = word + strlen(name);
53                                 if (IN_SET(*x, '\0', '=', ',')) {
54                                         n = name;
55                                         if (value) {
56                                                 free(v);
57                                                 if (IN_SET(*x, '\0', ','))
58                                                         v = NULL;
59                                                 else {
60                                                         assert(*x == '=');
61                                                         x++;
62                                                         v = strndup(x, l - strlen(name) - 1);
63                                                         if (!v)
64                                                                 return -ENOMEM;
65                                                 }
66                                         }
67                                 }
68                         }
69         } else {
70                 char **t, **s;
71
72                 stor = strv_split(opts, ",");
73                 if (!stor)
74                         return -ENOMEM;
75                 strv = memdup(stor, sizeof(char*) * (strv_length(stor) + 1));
76                 if (!strv)
77                         return -ENOMEM;
78
79                 for (s = t = strv; *s; s++) {
80                         NULSTR_FOREACH(name, names) {
81                                 x = startswith(*s, name);
82                                 if (x && IN_SET(*x, '\0', '='))
83                                         goto found;
84                         }
85
86                         *t = *s;
87                         t++;
88                         continue;
89                 found:
90                         /* Keep the last occurence found */
91                         n = name;
92                         if (value) {
93                                 free(v);
94                                 if (*x == '\0')
95                                         v = NULL;
96                                 else {
97                                         assert(*x == '=');
98                                         x++;
99                                         v = strdup(x);
100                                         if (!v)
101                                                 return -ENOMEM;
102                                 }
103                         }
104                 }
105                 *t = NULL;
106         }
107
108 answer:
109         if (namefound)
110                 *namefound = n;
111         if (filtered) {
112                 char *f;
113
114                 f = strv_join(strv, ",");
115                 if (!f)
116                         return -ENOMEM;
117
118                 *filtered = f;
119         }
120         if (value) {
121                 *value = v;
122                 v = NULL;
123         }
124
125         return !!n;
126 }
127
128 int fstab_extract_values(const char *opts, const char *name, char ***values) {
129         _cleanup_strv_free_ char **optsv = NULL, **res = NULL;
130         char **s;
131
132         assert(opts);
133         assert(name);
134         assert(values);
135
136         optsv = strv_split(opts, ",");
137         if (!optsv)
138                 return -ENOMEM;
139
140         STRV_FOREACH(s, optsv) {
141                 char *arg;
142                 int r;
143
144                 arg = startswith(*s, name);
145                 if (!arg || *arg != '=')
146                         continue;
147                 r = strv_extend(&res, arg + 1);
148                 if (r < 0)
149                         return r;
150         }
151
152         *values = res;
153         res = NULL;
154
155         return !!*values;
156 }
157
158 int fstab_find_pri(const char *options, int *ret) {
159         _cleanup_free_ char *opt = NULL;
160         int r;
161         unsigned pri;
162
163         assert(ret);
164
165         r = fstab_filter_options(options, "pri\0", NULL, &opt, NULL);
166         if (r < 0)
167                 return r;
168         if (r == 0 || !opt)
169                 return 0;
170
171         r = safe_atou(opt, &pri);
172         if (r < 0)
173                 return r;
174
175         if ((int) pri < 0)
176                 return -ERANGE;
177
178         *ret = (int) pri;
179         return 1;
180 }