chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
util: add allocation loop to gettyname_malloc()
[elogind.git]
/
src
/
shared
/
util.c
diff --git
a/src/shared/util.c
b/src/shared/util.c
index ee95a4b6f71c67bb7ff3f5c3e8ce736bfaa98113..6bd278e7901a888fedd6c946c3f97130711a76c7 100644
(file)
--- a/
src/shared/util.c
+++ b/
src/shared/util.c
@@
-521,7
+521,7
@@
int safe_atod(const char *s, double *ret_d) {
static size_t strcspn_escaped(const char *s, const char *reject) {
bool escaped = false;
static size_t strcspn_escaped(const char *s, const char *reject) {
bool escaped = false;
-
size_
t n;
+
in
t n;
for (n=0; s[n]; n++) {
if (escaped)
for (n=0; s[n]; n++) {
if (escaped)
@@
-531,6
+531,7
@@
static size_t strcspn_escaped(const char *s, const char *reject) {
else if (strchr(reject, s[n]))
break;
}
else if (strchr(reject, s[n]))
break;
}
+
/* if s ends in \, return index of previous char */
return n - escaped;
}
/* if s ends in \, return index of previous char */
return n - escaped;
}
@@
-566,6
+567,11
@@
const char* split(const char **state, size_t *l, const char *separator, bool quo
*state = current++ + *l + 2;
} else if (quoted) {
*l = strcspn_escaped(current, separator);
*state = current++ + *l + 2;
} else if (quoted) {
*l = strcspn_escaped(current, separator);
+ if (current[*l] && !strchr(separator, current[*l])) {
+ /* unfinished escape */
+ *state = current;
+ return NULL;
+ }
*state = current + *l;
} else {
*l = strcspn(current, separator);
*state = current + *l;
} else {
*l = strcspn(current, separator);
@@
-1504,7
+1510,7
@@
char *ascii_strlower(char *t) {
return t;
}
return t;
}
-_pure_ static bool
ignore
_file_allow_backup(const char *filename) {
+_pure_ static bool
hidden
_file_allow_backup(const char *filename) {
assert(filename);
return
assert(filename);
return
@@
-1521,13
+1527,13
@@
_pure_ static bool ignore_file_allow_backup(const char *filename) {
endswith(filename, ".swp");
}
endswith(filename, ".swp");
}
-bool
ignore
_file(const char *filename) {
+bool
hidden
_file(const char *filename) {
assert(filename);
if (endswith(filename, "~"))
return true;
assert(filename);
if (endswith(filename, "~"))
return true;
- return
ignore
_file_allow_backup(filename);
+ return
hidden
_file_allow_backup(filename);
}
int fd_nonblock(int fd, bool nonblock) {
}
int fd_nonblock(int fd, bool nonblock) {
@@
-1621,7
+1627,7
@@
int close_all_fds(const int except[], unsigned n_except) {
while ((de = readdir(d))) {
int fd = -1;
while ((de = readdir(d))) {
int fd = -1;
- if (
ignore
_file(de->d_name))
+ if (
hidden
_file(de->d_name))
continue;
if (safe_atoi(de->d_name, &fd) < 0)
continue;
if (safe_atoi(de->d_name, &fd) < 0)
@@
-2468,9
+2474,9
@@
int make_stdio(int fd) {
assert(fd >= 0);
assert(fd >= 0);
- r = dup
3(fd, STDIN_FILENO, 0
);
- s = dup
3(fd, STDOUT_FILENO, 0
);
- t = dup
3(fd, STDERR_FILENO, 0
);
+ r = dup
2(fd, STDIN_FILENO
);
+ s = dup
2(fd, STDOUT_FILENO
);
+ t = dup
2(fd, STDERR_FILENO
);
if (fd >= 3)
safe_close(fd);
if (fd >= 3)
safe_close(fd);
@@
-2478,7
+2484,11
@@
int make_stdio(int fd) {
if (r < 0 || s < 0 || t < 0)
return -errno;
if (r < 0 || s < 0 || t < 0)
return -errno;
- /* We rely here that the new fd has O_CLOEXEC not set */
+ /* Explicitly unset O_CLOEXEC, since if fd was < 3, then
+ * dup2() was a NOP and the bit hence possibly set. */
+ fd_cloexec(STDIN_FILENO, false);
+ fd_cloexec(STDOUT_FILENO, false);
+ fd_cloexec(STDERR_FILENO, false);
return 0;
}
return 0;
}
@@
-2521,7
+2531,7
@@
int dir_is_empty(const char *path) {
if (!de)
return 1;
if (!de)
return 1;
- if (!
ignore
_file(de->d_name))
+ if (!
hidden
_file(de->d_name))
return 0;
}
}
return 0;
}
}
@@
-2775,23
+2785,36
@@
char *getusername_malloc(void) {
return lookup_uid(getuid());
}
return lookup_uid(getuid());
}
-int getttyname_malloc(int fd, char **r) {
-
char path[PATH_MAX], *c
;
- int
k
;
+int getttyname_malloc(int fd, char **r
et
) {
+
size_t l = 100
;
+ int
r
;
- assert(r);
+ assert(fd >= 0);
+ assert(ret);
- k = ttyname_r(fd, path, sizeof(path));
- if (k > 0)
- return -k;
+ for (;;) {
+ char path[l];
- char_array_0(path);
+ r = ttyname_r(fd, path, sizeof(path));
+ if (r == 0) {
+ const char *p;
+ char *c;
- c = strdup(startswith(path, "/dev/") ? path + 5 : path);
- if (!c)
- return -ENOMEM;
+ p = startswith(path, "/dev/");
+ c = strdup(p ?: path);
+ if (!c)
+ return -ENOMEM;
+
+ *ret = c;
+ return 0;
+ }
+
+ if (r != ERANGE)
+ return -r;
+
+ l *= 2;
+ }
- *r = c;
return 0;
}
return 0;
}
@@
-3059,7
+3082,7
@@
static int rm_rf_internal(const char *path, bool only_dirs, bool delete_root, bo
fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
if (fd < 0) {
fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
if (fd < 0) {
- if (errno != ENOTDIR)
+ if (errno != ENOTDIR
&& errno != ELOOP
)
return -errno;
if (!dangerous) {
return -errno;
if (!dangerous) {
@@
-3986,7
+4009,7
@@
const char *default_term_for_tty(const char *tty) {
bool dirent_is_file(const struct dirent *de) {
assert(de);
bool dirent_is_file(const struct dirent *de) {
assert(de);
- if (
ignore
_file(de->d_name))
+ if (
hidden
_file(de->d_name))
return false;
if (de->d_type != DT_REG &&
return false;
if (de->d_type != DT_REG &&
@@
-4005,7
+4028,7
@@
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
de->d_type != DT_UNKNOWN)
return false;
de->d_type != DT_UNKNOWN)
return false;
- if (
ignore
_file_allow_backup(de->d_name))
+ if (
hidden
_file_allow_backup(de->d_name))
return false;
return endswith(de->d_name, suffix);
return false;
return endswith(de->d_name, suffix);
@@
-4247,6
+4270,23
@@
bool machine_name_is_valid(const char *s) {
return true;
}
return true;
}
+bool image_name_is_valid(const char *s) {
+ if (!filename_is_valid(s))
+ return false;
+
+ if (string_has_cc(s, NULL))
+ return false;
+
+ if (!utf8_is_valid(s))
+ return false;
+
+ /* Temporary files for atomically creating new files */
+ if (startswith(s, ".#"))
+ return false;
+
+ return true;
+}
+
int pipe_eof(int fd) {
struct pollfd pollfd = {
.fd = fd,
int pipe_eof(int fd) {
struct pollfd pollfd = {
.fd = fd,
@@
-5899,7
+5939,7
@@
int on_ac_power(void) {
if (!de)
break;
if (!de)
break;
- if (
ignore
_file(de->d_name))
+ if (
hidden
_file(de->d_name))
continue;
device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
continue;
device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
@@
-6982,18
+7022,18
@@
int tempfn_xxxxxx(const char *p, char **ret) {
* /foo/bar/waldo
*
* Into this:
* /foo/bar/waldo
*
* Into this:
- * /foo/bar/.waldoXXXXXX
+ * /foo/bar/.
#
waldoXXXXXX
*/
fn = basename(p);
if (!filename_is_valid(fn))
return -EINVAL;
*/
fn = basename(p);
if (!filename_is_valid(fn))
return -EINVAL;
- t = new(char, strlen(p) +
1
+ 6 + 1);
+ t = new(char, strlen(p) +
2
+ 6 + 1);
if (!t)
return -ENOMEM;
if (!t)
return -ENOMEM;
- strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), "."), fn), "XXXXXX");
+ strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".
#
"), fn), "XXXXXX");
*ret = path_kill_slashes(t);
return 0;
*ret = path_kill_slashes(t);
return 0;
@@
-7013,18
+7053,18
@@
int tempfn_random(const char *p, char **ret) {
* /foo/bar/waldo
*
* Into this:
* /foo/bar/waldo
*
* Into this:
- * /foo/bar/.waldobaa2a261115984a9
+ * /foo/bar/.
#
waldobaa2a261115984a9
*/
fn = basename(p);
if (!filename_is_valid(fn))
return -EINVAL;
*/
fn = basename(p);
if (!filename_is_valid(fn))
return -EINVAL;
- t = new(char, strlen(p) +
1
+ 16 + 1);
+ t = new(char, strlen(p) +
2
+ 16 + 1);
if (!t)
return -ENOMEM;
if (!t)
return -ENOMEM;
- x = stpcpy(stpcpy(mempcpy(t, p, fn - p), "."), fn);
+ x = stpcpy(stpcpy(mempcpy(t, p, fn - p), ".
#
"), fn);
u = random_u64();
for (i = 0; i < 16; i++) {
u = random_u64();
for (i = 0; i < 16; i++) {
@@
-7049,14
+7089,14
@@
int tempfn_random_child(const char *p, char **ret) {
/* Turns this:
* /foo/bar/waldo
* Into this:
/* Turns this:
* /foo/bar/waldo
* Into this:
- * /foo/bar/waldo/.3c2b6219aa75d7d0
+ * /foo/bar/waldo/.
#
3c2b6219aa75d7d0
*/
*/
- t = new(char, strlen(p) +
2
+ 16 + 1);
+ t = new(char, strlen(p) +
3
+ 16 + 1);
if (!t)
return -ENOMEM;
if (!t)
return -ENOMEM;
- x = stpcpy(stpcpy(t, p), "/.");
+ x = stpcpy(stpcpy(t, p), "/.
#
");
u = random_u64();
for (i = 0; i < 16; i++) {
u = random_u64();
for (i = 0; i < 16; i++) {
@@
-7406,3
+7446,30
@@
int sethostname_idempotent(const char *s) {
return 1;
}
return 1;
}
+
+int ptsname_malloc(int fd, char **ret) {
+ size_t l = 100;
+
+ assert(fd >= 0);
+ assert(ret);
+
+ for (;;) {
+ char *c;
+
+ c = new(char, l);
+ if (!c)
+ return -ENOMEM;
+
+ if (ptsname_r(fd, c, l) == 0) {
+ *ret = c;
+ return 0;
+ }
+ if (errno != ERANGE) {
+ free(c);
+ return -errno;
+ }
+
+ free(c);
+ l *= 2;
+ }
+}