aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--archival/Config.in4
-rw-r--r--archival/libunarchive/data_extract_all.c2
-rw-r--r--archival/libunarchive/decompress_unlzma.c154
-rw-r--r--modutils/modprobe-small.c1
-rw-r--r--networking/httpd.c30
-rw-r--r--networking/inetd.c25
-rw-r--r--shell/ash.c2
-rw-r--r--shell/hush.c61
9 files changed, 190 insertions, 91 deletions
diff --git a/Makefile b/Makefile
index 51c08a782..ebcdb3bbf 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
1VERSION = 1 1VERSION = 1
2PATCHLEVEL = 15 2PATCHLEVEL = 15
3SUBLEVEL = 0 3SUBLEVEL = 1
4EXTRAVERSION = 4EXTRAVERSION =
5NAME = Unnamed 5NAME = Unnamed
6 6
diff --git a/archival/Config.in b/archival/Config.in
index cae7f20bb..71b953819 100644
--- a/archival/Config.in
+++ b/archival/Config.in
@@ -298,8 +298,8 @@ config FEATURE_LZMA_FAST
298 default n 298 default n
299 depends on UNLZMA 299 depends on UNLZMA
300 help 300 help
301 This option reduces decompression time by about 25% at the cost of 301 This option reduces decompression time by about 33% at the cost of
302 a 1K bigger binary. 302 a 2K bigger binary.
303 303
304config UNZIP 304config UNZIP
305 bool "unzip" 305 bool "unzip"
diff --git a/archival/libunarchive/data_extract_all.c b/archival/libunarchive/data_extract_all.c
index 123d1de74..d79ef7cb9 100644
--- a/archival/libunarchive/data_extract_all.c
+++ b/archival/libunarchive/data_extract_all.c
@@ -132,7 +132,7 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
132#endif 132#endif
133 lchown(file_header->name, file_header->uid, file_header->gid); 133 lchown(file_header->name, file_header->uid, file_header->gid);
134 } 134 }
135 if (S_ISLNK(file_header->mode)) { 135 if (!S_ISLNK(file_header->mode)) {
136 /* uclibc has no lchmod, glibc is even stranger - 136 /* uclibc has no lchmod, glibc is even stranger -
137 * it has lchmod which seems to do nothing! 137 * it has lchmod which seems to do nothing!
138 * so we use chmod... */ 138 * so we use chmod... */
diff --git a/archival/libunarchive/decompress_unlzma.c b/archival/libunarchive/decompress_unlzma.c
index 68085d68c..33e5cd65d 100644
--- a/archival/libunarchive/decompress_unlzma.c
+++ b/archival/libunarchive/decompress_unlzma.c
@@ -8,15 +8,14 @@
8 * 8 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 */ 10 */
11
11#include "libbb.h" 12#include "libbb.h"
12#include "unarchive.h" 13#include "unarchive.h"
13 14
14#if ENABLE_FEATURE_LZMA_FAST 15#if ENABLE_FEATURE_LZMA_FAST
15# define speed_inline ALWAYS_INLINE 16# define speed_inline ALWAYS_INLINE
16# define size_inline
17#else 17#else
18# define speed_inline 18# define speed_inline
19# define size_inline ALWAYS_INLINE
20#endif 19#endif
21 20
22 21
@@ -45,8 +44,8 @@ typedef struct {
45#define RC_MODEL_TOTAL_BITS 11 44#define RC_MODEL_TOTAL_BITS 11
46 45
47 46
48/* Called twice: once at startup (LZMA_FAST only) and once in rc_normalize() */ 47/* Called twice: once at startup and once in rc_normalize() */
49static size_inline void rc_read(rc_t *rc) 48static void rc_read(rc_t *rc)
50{ 49{
51 int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE); 50 int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
52 if (buffer_size <= 0) 51 if (buffer_size <= 0)
@@ -55,17 +54,8 @@ static size_inline void rc_read(rc_t *rc)
55 rc->buffer_end = RC_BUFFER + buffer_size; 54 rc->buffer_end = RC_BUFFER + buffer_size;
56} 55}
57 56
58/* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
59static void rc_do_normalize(rc_t *rc)
60{
61 if (rc->ptr >= rc->buffer_end)
62 rc_read(rc);
63 rc->range <<= 8;
64 rc->code = (rc->code << 8) | *rc->ptr++;
65}
66
67/* Called once */ 57/* Called once */
68static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */ 58static rc_t* rc_init(int fd) /*, int buffer_size) */
69{ 59{
70 int i; 60 int i;
71 rc_t *rc; 61 rc_t *rc;
@@ -73,18 +63,17 @@ static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
73 rc = xmalloc(sizeof(*rc) + RC_BUFFER_SIZE); 63 rc = xmalloc(sizeof(*rc) + RC_BUFFER_SIZE);
74 64
75 rc->fd = fd; 65 rc->fd = fd;
66 /* rc->buffer_size = buffer_size; */
67 rc->buffer_end = RC_BUFFER + RC_BUFFER_SIZE;
76 rc->ptr = rc->buffer_end; 68 rc->ptr = rc->buffer_end;
77 69
70 rc->code = 0;
71 rc->range = 0xFFFFFFFF;
78 for (i = 0; i < 5; i++) { 72 for (i = 0; i < 5; i++) {
79#if ENABLE_FEATURE_LZMA_FAST
80 if (rc->ptr >= rc->buffer_end) 73 if (rc->ptr >= rc->buffer_end)
81 rc_read(rc); 74 rc_read(rc);
82 rc->code = (rc->code << 8) | *rc->ptr++; 75 rc->code = (rc->code << 8) | *rc->ptr++;
83#else
84 rc_do_normalize(rc);
85#endif
86 } 76 }
87 rc->range = 0xFFFFFFFF;
88 return rc; 77 return rc;
89} 78}
90 79
@@ -94,6 +83,14 @@ static ALWAYS_INLINE void rc_free(rc_t *rc)
94 free(rc); 83 free(rc);
95} 84}
96 85
86/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
87static void rc_do_normalize(rc_t *rc)
88{
89 if (rc->ptr >= rc->buffer_end)
90 rc_read(rc);
91 rc->range <<= 8;
92 rc->code = (rc->code << 8) | *rc->ptr++;
93}
97static ALWAYS_INLINE void rc_normalize(rc_t *rc) 94static ALWAYS_INLINE void rc_normalize(rc_t *rc)
98{ 95{
99 if (rc->range < (1 << RC_TOP_BITS)) { 96 if (rc->range < (1 << RC_TOP_BITS)) {
@@ -101,28 +98,49 @@ static ALWAYS_INLINE void rc_normalize(rc_t *rc)
101 } 98 }
102} 99}
103 100
104/* rc_is_bit_1 is called 9 times */ 101/* rc_is_bit_0 is called 9 times */
105static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p) 102/* Why rc_is_bit_0_helper exists?
103 * Because we want to always expose (rc->code < rc->bound) to optimizer.
104 * Thus rc_is_bit_0 is always inlined, and rc_is_bit_0_helper is inlined
105 * only if we compile for speed.
106 */
107static speed_inline uint32_t rc_is_bit_0_helper(rc_t *rc, uint16_t *p)
106{ 108{
107 rc_normalize(rc); 109 rc_normalize(rc);
108 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS); 110 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
109 if (rc->code < rc->bound) { 111 return rc->bound;
110 rc->range = rc->bound; 112}
111 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS; 113static ALWAYS_INLINE int rc_is_bit_0(rc_t *rc, uint16_t *p)
112 return 0; 114{
113 } 115 uint32_t t = rc_is_bit_0_helper(rc, p);
116 return rc->code < t;
117}
118
119/* Called ~10 times, but very small, thus inlined */
120static speed_inline void rc_update_bit_0(rc_t *rc, uint16_t *p)
121{
122 rc->range = rc->bound;
123 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
124}
125static speed_inline void rc_update_bit_1(rc_t *rc, uint16_t *p)
126{
114 rc->range -= rc->bound; 127 rc->range -= rc->bound;
115 rc->code -= rc->bound; 128 rc->code -= rc->bound;
116 *p -= *p >> RC_MOVE_BITS; 129 *p -= *p >> RC_MOVE_BITS;
117 return 1;
118} 130}
119 131
120/* Called 4 times in unlzma loop */ 132/* Called 4 times in unlzma loop */
121static speed_inline int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol) 133static int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
122{ 134{
123 int ret = rc_is_bit_1(rc, p); 135 if (rc_is_bit_0(rc, p)) {
124 *symbol = *symbol * 2 + ret; 136 rc_update_bit_0(rc, p);
125 return ret; 137 *symbol *= 2;
138 return 0;
139 } else {
140 rc_update_bit_1(rc, p);
141 *symbol = *symbol * 2 + 1;
142 return 1;
143 }
126} 144}
127 145
128/* Called once */ 146/* Called once */
@@ -248,13 +266,13 @@ unpack_lzma_stream(int src_fd, int dst_fd)
248 header.dst_size = SWAP_LE64(header.dst_size); 266 header.dst_size = SWAP_LE64(header.dst_size);
249 267
250 if (header.dict_size == 0) 268 if (header.dict_size == 0)
251 header.dict_size++; 269 header.dict_size = 1;
252 270
253 buffer = xmalloc(MIN(header.dst_size, header.dict_size)); 271 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
254 272
255 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)); 273 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
256 p = xmalloc(num_probs * sizeof(*p)); 274 p = xmalloc(num_probs * sizeof(*p));
257 num_probs += LZMA_LITERAL - LZMA_BASE_SIZE; 275 num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
258 for (i = 0; i < num_probs; i++) 276 for (i = 0; i < num_probs; i++)
259 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1; 277 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
260 278
@@ -264,8 +282,9 @@ unpack_lzma_stream(int src_fd, int dst_fd)
264 int pos_state = (buffer_pos + global_pos) & pos_state_mask; 282 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
265 283
266 prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state; 284 prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
267 if (!rc_is_bit_1(rc, prob)) { 285 if (rc_is_bit_0(rc, prob)) {
268 mi = 1; 286 mi = 1;
287 rc_update_bit_0(rc, prob);
269 prob = (p + LZMA_LITERAL 288 prob = (p + LZMA_LITERAL
270 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc) 289 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
271 + (previous_byte >> (8 - lc)) 290 + (previous_byte >> (8 - lc))
@@ -321,21 +340,27 @@ unpack_lzma_stream(int src_fd, int dst_fd)
321 int offset; 340 int offset;
322 uint16_t *prob_len; 341 uint16_t *prob_len;
323 342
343 rc_update_bit_1(rc, prob);
324 prob = p + LZMA_IS_REP + state; 344 prob = p + LZMA_IS_REP + state;
325 if (!rc_is_bit_1(rc, prob)) { 345 if (rc_is_bit_0(rc, prob)) {
346 rc_update_bit_0(rc, prob);
326 rep3 = rep2; 347 rep3 = rep2;
327 rep2 = rep1; 348 rep2 = rep1;
328 rep1 = rep0; 349 rep1 = rep0;
329 state = state < LZMA_NUM_LIT_STATES ? 0 : 3; 350 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
330 prob = p + LZMA_LEN_CODER; 351 prob = p + LZMA_LEN_CODER;
331 } else { 352 } else {
332 prob += LZMA_IS_REP_G0 - LZMA_IS_REP; 353 rc_update_bit_1(rc, prob);
333 if (!rc_is_bit_1(rc, prob)) { 354 prob = p + LZMA_IS_REP_G0 + state;
355 if (rc_is_bit_0(rc, prob)) {
356 rc_update_bit_0(rc, prob);
334 prob = (p + LZMA_IS_REP_0_LONG 357 prob = (p + LZMA_IS_REP_0_LONG
335 + (state << LZMA_NUM_POS_BITS_MAX) 358 + (state << LZMA_NUM_POS_BITS_MAX)
336 + pos_state 359 + pos_state
337 ); 360 );
338 if (!rc_is_bit_1(rc, prob)) { 361 if (rc_is_bit_0(rc, prob)) {
362 rc_update_bit_0(rc, prob);
363
339 state = state < LZMA_NUM_LIT_STATES ? 9 : 11; 364 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
340#if ENABLE_FEATURE_LZMA_FAST 365#if ENABLE_FEATURE_LZMA_FAST
341 pos = buffer_pos - rep0; 366 pos = buffer_pos - rep0;
@@ -347,16 +372,25 @@ unpack_lzma_stream(int src_fd, int dst_fd)
347 len = 1; 372 len = 1;
348 goto string; 373 goto string;
349#endif 374#endif
375 } else {
376 rc_update_bit_1(rc, prob);
350 } 377 }
351 } else { 378 } else {
352 uint32_t distance; 379 uint32_t distance;
353 380
354 prob += LZMA_IS_REP_G1 - LZMA_IS_REP_G0; 381 rc_update_bit_1(rc, prob);
355 distance = rep1; 382 prob = p + LZMA_IS_REP_G1 + state;
356 if (rc_is_bit_1(rc, prob)) { 383 if (rc_is_bit_0(rc, prob)) {
357 prob += LZMA_IS_REP_G2 - LZMA_IS_REP_G1; 384 rc_update_bit_0(rc, prob);
358 distance = rep2; 385 distance = rep1;
359 if (rc_is_bit_1(rc, prob)) { 386 } else {
387 rc_update_bit_1(rc, prob);
388 prob = p + LZMA_IS_REP_G2 + state;
389 if (rc_is_bit_0(rc, prob)) {
390 rc_update_bit_0(rc, prob);
391 distance = rep2;
392 } else {
393 rc_update_bit_1(rc, prob);
360 distance = rep3; 394 distance = rep3;
361 rep3 = rep2; 395 rep3 = rep2;
362 } 396 }
@@ -370,20 +404,24 @@ unpack_lzma_stream(int src_fd, int dst_fd)
370 } 404 }
371 405
372 prob_len = prob + LZMA_LEN_CHOICE; 406 prob_len = prob + LZMA_LEN_CHOICE;
373 if (!rc_is_bit_1(rc, prob_len)) { 407 if (rc_is_bit_0(rc, prob_len)) {
374 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE 408 rc_update_bit_0(rc, prob_len);
375 + (pos_state << LZMA_LEN_NUM_LOW_BITS); 409 prob_len = (prob + LZMA_LEN_LOW
410 + (pos_state << LZMA_LEN_NUM_LOW_BITS));
376 offset = 0; 411 offset = 0;
377 num_bits = LZMA_LEN_NUM_LOW_BITS; 412 num_bits = LZMA_LEN_NUM_LOW_BITS;
378 } else { 413 } else {
379 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE; 414 rc_update_bit_1(rc, prob_len);
380 if (!rc_is_bit_1(rc, prob_len)) { 415 prob_len = prob + LZMA_LEN_CHOICE_2;
381 prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2 416 if (rc_is_bit_0(rc, prob_len)) {
382 + (pos_state << LZMA_LEN_NUM_MID_BITS); 417 rc_update_bit_0(rc, prob_len);
418 prob_len = (prob + LZMA_LEN_MID
419 + (pos_state << LZMA_LEN_NUM_MID_BITS));
383 offset = 1 << LZMA_LEN_NUM_LOW_BITS; 420 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
384 num_bits = LZMA_LEN_NUM_MID_BITS; 421 num_bits = LZMA_LEN_NUM_MID_BITS;
385 } else { 422 } else {
386 prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2; 423 rc_update_bit_1(rc, prob_len);
424 prob_len = prob + LZMA_LEN_HIGH;
387 offset = ((1 << LZMA_LEN_NUM_LOW_BITS) 425 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
388 + (1 << LZMA_LEN_NUM_MID_BITS)); 426 + (1 << LZMA_LEN_NUM_MID_BITS));
389 num_bits = LZMA_LEN_NUM_HIGH_BITS; 427 num_bits = LZMA_LEN_NUM_HIGH_BITS;
@@ -400,20 +438,19 @@ unpack_lzma_stream(int src_fd, int dst_fd)
400 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len : 438 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
401 LZMA_NUM_LEN_TO_POS_STATES - 1) 439 LZMA_NUM_LEN_TO_POS_STATES - 1)
402 << LZMA_NUM_POS_SLOT_BITS); 440 << LZMA_NUM_POS_SLOT_BITS);
403 rc_bit_tree_decode(rc, prob, 441 rc_bit_tree_decode(rc, prob, LZMA_NUM_POS_SLOT_BITS,
404 LZMA_NUM_POS_SLOT_BITS, &pos_slot); 442 &pos_slot);
405 rep0 = pos_slot;
406 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) { 443 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
407 num_bits = (pos_slot >> 1) - 1; 444 num_bits = (pos_slot >> 1) - 1;
408 rep0 = 2 | (pos_slot & 1); 445 rep0 = 2 | (pos_slot & 1);
409 prob = p + LZMA_ALIGN;
410 if (pos_slot < LZMA_END_POS_MODEL_INDEX) { 446 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
411 rep0 <<= num_bits; 447 rep0 <<= num_bits;
412 prob += LZMA_SPEC_POS - LZMA_ALIGN - 1 + rep0 - pos_slot; 448 prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
413 } else { 449 } else {
414 num_bits -= LZMA_NUM_ALIGN_BITS; 450 num_bits -= LZMA_NUM_ALIGN_BITS;
415 while (num_bits--) 451 while (num_bits--)
416 rep0 = (rep0 << 1) | rc_direct_bit(rc); 452 rep0 = (rep0 << 1) | rc_direct_bit(rc);
453 prob = p + LZMA_ALIGN;
417 rep0 <<= LZMA_NUM_ALIGN_BITS; 454 rep0 <<= LZMA_NUM_ALIGN_BITS;
418 num_bits = LZMA_NUM_ALIGN_BITS; 455 num_bits = LZMA_NUM_ALIGN_BITS;
419 } 456 }
@@ -424,7 +461,8 @@ unpack_lzma_stream(int src_fd, int dst_fd)
424 rep0 |= i; 461 rep0 |= i;
425 i <<= 1; 462 i <<= 1;
426 } 463 }
427 } 464 } else
465 rep0 = pos_slot;
428 if (++rep0 == 0) 466 if (++rep0 == 0)
429 break; 467 break;
430 } 468 }
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c
index 0d78033c3..9ccee5e43 100644
--- a/modutils/modprobe-small.c
+++ b/modutils/modprobe-small.c
@@ -218,6 +218,7 @@ static void parse_module(module_info *info, const char *pathname)
218 bksp(); /* remove last ' ' */ 218 bksp(); /* remove last ' ' */
219 appendc('\0'); 219 appendc('\0');
220 info->aliases = copy_stringbuf(); 220 info->aliases = copy_stringbuf();
221 replace(info->aliases, '-', '_');
221 222
222 /* "dependency1 depandency2" */ 223 /* "dependency1 depandency2" */
223 reset_stringbuf(); 224 reset_stringbuf();
diff --git a/networking/httpd.c b/networking/httpd.c
index 956eecaf2..bc081c1bd 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -2101,8 +2101,12 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
2101 } 2101 }
2102 send_cgi_and_exit(urlcopy, prequest, length, cookie, content_type); 2102 send_cgi_and_exit(urlcopy, prequest, length, cookie, content_type);
2103 } 2103 }
2104#endif
2105
2106 if (urlp[-1] == '/')
2107 strcpy(urlp, index_page);
2108 if (stat(tptr, &sb) == 0) {
2104#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR 2109#if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
2105 {
2106 char *suffix = strrchr(tptr, '.'); 2110 char *suffix = strrchr(tptr, '.');
2107 if (suffix) { 2111 if (suffix) {
2108 Htaccess *cur; 2112 Htaccess *cur;
@@ -2112,16 +2116,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
2112 } 2116 }
2113 } 2117 }
2114 } 2118 }
2115 }
2116#endif 2119#endif
2117 if (prequest != request_GET && prequest != request_HEAD) {
2118 send_headers_and_exit(HTTP_NOT_IMPLEMENTED);
2119 }
2120#endif /* FEATURE_HTTPD_CGI */
2121
2122 if (urlp[-1] == '/')
2123 strcpy(urlp, index_page);
2124 if (stat(tptr, &sb) == 0) {
2125 file_size = sb.st_size; 2120 file_size = sb.st_size;
2126 last_mod = sb.st_mtime; 2121 last_mod = sb.st_mtime;
2127 } 2122 }
@@ -2135,19 +2130,18 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
2135 send_cgi_and_exit("/cgi-bin/index.cgi", prequest, length, cookie, content_type); 2130 send_cgi_and_exit("/cgi-bin/index.cgi", prequest, length, cookie, content_type);
2136 } 2131 }
2137 } 2132 }
2138#endif 2133 /* else fall through to send_file, it errors out if open fails: */
2139 /* else {
2140 * fall through to send_file, it errors out if open fails
2141 * }
2142 */
2143 2134
2135 if (prequest != request_GET && prequest != request_HEAD) {
2136 /* POST for files does not make sense */
2137 send_headers_and_exit(HTTP_NOT_IMPLEMENTED);
2138 }
2144 send_file_and_exit(tptr, 2139 send_file_and_exit(tptr,
2145#if ENABLE_FEATURE_HTTPD_CGI
2146 (prequest != request_HEAD ? SEND_HEADERS_AND_BODY : SEND_HEADERS) 2140 (prequest != request_HEAD ? SEND_HEADERS_AND_BODY : SEND_HEADERS)
2141 );
2147#else 2142#else
2148 SEND_HEADERS_AND_BODY 2143 send_file_and_exit(tptr, SEND_HEADERS_AND_BODY);
2149#endif 2144#endif
2150 );
2151} 2145}
2152 2146
2153/* 2147/*
diff --git a/networking/inetd.c b/networking/inetd.c
index 331c49441..391bb9ba6 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -1031,10 +1031,10 @@ static void reap_child(int sig UNUSED_PARAM)
1031 continue; 1031 continue;
1032 /* One of our "wait" services */ 1032 /* One of our "wait" services */
1033 if (WIFEXITED(status) && WEXITSTATUS(status)) 1033 if (WIFEXITED(status) && WEXITSTATUS(status))
1034 bb_error_msg("%s: exit status 0x%x", 1034 bb_error_msg("%s: exit status %u",
1035 sep->se_program, WEXITSTATUS(status)); 1035 sep->se_program, WEXITSTATUS(status));
1036 else if (WIFSIGNALED(status)) 1036 else if (WIFSIGNALED(status))
1037 bb_error_msg("%s: exit signal 0x%x", 1037 bb_error_msg("%s: exit signal %u",
1038 sep->se_program, WTERMSIG(status)); 1038 sep->se_program, WTERMSIG(status));
1039 sep->se_wait = 1; 1039 sep->se_wait = 1;
1040 add_fd_to_set(sep->se_fd); 1040 add_fd_to_set(sep->se_fd);
@@ -1119,7 +1119,12 @@ int inetd_main(int argc UNUSED_PARAM, char **argv)
1119 else 1119 else
1120 bb_sanitize_stdio(); 1120 bb_sanitize_stdio();
1121 if (!(opt & 4)) { 1121 if (!(opt & 4)) {
1122 openlog(applet_name, LOG_PID, LOG_DAEMON); 1122 /* LOG_NDELAY: connect to syslog daemon NOW.
1123 * Otherwise, we may open syslog socket
1124 * in vforked child, making opened fds and syslog()
1125 * internal state inconsistent.
1126 * This was observed to leak file descriptors. */
1127 openlog(applet_name, LOG_PID | LOG_NDELAY, LOG_DAEMON);
1123 logmode = LOGMODE_SYSLOG; 1128 logmode = LOGMODE_SYSLOG;
1124 } 1129 }
1125 1130
@@ -1355,17 +1360,23 @@ int inetd_main(int argc UNUSED_PARAM, char **argv)
1355 if (rlim_ofile.rlim_cur != rlim_ofile_cur) 1360 if (rlim_ofile.rlim_cur != rlim_ofile_cur)
1356 if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) 1361 if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
1357 bb_perror_msg("setrlimit"); 1362 bb_perror_msg("setrlimit");
1358 closelog(); 1363
1364 /* closelog(); - WRONG. we are after vfork,
1365 * this may confuse syslog() internal state.
1366 * Let's hope libc sets syslog fd to CLOEXEC...
1367 */
1359 xmove_fd(ctrl, STDIN_FILENO); 1368 xmove_fd(ctrl, STDIN_FILENO);
1360 xdup2(STDIN_FILENO, STDOUT_FILENO); 1369 xdup2(STDIN_FILENO, STDOUT_FILENO);
1361 /* manpages of inetd I managed to find either say 1370 /* manpages of inetd I managed to find either say
1362 * that stderr is also redirected to the network, 1371 * that stderr is also redirected to the network,
1363 * or do not talk about redirection at all (!) */ 1372 * or do not talk about redirection at all (!) */
1364 xdup2(STDIN_FILENO, STDERR_FILENO); 1373 if (!sep->se_wait) /* only for usual "tcp nowait" */
1365 /* NB: among others, this loop closes listening socket 1374 xdup2(STDIN_FILENO, STDERR_FILENO);
1375 /* NB: among others, this loop closes listening sockets
1366 * for nowait stream children */ 1376 * for nowait stream children */
1367 for (sep2 = serv_list; sep2; sep2 = sep2->se_next) 1377 for (sep2 = serv_list; sep2; sep2 = sep2->se_next)
1368 maybe_close(sep2->se_fd); 1378 if (sep2->se_fd != ctrl)
1379 maybe_close(sep2->se_fd);
1369 sigaction_set(SIGPIPE, &saved_pipe_handler); 1380 sigaction_set(SIGPIPE, &saved_pipe_handler);
1370 restore_sigmask(&omask); 1381 restore_sigmask(&omask);
1371 BB_EXECVP(sep->se_program, sep->se_argv); 1382 BB_EXECVP(sep->se_program, sep->se_argv);
diff --git a/shell/ash.c b/shell/ash.c
index 077f5e5fc..4a163ffbc 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -5810,7 +5810,7 @@ argstr(char *p, int flag, struct strlist *var_str_list)
5810 }; 5810 };
5811 const char *reject = spclchars; 5811 const char *reject = spclchars;
5812 int c; 5812 int c;
5813 int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ 5813 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
5814 int breakall = flag & EXP_WORD; 5814 int breakall = flag & EXP_WORD;
5815 int inquotes; 5815 int inquotes;
5816 size_t length; 5816 size_t length;
diff --git a/shell/hush.c b/shell/hush.c
index 7ac29ace2..5794b1ddf 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -58,7 +58,7 @@
58 * TODOs: 58 * TODOs:
59 * grep for "TODO" and fix (some of them are easy) 59 * grep for "TODO" and fix (some of them are easy)
60 * builtins: ulimit 60 * builtins: ulimit
61 * special variables (PWD etc) 61 * special variables (done: PWD)
62 * follow IFS rules more precisely, including update semantics 62 * follow IFS rules more precisely, including update semantics
63 * export builtin should be special, its arguments are assignments 63 * export builtin should be special, its arguments are assignments
64 * and therefore expansion of them should be "one-word" expansion: 64 * and therefore expansion of them should be "one-word" expansion:
@@ -1432,6 +1432,13 @@ static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_
1432 return 0; 1432 return 0;
1433} 1433}
1434 1434
1435/* Used at startup and after each cd */
1436static void set_pwd_var(int exp)
1437{
1438 set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)),
1439 /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0);
1440}
1441
1435static int unset_local_var_len(const char *name, int name_len) 1442static int unset_local_var_len(const char *name, int name_len)
1436{ 1443{
1437 struct variable *cur; 1444 struct variable *cur;
@@ -1604,6 +1611,9 @@ static const char* setup_prompt_string(int promptmode)
1604 /* Set up the prompt */ 1611 /* Set up the prompt */
1605 if (promptmode == 0) { /* PS1 */ 1612 if (promptmode == 0) { /* PS1 */
1606 free((char*)G.PS1); 1613 free((char*)G.PS1);
1614 /* bash uses $PWD value, even if it is set by user.
1615 * It uses current dir only if PWD is unset.
1616 * We always use current dir. */
1607 G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); 1617 G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#');
1608 prompt_str = G.PS1; 1618 prompt_str = G.PS1;
1609 } else 1619 } else
@@ -6432,8 +6442,49 @@ int hush_main(int argc, char **argv)
6432 } 6442 }
6433 e++; 6443 e++;
6434 } 6444 }
6445 /* reinstate HUSH_VERSION */
6435 debug_printf_env("putenv '%s'\n", hush_version_str); 6446 debug_printf_env("putenv '%s'\n", hush_version_str);
6436 putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ 6447 putenv((char *)hush_version_str);
6448
6449 /* Export PWD */
6450 set_pwd_var(/*exp:*/ 1);
6451 /* bash also exports SHLVL and _,
6452 * and sets (but doesn't export) the following variables:
6453 * BASH=/bin/bash
6454 * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu")
6455 * BASH_VERSION='3.2.0(1)-release'
6456 * HOSTTYPE=i386
6457 * MACHTYPE=i386-pc-linux-gnu
6458 * OSTYPE=linux-gnu
6459 * HOSTNAME=<xxxxxxxxxx>
6460 * PPID=<NNNNN>
6461 * EUID=<NNNNN>
6462 * UID=<NNNNN>
6463 * GROUPS=()
6464 * LINES=<NNN>
6465 * COLUMNS=<NNN>
6466 * BASH_ARGC=()
6467 * BASH_ARGV=()
6468 * BASH_LINENO=()
6469 * BASH_SOURCE=()
6470 * DIRSTACK=()
6471 * PIPESTATUS=([0]="0")
6472 * HISTFILE=/<xxx>/.bash_history
6473 * HISTFILESIZE=500
6474 * HISTSIZE=500
6475 * MAILCHECK=60
6476 * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.
6477 * SHELL=/bin/bash
6478 * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
6479 * TERM=dumb
6480 * OPTERR=1
6481 * OPTIND=1
6482 * IFS=$' \t\n'
6483 * PS1='\s-\v\$ '
6484 * PS2='> '
6485 * PS4='+ '
6486 */
6487
6437#if ENABLE_FEATURE_EDITING 6488#if ENABLE_FEATURE_EDITING
6438 G.line_input_state = new_line_input_t(FOR_SHELL); 6489 G.line_input_state = new_line_input_t(FOR_SHELL);
6439#endif 6490#endif
@@ -6816,7 +6867,11 @@ static int FAST_FUNC builtin_cd(char **argv)
6816 bb_perror_msg("cd: %s", newdir); 6867 bb_perror_msg("cd: %s", newdir);
6817 return EXIT_FAILURE; 6868 return EXIT_FAILURE;
6818 } 6869 }
6819 get_cwd(1); 6870 /* Read current dir (get_cwd(1) is inside) and set PWD.
6871 * Note: do not enforce exporting. If PWD was unset or unexported,
6872 * set it again, but do not export. bash does the same.
6873 */
6874 set_pwd_var(/*exp:*/ 0);
6820 return EXIT_SUCCESS; 6875 return EXIT_SUCCESS;
6821} 6876}
6822 6877