]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-util.c
2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
33 #include <sys/param.h>
35 #include "device-nodes.h"
37 #include "libudev-private.h"
39 #include "MurmurHash2.h"
42 * SECTION:libudev-util
43 * @short_description: utils
45 * Utilities useful when dealing with devices and device node names.
48 int util_delete_path(struct udev
*udev
, const char *path
)
50 char p
[UTIL_PATH_SIZE
];
57 strscpy(p
, sizeof(p
), path
);
58 pos
= strrchr(p
, '/');
59 if (pos
== p
|| pos
== NULL
)
64 pos
= strrchr(p
, '/');
66 /* don't remove the last one */
67 if ((pos
== p
) || (pos
== NULL
))
80 uid_t
util_lookup_user(struct udev
*udev
, const char *user
)
86 size_t buflen
= sysconf(_SC_GETPW_R_SIZE_MAX
);
87 char *buf
= alloca(buflen
);
89 if (streq(user
, "root"))
91 uid
= strtoul(user
, &endptr
, 10);
92 if (endptr
[0] == '\0')
95 errno
= getpwnam_r(user
, &pwbuf
, buf
, buflen
, &pw
);
98 if (errno
== 0 || errno
== ENOENT
|| errno
== ESRCH
)
99 udev_err(udev
, "specified user '%s' unknown\n", user
);
101 udev_err(udev
, "error resolving user '%s': %m\n", user
);
105 gid_t
util_lookup_group(struct udev
*udev
, const char *group
)
111 size_t buflen
= sysconf(_SC_GETPW_R_SIZE_MAX
);
114 if (streq(group
, "root"))
116 gid
= strtoul(group
, &endptr
, 10);
117 if (endptr
[0] == '\0')
123 newbuf
= realloc(buf
, buflen
);
127 errno
= getgrnam_r(group
, &grbuf
, buf
, buflen
, &gr
);
130 } else if (errno
== ERANGE
) {
133 } else if (errno
== 0 || errno
== ENOENT
|| errno
== ESRCH
) {
134 udev_err(udev
, "specified group '%s' unknown\n", group
);
136 udev_err(udev
, "error resolving group '%s': %m\n", group
);
144 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
145 int util_resolve_subsys_kernel(struct udev
*udev
, const char *string
,
146 char *result
, size_t maxsize
, int read_value
)
148 char temp
[UTIL_PATH_SIZE
];
151 struct udev_device
*dev
;
154 if (string
[0] != '[')
157 strscpy(temp
, sizeof(temp
), string
);
161 sysname
= strchr(subsys
, '/');
165 sysname
= &sysname
[1];
167 attr
= strchr(sysname
, ']');
177 if (read_value
&& attr
== NULL
)
180 dev
= udev_device_new_from_subsystem_sysname(udev
, subsys
, sysname
);
187 val
= udev_device_get_sysattr_value(dev
, attr
);
189 strscpy(result
, maxsize
, val
);
192 udev_dbg(udev
, "value '[%s/%s]%s' is '%s'\n", subsys
, sysname
, attr
, result
);
198 l
= strpcpyl(&s
, maxsize
, udev_device_get_syspath(dev
), NULL
);
200 strpcpyl(&s
, l
, "/", attr
, NULL
);
201 udev_dbg(udev
, "path '[%s/%s]%s' is '%s'\n", subsys
, sysname
, attr
, result
);
203 udev_device_unref(dev
);
207 ssize_t
util_get_sys_core_link_value(struct udev
*udev
, const char *slink
, const char *syspath
, char *value
, size_t size
)
209 char path
[UTIL_PATH_SIZE
];
210 char target
[UTIL_PATH_SIZE
];
214 strscpyl(path
, sizeof(path
), syspath
, "/", slink
, NULL
);
215 len
= readlink(path
, target
, sizeof(target
));
216 if (len
<= 0 || len
== (ssize_t
)sizeof(target
))
219 pos
= strrchr(target
, '/');
223 return strscpy(value
, size
, pos
);
226 int util_resolve_sys_link(struct udev
*udev
, char *syspath
, size_t size
)
228 char link_target
[UTIL_PATH_SIZE
];
235 len
= readlink(syspath
, link_target
, sizeof(link_target
));
236 if (len
<= 0 || len
== (ssize_t
)sizeof(link_target
))
238 link_target
[len
] = '\0';
240 for (back
= 0; startswith(&link_target
[back
* 3], "../"); back
++)
242 for (i
= 0; i
<= back
; i
++) {
243 base
= strrchr(syspath
, '/');
249 strscpyl(base
, size
- (base
- syspath
), "/", &link_target
[back
* 3], NULL
);
253 int util_log_priority(const char *priority
)
258 prio
= strtol(priority
, &endptr
, 10);
259 if (endptr
[0] == '\0' || isspace(endptr
[0]))
261 if (startswith(priority
, "err"))
263 if (startswith(priority
, "info"))
265 if (startswith(priority
, "debug"))
270 size_t util_path_encode(const char *src
, char *dest
, size_t size
)
274 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
280 memcpy(&dest
[j
], "\\x2f", 4);
282 } else if (src
[i
] == '\\') {
287 memcpy(&dest
[j
], "\\x5c", 4);
302 void util_remove_trailing_chars(char *path
, char c
)
309 while (len
> 0 && path
[len
-1] == c
)
313 int util_replace_whitespace(const char *str
, char *to
, size_t len
)
317 /* strip trailing whitespace */
318 len
= strnlen(str
, len
);
319 while (len
&& isspace(str
[len
-1]))
322 /* strip leading whitespace */
324 while (isspace(str
[i
]) && (i
< len
))
329 /* substitute multiple whitespace with a single '_' */
330 if (isspace(str
[i
])) {
331 while (isspace(str
[i
]))
341 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
342 int util_replace_chars(char *str
, const char *white
)
347 while (str
[i
] != '\0') {
350 if (whitelisted_char_for_devnode(str
[i
], white
)) {
355 /* accept hex encoding */
356 if (str
[i
] == '\\' && str
[i
+1] == 'x') {
361 /* accept valid utf8 */
362 len
= utf8_encoded_valid_unichar(&str
[i
]);
368 /* if space is allowed, replace whitespace with ordinary space */
369 if (isspace(str
[i
]) && white
!= NULL
&& strchr(white
, ' ') != NULL
) {
376 /* everything else is replaced with '_' */
385 * udev_util_encode_string:
386 * @str: input string to be encoded
387 * @str_enc: output string to store the encoded input string
388 * @len: maximum size of the output string, which may be
389 * four times as long as the input string
391 * Encode all potentially unsafe characters of a string to the
392 * corresponding 2 char hex value prefixed by '\x'.
394 * Returns: 0 if the entire string was copied, non-zero otherwise.
396 _public_
int udev_util_encode_string(const char *str
, char *str_enc
, size_t len
)
398 return encode_devnode_name(str
, str_enc
, len
);
401 unsigned int util_string_hash32(const char *str
)
403 return MurmurHash2(str
, strlen(str
), 0);
406 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
407 uint64_t util_string_bloom64(const char *str
)
410 unsigned int hash
= util_string_hash32(str
);
412 bits
|= 1LLU << (hash
& 63);
413 bits
|= 1LLU << ((hash
>> 6) & 63);
414 bits
|= 1LLU << ((hash
>> 12) & 63);
415 bits
|= 1LLU << ((hash
>> 18) & 63);