2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2015-10-24 22:58:24 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stddef.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-01-26 15:52:18 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "macro.h"
|
|
|
|
|
2015-11-03 12:25:29 +01:00
|
|
|
/* What is interpreted as whitespace? */
|
|
|
|
#define WHITESPACE " \t\n\r"
|
|
|
|
#define NEWLINE "\n\r"
|
|
|
|
#define QUOTES "\"\'"
|
|
|
|
#define COMMENTS "#;"
|
|
|
|
#define GLOB_CHARS "*?["
|
|
|
|
#define DIGITS "0123456789"
|
|
|
|
#define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
#define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
#define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
|
|
|
|
#define ALPHANUMERICAL LETTERS DIGITS
|
2016-05-17 15:44:32 +02:00
|
|
|
#define HEXDIGITS DIGITS "abcdefABCDEF"
|
2015-11-03 12:25:29 +01:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#define streq(a,b) (strcmp((a),(b)) == 0)
|
|
|
|
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
|
|
|
|
#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
|
|
|
|
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
|
|
|
|
|
|
|
|
int strcmp_ptr(const char *a, const char *b) _pure_;
|
2020-06-02 17:31:34 +02:00
|
|
|
int strcasecmp_ptr(const char *a, const char *b) _pure_;
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
static inline bool streq_ptr(const char *a, const char *b) {
|
|
|
|
return strcmp_ptr(a, b) == 0;
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:29:00 +02:00
|
|
|
static inline char* strstr_ptr(const char *haystack, const char *needle) {
|
|
|
|
if (!haystack || !needle)
|
|
|
|
return NULL;
|
|
|
|
return strstr(haystack, needle);
|
|
|
|
}
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
static inline const char* strempty(const char *s) {
|
2018-01-10 17:11:19 +01:00
|
|
|
return s ?: "";
|
2015-10-24 22:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char* strnull(const char *s) {
|
2018-01-10 17:11:19 +01:00
|
|
|
return s ?: "(null)";
|
2015-10-24 22:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *strna(const char *s) {
|
2018-01-10 17:11:19 +01:00
|
|
|
return s ?: "n/a";
|
2015-10-24 22:58:24 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 13:10:49 +02:00
|
|
|
static inline const char* yes_no(bool b) {
|
|
|
|
return b ? "yes" : "no";
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char* true_false(bool b) {
|
|
|
|
return b ? "true" : "false";
|
|
|
|
}
|
|
|
|
|
2020-10-11 16:20:27 +02:00
|
|
|
static inline const char* plus_minus(bool b) {
|
|
|
|
return b ? "+" : "-";
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:10:49 +02:00
|
|
|
static inline const char* one_zero(bool b) {
|
|
|
|
return b ? "1" : "0";
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char* enable_disable(bool b) {
|
|
|
|
return b ? "enable" : "disable";
|
|
|
|
}
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
static inline bool isempty(const char *p) {
|
|
|
|
return !p || !p[0];
|
|
|
|
}
|
|
|
|
|
2016-05-30 17:59:43 +02:00
|
|
|
static inline const char *empty_to_null(const char *p) {
|
|
|
|
return isempty(p) ? NULL : p;
|
|
|
|
}
|
|
|
|
|
2018-05-10 18:55:46 +02:00
|
|
|
static inline const char *empty_to_dash(const char *str) {
|
2016-08-31 20:06:57 +02:00
|
|
|
return isempty(str) ? "-" : str;
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:03:33 +02:00
|
|
|
static inline bool empty_or_dash(const char *str) {
|
|
|
|
return !str ||
|
|
|
|
str[0] == 0 ||
|
|
|
|
(str[0] == '-' && str[1] == 0);
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:11:11 +02:00
|
|
|
static inline const char *empty_or_dash_to_null(const char *p) {
|
|
|
|
return empty_or_dash(p) ? NULL : p;
|
|
|
|
}
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
static inline char *startswith(const char *s, const char *prefix) {
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(prefix);
|
|
|
|
if (strncmp(s, prefix, l) == 0)
|
|
|
|
return (char*) s + l;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *startswith_no_case(const char *s, const char *prefix) {
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(prefix);
|
|
|
|
if (strncasecmp(s, prefix, l) == 0)
|
|
|
|
return (char*) s + l;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *endswith(const char *s, const char *postfix) _pure_;
|
|
|
|
char *endswith_no_case(const char *s, const char *postfix) _pure_;
|
|
|
|
|
|
|
|
char *first_word(const char *s, const char *word) _pure_;
|
|
|
|
|
|
|
|
char *strnappend(const char *s, const char *suffix, size_t length);
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
char *strjoin_real(const char *x, ...) _sentinel_;
|
|
|
|
#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
#define strjoina(a, ...) \
|
|
|
|
({ \
|
|
|
|
const char *_appendees_[] = { a, __VA_ARGS__ }; \
|
|
|
|
char *_d_, *_p_; \
|
2020-07-30 17:00:25 +02:00
|
|
|
size_t _len_ = 0; \
|
|
|
|
size_t _i_; \
|
2015-10-24 22:58:24 +02:00
|
|
|
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
|
|
|
|
_len_ += strlen(_appendees_[_i_]); \
|
2019-01-26 15:52:18 +01:00
|
|
|
_p_ = _d_ = newa(char, _len_ + 1); \
|
2015-10-24 22:58:24 +02:00
|
|
|
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
|
|
|
|
_p_ = stpcpy(_p_, _appendees_[_i_]); \
|
|
|
|
*_p_ = 0; \
|
|
|
|
_d_; \
|
|
|
|
})
|
|
|
|
|
|
|
|
char *strstrip(char *s);
|
|
|
|
char *delete_chars(char *s, const char *bad);
|
2017-11-09 11:12:47 +01:00
|
|
|
char *delete_trailing_chars(char *s, const char *bad);
|
2015-10-24 22:58:24 +02:00
|
|
|
char *truncate_nl(char *s);
|
|
|
|
|
2017-11-09 11:12:47 +01:00
|
|
|
static inline char *skip_leading_chars(const char *s, const char *bad) {
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!bad)
|
|
|
|
bad = WHITESPACE;
|
|
|
|
|
|
|
|
return (char*) s + strspn(s, bad);
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:43:26 +01:00
|
|
|
char ascii_tolower(char x);
|
|
|
|
char *ascii_strlower(char *s);
|
|
|
|
char *ascii_strlower_n(char *s, size_t n);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2016-07-19 20:43:54 +02:00
|
|
|
char ascii_toupper(char x);
|
|
|
|
char *ascii_strupper(char *s);
|
|
|
|
|
2016-01-13 02:21:16 +01:00
|
|
|
int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
|
2016-01-13 19:45:05 +01:00
|
|
|
int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
|
2016-01-13 02:21:16 +01:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
bool chars_intersect(const char *a, const char *b) _pure_;
|
|
|
|
|
|
|
|
static inline bool _pure_ in_charset(const char *s, const char* charset) {
|
|
|
|
assert(s);
|
|
|
|
assert(charset);
|
|
|
|
return s[strspn(s, charset)] == '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string_has_cc(const char *p, const char *ok) _pure_;
|
|
|
|
|
|
|
|
char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
|
2018-06-02 17:52:07 +02:00
|
|
|
static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
|
|
|
|
return ellipsize_mem(s, strlen(s), length, percent);
|
|
|
|
}
|
|
|
|
|
2018-05-17 10:55:21 +02:00
|
|
|
char *cellescape(char *buf, size_t len, const char *s);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2018-05-17 11:09:07 +02:00
|
|
|
/* This limit is arbitrary, enough to give some idea what the string contains */
|
|
|
|
#define CELLESCAPE_DEFAULT_LENGTH 64
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
char* strshorten(char *s, size_t l);
|
|
|
|
|
|
|
|
char *strreplace(const char *text, const char *old_string, const char *new_string);
|
|
|
|
|
2018-01-27 13:00:09 +01:00
|
|
|
char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2021-01-05 15:03:41 +01:00
|
|
|
char *strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_;
|
2017-11-28 16:37:53 +01:00
|
|
|
|
2021-01-05 15:03:41 +01:00
|
|
|
#define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
|
|
|
|
#define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
char *strrep(const char *s, unsigned n);
|
|
|
|
|
|
|
|
int split_pair(const char *s, const char *sep, char **l, char **r);
|
|
|
|
|
|
|
|
int free_and_strdup(char **p, const char *s);
|
2019-03-21 10:54:24 +01:00
|
|
|
static inline int free_and_strdup_warn(char **p, const char *s) {
|
|
|
|
if (free_and_strdup(p, s) < 0)
|
|
|
|
return log_oom();
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-09 07:03:01 +02:00
|
|
|
int free_and_strndup(char **p, const char *s, size_t l);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-26 21:26:33 +01:00
|
|
|
bool string_is_safe(const char *p) _pure_;
|
2017-07-20 11:38:15 +02:00
|
|
|
|
|
|
|
static inline size_t strlen_ptr(const char *s) {
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return strlen(s);
|
|
|
|
}
|
2018-05-30 13:07:37 +02:00
|
|
|
|
2019-05-07 15:10:58 +02:00
|
|
|
DISABLE_WARNING_STRINGOP_TRUNCATION;
|
|
|
|
static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
|
|
|
|
strncpy(buf, src, buf_len);
|
|
|
|
}
|
|
|
|
REENABLE_WARNING;
|
|
|
|
|
2018-05-30 13:07:37 +02:00
|
|
|
/* Like startswith(), but operates on arbitrary memory blocks */
|
|
|
|
static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
|
|
|
|
assert(token);
|
|
|
|
|
2020-07-30 17:00:25 +02:00
|
|
|
size_t n = strlen(token);
|
2018-05-30 13:07:37 +02:00
|
|
|
if (sz < n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
if (memcmp(p, token, n) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (uint8_t*) p + n;
|
|
|
|
}
|
importd, basic/string-util: use case-insensitive comparison for HTTP headers
According to RFC2616[1], HTTP header names are case-insensitive. So
it's totally valid to have a header starting with either `Date:` or
`date:`.
However, when systemd-importd pulls an image from an HTTP server, it
parses HTTP headers by comparing header names as-is, without any
conversion. That causes failures when some HTTP servers return headers
with different combinations of upper-/lower-cases.
An example:
https://alpha.release.flatcar-linux.net/amd64-usr/current/flatcar_developer_container.bin.bz2 returns `Etag: "pe89so9oir60"`,
while https://alpha.release.core-os.net/amd64-usr/current/coreos_developer_container.bin.bz2
returns `ETag: "f03372edea9a1e7232e282c346099857"`.
Since systemd-importd expects to see `ETag`, the etag for the Container Linux image
is correctly interpreted as a part of the hidden file name.
However, it cannot parse etag for Flatcar Linux, so the etag the Flatcar Linux image
is not appended to the hidden file name.
```
$ sudo ls -al /var/lib/machines/
-r--r--r-- 1 root root 3303014400 Aug 21 20:07 '.raw-https:\x2f\x2falpha\x2erelease\x2ecore-os\x2enet\x2famd64-usr\x2fcurrent\x2fcoreos_developer_container\x2ebin\x2ebz2.\x22f03372edea9a1e7232e282c346099857\x22.raw'
-r--r--r-- 1 root root 3303014400 Aug 17 06:15 '.raw-https:\x2f\x2falpha\x2erelease\x2eflatcar-linux\x2enet\x2famd64-usr\x2fcurrent\x2fflatcar_developer_container\x2ebin\x2ebz2.raw'
```
As a result, when the Flatcar image is removed and downloaded again,
systemd-importd is not able to determine if the file has been already
downloaded, so it always download it again. Then it fails to rename it
to an expected name, because there's already a hidden file.
To fix this issue, let's introduce a new helper function
`memory_startswith_no_case()`, which compares memory regions in a
case-insensitive way. Use this function in `curl_header_strdup()`.
See also https://github.com/kinvolk/kube-spawn/issues/304
[1]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
2018-09-03 20:44:13 +02:00
|
|
|
|
|
|
|
/* Like startswith_no_case(), but operates on arbitrary memory blocks.
|
|
|
|
* It works only for ASCII strings.
|
|
|
|
*/
|
|
|
|
static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) {
|
|
|
|
assert(token);
|
|
|
|
|
2020-07-30 17:00:25 +02:00
|
|
|
size_t n = strlen(token);
|
importd, basic/string-util: use case-insensitive comparison for HTTP headers
According to RFC2616[1], HTTP header names are case-insensitive. So
it's totally valid to have a header starting with either `Date:` or
`date:`.
However, when systemd-importd pulls an image from an HTTP server, it
parses HTTP headers by comparing header names as-is, without any
conversion. That causes failures when some HTTP servers return headers
with different combinations of upper-/lower-cases.
An example:
https://alpha.release.flatcar-linux.net/amd64-usr/current/flatcar_developer_container.bin.bz2 returns `Etag: "pe89so9oir60"`,
while https://alpha.release.core-os.net/amd64-usr/current/coreos_developer_container.bin.bz2
returns `ETag: "f03372edea9a1e7232e282c346099857"`.
Since systemd-importd expects to see `ETag`, the etag for the Container Linux image
is correctly interpreted as a part of the hidden file name.
However, it cannot parse etag for Flatcar Linux, so the etag the Flatcar Linux image
is not appended to the hidden file name.
```
$ sudo ls -al /var/lib/machines/
-r--r--r-- 1 root root 3303014400 Aug 21 20:07 '.raw-https:\x2f\x2falpha\x2erelease\x2ecore-os\x2enet\x2famd64-usr\x2fcurrent\x2fcoreos_developer_container\x2ebin\x2ebz2.\x22f03372edea9a1e7232e282c346099857\x22.raw'
-r--r--r-- 1 root root 3303014400 Aug 17 06:15 '.raw-https:\x2f\x2falpha\x2erelease\x2eflatcar-linux\x2enet\x2famd64-usr\x2fcurrent\x2fflatcar_developer_container\x2ebin\x2ebz2.raw'
```
As a result, when the Flatcar image is removed and downloaded again,
systemd-importd is not able to determine if the file has been already
downloaded, so it always download it again. Then it fails to rename it
to an expected name, because there's already a hidden file.
To fix this issue, let's introduce a new helper function
`memory_startswith_no_case()`, which compares memory regions in a
case-insensitive way. Use this function in `curl_header_strdup()`.
See also https://github.com/kinvolk/kube-spawn/issues/304
[1]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
2018-09-03 20:44:13 +02:00
|
|
|
if (sz < n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
|
2020-07-30 17:00:25 +02:00
|
|
|
for (size_t i = 0; i < n; i++)
|
importd, basic/string-util: use case-insensitive comparison for HTTP headers
According to RFC2616[1], HTTP header names are case-insensitive. So
it's totally valid to have a header starting with either `Date:` or
`date:`.
However, when systemd-importd pulls an image from an HTTP server, it
parses HTTP headers by comparing header names as-is, without any
conversion. That causes failures when some HTTP servers return headers
with different combinations of upper-/lower-cases.
An example:
https://alpha.release.flatcar-linux.net/amd64-usr/current/flatcar_developer_container.bin.bz2 returns `Etag: "pe89so9oir60"`,
while https://alpha.release.core-os.net/amd64-usr/current/coreos_developer_container.bin.bz2
returns `ETag: "f03372edea9a1e7232e282c346099857"`.
Since systemd-importd expects to see `ETag`, the etag for the Container Linux image
is correctly interpreted as a part of the hidden file name.
However, it cannot parse etag for Flatcar Linux, so the etag the Flatcar Linux image
is not appended to the hidden file name.
```
$ sudo ls -al /var/lib/machines/
-r--r--r-- 1 root root 3303014400 Aug 21 20:07 '.raw-https:\x2f\x2falpha\x2erelease\x2ecore-os\x2enet\x2famd64-usr\x2fcurrent\x2fcoreos_developer_container\x2ebin\x2ebz2.\x22f03372edea9a1e7232e282c346099857\x22.raw'
-r--r--r-- 1 root root 3303014400 Aug 17 06:15 '.raw-https:\x2f\x2falpha\x2erelease\x2eflatcar-linux\x2enet\x2famd64-usr\x2fcurrent\x2fflatcar_developer_container\x2ebin\x2ebz2.raw'
```
As a result, when the Flatcar image is removed and downloaded again,
systemd-importd is not able to determine if the file has been already
downloaded, so it always download it again. Then it fails to rename it
to an expected name, because there's already a hidden file.
To fix this issue, let's introduce a new helper function
`memory_startswith_no_case()`, which compares memory regions in a
case-insensitive way. Use this function in `curl_header_strdup()`.
See also https://github.com/kinvolk/kube-spawn/issues/304
[1]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
2018-09-03 20:44:13 +02:00
|
|
|
if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i]))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (uint8_t*) p + n;
|
|
|
|
}
|
Rework cmdline printing to use unicode
The functions to retrieve and print process cmdlines were based on the
assumption that they contain printable ASCII, and everything else
should be filtered out. That assumption doesn't hold in today's world,
where people are free to use unicode everywhere.
This replaces the custom cmdline reading code with a more generic approach
using utf8_escape_non_printable_full().
For kernel threads, truncation is done on the parenthesized name, so we'll
get "[worker]", "[worker…]", …, "[w…]", "[…", "…" as we reduce the number of
available columns.
This implementation is most likely slower for very long cmdlines, but I don't
think this is very important. The common case is to have short commandlines,
and should print those properly. Absurdly long cmdlines are the exception,
which needs to be handled correctly and safely, but speed is not too important.
Fixes #12532.
v2:
- use size_t for the number of columns. This change propagates into various
other functions that call get_process_cmdline(), increasing the size of the
patch, but the changes are rather trivial.
2019-05-15 11:20:26 +02:00
|
|
|
|
|
|
|
static inline char* str_realloc(char **p) {
|
|
|
|
/* Reallocate *p to actual size */
|
|
|
|
|
|
|
|
if (!*p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *t = realloc(*p, strlen(*p) + 1);
|
|
|
|
if (!t)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (*p = t);
|
|
|
|
}
|
2019-07-11 14:50:26 +02:00
|
|
|
|
|
|
|
char* string_erase(char *x);
|
2020-01-13 16:07:06 +01:00
|
|
|
|
|
|
|
int string_truncate_lines(const char *s, size_t n_lines, char **ret);
|
2020-01-13 16:20:27 +01:00
|
|
|
int string_extract_line(const char *s, size_t i, char **ret);
|
2020-07-30 10:34:44 +02:00
|
|
|
|
|
|
|
int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word);
|
|
|
|
static inline int string_contains_word(const char *string, const char *separators, const char *word) {
|
|
|
|
return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
|
|
|
|
}
|