summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-12-23 02:10:08 +0100
committerLennart Poettering <lennart@poettering.net>2014-12-23 03:26:24 +0100
commit9d8c4979c0c9b09bd3d0d57ee585f6792c6add26 (patch)
treea7f1b905691023cb0972f483246114b1085414df /src
parent611b312b7d0799281347374dc303c73a066cedf8 (diff)
util: add allocation loop to gettyname_malloc()
Diffstat (limited to 'src')
-rw-r--r--src/shared/util.c40
1 files changed, 28 insertions, 12 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index 6695a85a1c..6bd278e790 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2785,23 +2785,36 @@ char *getusername_malloc(void) {
return lookup_uid(getuid());
}
-int getttyname_malloc(int fd, char **r) {
- char path[PATH_MAX], *c;
- int k;
+int getttyname_malloc(int fd, char **ret) {
+ 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;
}
@@ -7437,6 +7450,9 @@ int sethostname_idempotent(const char *s) {
int ptsname_malloc(int fd, char **ret) {
size_t l = 100;
+ assert(fd >= 0);
+ assert(ret);
+
for (;;) {
char *c;