aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--debianutils/start_stop_daemon.c40
-rw-r--r--libbb/read.c23
2 files changed, 33 insertions, 30 deletions
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index c5c4059a1..9150763cb 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -32,7 +32,6 @@ struct globals {
32 int user_id; 32 int user_id;
33 smallint quiet; 33 smallint quiet;
34 smallint signal_nr; 34 smallint signal_nr;
35 struct stat execstat;
36}; 35};
37#define G (*(struct globals*)&bb_common_bufsiz1) 36#define G (*(struct globals*)&bb_common_bufsiz1)
38#define found (G.found ) 37#define found (G.found )
@@ -43,7 +42,6 @@ struct globals {
43#define user_id (G.user_id ) 42#define user_id (G.user_id )
44#define quiet (G.quiet ) 43#define quiet (G.quiet )
45#define signal_nr (G.signal_nr ) 44#define signal_nr (G.signal_nr )
46#define execstat (G.execstat )
47#define INIT_G() \ 45#define INIT_G() \
48 do { \ 46 do { \
49 user_id = -1; \ 47 user_id = -1; \
@@ -53,12 +51,13 @@ struct globals {
53 51
54static int pid_is_exec(pid_t pid) 52static int pid_is_exec(pid_t pid)
55{ 53{
56 struct stat st; 54 struct stat st, execstat;
57 char buf[sizeof("/proc//exe") + sizeof(int)*3]; 55 char buf[sizeof("/proc//exe") + sizeof(int)*3];
58 56
59 sprintf(buf, "/proc/%u/exe", pid); 57 sprintf(buf, "/proc/%u/exe", pid);
60 if (stat(buf, &st) < 0) 58 if (stat(buf, &st) < 0)
61 return 0; 59 return 0;
60 xstat(execname, &execstat);
62 if (st.st_dev == execstat.st_dev 61 if (st.st_dev == execstat.st_dev
63 && st.st_ino == execstat.st_ino) 62 && st.st_ino == execstat.st_ino)
64 return 1; 63 return 1;
@@ -78,24 +77,21 @@ static int pid_is_user(int pid)
78 77
79static int pid_is_cmd(pid_t pid) 78static int pid_is_cmd(pid_t pid)
80{ 79{
81 char fname[sizeof("/proc//stat") + sizeof(int)*3]; 80 char buf[256]; /* is it big enough? */
82 char *buf; 81 char *p, *pe;
83 int r = 0; 82
84 83 sprintf(buf, "/proc/%u/stat", pid);
85 sprintf(fname, "/proc/%u/stat", pid); 84 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
86 buf = xmalloc_open_read_close(fname, NULL); 85 return 0;
87 if (buf) { 86 buf[sizeof(buf) - 1] = '\0'; /* paranoia */
88 char *p = strchr(buf, '('); 87 p = strchr(buf, '(');
89 if (p) { 88 if (!p)
90 char *pe = strrchr(++p, ')'); 89 return 0;
91 if (pe) { 90 pe = strrchr(++p, ')');
92 *pe = '\0'; 91 if (!pe)
93 r = !strcmp(p, cmdname); 92 return 0;
94 } 93 *pe = '\0';
95 } 94 return !strcmp(p, cmdname);
96 free(buf);
97 }
98 return r;
99} 95}
100 96
101static void check(int pid) 97static void check(int pid)
@@ -303,8 +299,6 @@ int start_stop_daemon_main(int argc ATTRIBUTE_UNUSED, char **argv)
303 if (errno) 299 if (errno)
304 user_id = xuname2uid(userspec); 300 user_id = xuname2uid(userspec);
305 } 301 }
306 if (execname)
307 xstat(execname, &execstat);
308 302
309 do_procinit(); /* Both start and stop needs to know current processes */ 303 do_procinit(); /* Both start and stop needs to know current processes */
310 304
diff --git a/libbb/read.c b/libbb/read.c
index ba366cb97..e5f140f3b 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -208,25 +208,34 @@ ssize_t open_read_close(const char *filename, void *buf, size_t size)
208void *xmalloc_open_read_close(const char *filename, size_t *sizep) 208void *xmalloc_open_read_close(const char *filename, size_t *sizep)
209{ 209{
210 char *buf; 210 char *buf;
211 size_t size = sizep ? *sizep : INT_MAX; 211 size_t size;
212 int fd; 212 int fd;
213 off_t len; 213 off_t len;
214 214
215 fd = open(filename, O_RDONLY); 215 fd = open(filename, O_RDONLY);
216 if (fd < 0) 216 if (fd < 0)
217 return NULL; 217 return NULL;
218
218 /* /proc/N/stat files report len 0 here */ 219 /* /proc/N/stat files report len 0 here */
219 /* In order to make such files readable, we add small const */ 220 /* In order to make such files readable, we add small const */
220 len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ 221 size = 0x3ff; /* read only 1k on unseekable files */
221 xlseek(fd, 0, SEEK_SET); 222 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
222 if (len < size) 223 if (len != (off_t)-1) {
223 size = len; 224 xlseek(fd, 0, SEEK_SET);
225 size = sizep ? *sizep : INT_MAX;
226 if (len < size)
227 size = len;
228 }
229
224 buf = xmalloc(size + 1); 230 buf = xmalloc(size + 1);
225 size = read_close(fd, buf, size); 231 size = read_close(fd, buf, size);
226 if ((ssize_t)size < 0) 232 if ((ssize_t)size < 0) {
227 bb_perror_msg_and_die("'%s'", filename); 233 free(buf);
234 return NULL;
235 }
228 xrealloc(buf, size + 1); 236 xrealloc(buf, size + 1);
229 buf[size] = '\0'; 237 buf[size] = '\0';
238
230 if (sizep) 239 if (sizep)
231 *sizep = size; 240 *sizep = size;
232 return buf; 241 return buf;