1From: Roger Shimizu <rosh@debian.org>
2Date: Sun, 10 Jan 2021 19:03:17 +0900
3Subject: Fix GNU/Hurd
4
5Reference:
6- https://www.gnu.org/software/hurd/hurd/porting/guidelines.html
7
8Closes: #915762
9---
10 base/cmsg.cpp                  |  2 ++
11 base/threads.cpp               |  2 +-
12 libcutils/ashmem-host.cpp      | 20 +++++++++++++++++---
13 libcutils/canned_fs_config.cpp |  2 +-
14 libcutils/fs.cpp               | 20 ++++++++++++++++----
15 5 files changed, 37 insertions(+), 9 deletions(-)
16
17diff --git a/base/cmsg.cpp b/base/cmsg.cpp
18index ae5bb16..e5ec321 100644
19--- a/base/cmsg.cpp
20+++ b/base/cmsg.cpp
21@@ -20,7 +20,9 @@
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <sys/socket.h>
25+#ifndef __GNU__
26 #include <sys/user.h>
27+#endif
28
29 #include <memory>
30
31diff --git a/base/threads.cpp b/base/threads.cpp
32index 48f6197..19cc293 100644
33--- a/base/threads.cpp
34+++ b/base/threads.cpp
35@@ -47,7 +47,7 @@ uint64_t GetThreadId() {
36 }  // namespace base
37 }  // namespace android
38
39-#if defined(__GLIBC__)
40+#if defined(__GLIBC__) && !defined(__GNU__)
41 int tgkill(int tgid, int tid, int sig) {
42   return syscall(__NR_tgkill, tgid, tid, sig);
43 }
44diff --git a/libcutils/ashmem-host.cpp b/libcutils/ashmem-host.cpp
45index 32446d4..83dd622 100644
46--- a/libcutils/ashmem-host.cpp
47+++ b/libcutils/ashmem-host.cpp
48@@ -31,16 +31,30 @@
49 #include <sys/types.h>
50 #include <time.h>
51 #include <unistd.h>
52+#include <stdint.h>
53
54 #include <utils/Compat.h>
55
56 int ashmem_create_region(const char* /*ignored*/, size_t size) {
57-    char pattern[PATH_MAX];
58-    snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
59+    char *pattern;
60+    size_t pattern_size = 128;
61+    while(1) {
62+        pattern = (char*) malloc(pattern_size);
63+        if(snprintf(pattern, strlen(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid()) < pattern_size)
64+            break;
65+        free(pattern);
66+        pattern_size *= 2;
67+        if(pattern_size >= INT_LEAST16_MAX)
68+            return -1;
69+    }
70     int fd = mkstemp(pattern);
71-    if (fd == -1) return -1;
72+    if (fd == -1) {
73+        free(pattern);
74+        return -1;
75+	}
76
77     unlink(pattern);
78+    free(pattern);
79
80     if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
81       close(fd);
82diff --git a/libcutils/canned_fs_config.cpp b/libcutils/canned_fs_config.cpp
83index 2772ef0..1e41f37 100644
84--- a/libcutils/canned_fs_config.cpp
85+++ b/libcutils/canned_fs_config.cpp
86@@ -42,7 +42,7 @@ static int path_compare(const void* a, const void* b) {
87 }
88
89 int load_canned_fs_config(const char* fn) {
90-    char buf[PATH_MAX + 200];
91+    char buf[1024];
92     FILE* f;
93
94     f = fopen(fn, "r");
95diff --git a/libcutils/fs.cpp b/libcutils/fs.cpp
96index ef85acc..2884835 100644
97--- a/libcutils/fs.cpp
98+++ b/libcutils/fs.cpp
99@@ -33,6 +33,7 @@
100 #include <sys/stat.h>
101 #include <sys/types.h>
102 #include <unistd.h>
103+#include <stdint.h>
104
105 #include <log/log.h>
106
107@@ -150,15 +151,24 @@ fail:
108 }
109
110 int fs_write_atomic_int(const char* path, int value) {
111-    char temp[PATH_MAX];
112-    if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
113-        ALOGE("Path too long");
114-        return -1;
115+    char *temp;
116+    size_t temp_size = 128;
117+    while(1) {
118+        temp = (char*) malloc(temp_size);
119+        if(snprintf(temp, strlen(temp), "%s.XXXXXX", path) < temp_size)
120+            break;
121+        free(temp);
122+        temp_size *= 2;
123+        if(temp_size >= INT_LEAST16_MAX) {
124+            ALOGE("Path too long");
125+            return -1;
126+        }
127     }
128
129     int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
130     if (fd == -1) {
131         ALOGE("Failed to open %s: %s", temp, strerror(errno));
132+        free(temp);
133         return -1;
134     }
135
136@@ -182,12 +192,14 @@ int fs_write_atomic_int(const char* path, int value) {
137         goto fail_closed;
138     }
139
140+    free(temp);
141     return 0;
142
143 fail:
144     close(fd);
145 fail_closed:
146     unlink(temp);
147+    free(temp);
148     return -1;
149 }
150
151