blob: a45edde5d4dfdffe95498ad68b520b66c3f35483 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
3 * Utility routines.
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
Eric Andersen2b69c401999-10-05 22:58:32 +000028#include "internal.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000029#if defined (BB_CHMOD_CHOWN_CHGRP) \
30 || defined (BB_CP_MV) \
31 || defined (BB_FIND) \
32 || defined (BB_LS) \
33 || defined (BB_INSMOD)
34/* same conditions as recursiveAction */
35#define bb_need_name_too_long
36#endif
Eric Andersen9f16d612000-06-12 23:11:16 +000037#define bb_need_memory_exhausted
Erik Andersenfac10d72000-02-07 05:29:42 +000038#define BB_DECLARE_EXTERN
39#include "messages.c"
40
Eric Andersen2b69c401999-10-05 22:58:32 +000041#include <stdio.h>
42#include <string.h>
43#include <errno.h>
44#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000045#include <dirent.h>
46#include <time.h>
47#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000048#include <sys/stat.h>
49#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000050#include <ctype.h>
Erik Andersend07ee462000-02-21 21:26:32 +000051#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Erik Andersen5cbdd712000-01-26 20:06:48 +000053#if defined BB_FEATURE_MOUNT_LOOP
54#include <fcntl.h>
55#include <sys/ioctl.h>
56#include <linux/loop.h>
57#endif
58
Erik Andersen246cc6d2000-03-07 07:41:42 +000059/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
60 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000061#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000062//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
63#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
64#endif
65
Erik Andersen5cbdd712000-01-26 20:06:48 +000066
Eric Andersend0246fb1999-11-04 21:18:07 +000067#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000068# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000069const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000070# else
71# if defined BB_MTAB
72const char mtab_file[] = "/etc/mtab";
73# else
Erik Andersen269bba22000-03-08 14:50:47 +000074# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000075const char mtab_file[] = "/dev/mtab";
76# else
Erik Andersen269bba22000-03-08 14:50:47 +000077# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or ( BB_FEATURE_USE_PROCFS | BB_FEATURE_USE_DEVPS_PATCH)
Eric Andersen0ecb54a1999-12-05 23:24:55 +000078# endif
79# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000080# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000081#endif
82
83
Eric Andersend73dc5b1999-11-10 23:13:02 +000084extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000085{
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
87 BB_VER, BB_BT);
88 fprintf(stderr, "Usage: %s\n", usage);
89 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000090}
91
Erik Andersen1ad302a2000-03-24 00:54:46 +000092extern void errorMsg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000093{
94 va_list p;
95
96 va_start(p, s);
97 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000098 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000099 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000100 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000101}
102
Erik Andersen1ad302a2000-03-24 00:54:46 +0000103extern void fatalError(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000104{
105 va_list p;
106
107 va_start(p, s);
108 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000109 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000110 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000111 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000112 exit( FALSE);
113}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000114
Erik Andersen23dea9b2000-05-13 00:28:25 +0000115#if defined BB_INIT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000116/* Returns kernel version encoded as major*65536 + minor*256 + patch,
117 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000118 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000119 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000120extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000121{
Erik Andersend07ee462000-02-21 21:26:32 +0000122 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000124
Erik Andersend07ee462000-02-21 21:26:32 +0000125 if (uname(&name) == -1) {
126 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 return (0);
128 }
Erik Andersend07ee462000-02-21 21:26:32 +0000129 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000131}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000132#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000133
Eric Andersen10dc9d42000-06-26 10:45:52 +0000134
135
136#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
137#include <sys/syscall.h>
Eric Andersena44790f2000-06-28 20:11:28 +0000138#include <linux/unistd.h>
Eric Andersen10dc9d42000-06-26 10:45:52 +0000139_syscall1(int, sysinfo, struct sysinfo *, info);
140#endif /* BB_INIT */
141
142
143
Erik Andersen23dea9b2000-05-13 00:28:25 +0000144#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000145
Erik Andersen029011b2000-03-04 21:19:32 +0000146#define HASH_SIZE 311 /* Should be prime */
147#define hash_inode(i) ((i) % HASH_SIZE)
148
149static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
150
151/*
152 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
153 * `ino_dev_hashtable', else return 0
154 *
155 * If NAME is a non-NULL pointer to a character pointer, and there is
156 * a match, then set *NAME to the value of the name slot in that
157 * bucket.
158 */
159int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
160{
161 ino_dev_hashtable_bucket_t *bucket;
162
163 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
164 while (bucket != NULL) {
165 if ((bucket->ino == statbuf->st_ino) &&
166 (bucket->dev == statbuf->st_dev))
167 {
168 if (name) *name = bucket->name;
169 return 1;
170 }
171 bucket = bucket->next;
172 }
173 return 0;
174}
175
176/* Add statbuf to statbuf hash table */
177void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
178{
179 int i;
180 size_t s;
181 ino_dev_hashtable_bucket_t *bucket;
182
183 i = hash_inode(statbuf->st_ino);
184 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000185 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000186 bucket->ino = statbuf->st_ino;
187 bucket->dev = statbuf->st_dev;
188 if (name)
189 strcpy(bucket->name, name);
190 else
191 bucket->name[0] = '\0';
192 bucket->next = ino_dev_hashtable[i];
193 ino_dev_hashtable[i] = bucket;
194}
195
196/* Clear statbuf hash table */
197void reset_ino_dev_hashtable(void)
198{
199 int i;
200 ino_dev_hashtable_bucket_t *bucket;
201
202 for (i = 0; i < HASH_SIZE; i++) {
203 while (ino_dev_hashtable[i] != NULL) {
204 bucket = ino_dev_hashtable[i]->next;
205 free(ino_dev_hashtable[i]);
206 ino_dev_hashtable[i] = bucket;
207 }
208 }
209}
210
Erik Andersen23dea9b2000-05-13 00:28:25 +0000211#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000212
Erik Andersen23dea9b2000-05-13 00:28:25 +0000213#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000214/*
215 * Return TRUE if a fileName is a directory.
216 * Nonexistant files return FALSE.
217 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000218int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000219{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000221 int didMalloc = 0;
222
223 if (statBuf == NULL) {
224 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
225 ++didMalloc;
226 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000227
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000229 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000231 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000232
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000233 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
234 status = FALSE;
235 }
236 else status = TRUE;
237
238 if (didMalloc) {
239 free(statBuf);
240 statBuf = NULL;
241 }
242 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000243}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000244#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000245
Erik Andersen23dea9b2000-05-13 00:28:25 +0000246#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000247/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000248 * Copy one file to another, while possibly preserving its modes, times, and
249 * modes. Returns TRUE if successful, or FALSE on a failure with an error
250 * message output. (Failure is not indicated if attributes cannot be set.)
251 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000252 */
253int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000255 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000256{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 int rfd;
258 int wfd;
259 int rcc;
260 int status;
261 char buf[BUF_SIZE];
262 struct stat srcStatBuf;
263 struct stat dstStatBuf;
264 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000265
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 if (followLinks == TRUE)
267 status = stat(srcName, &srcStatBuf);
268 else
269 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000270
Erik Andersenfac10d72000-02-07 05:29:42 +0000271 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 perror(srcName);
273 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000274 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275
276 if (followLinks == TRUE)
277 status = stat(destName, &dstStatBuf);
278 else
279 status = lstat(destName, &dstStatBuf);
280
Erik Andersen59b9e872000-05-10 05:05:45 +0000281 if (status < 0 || forceFlag==TRUE) {
282 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 dstStatBuf.st_ino = -1;
284 dstStatBuf.st_dev = -1;
285 }
286
287 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
288 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
289 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
290 return FALSE;
291 }
292
293 if (S_ISDIR(srcStatBuf.st_mode)) {
294 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
295 /* Make sure the directory is writable */
296 status = mkdir(destName, 0777777 ^ umask(0));
297 if (status < 0 && errno != EEXIST) {
298 perror(destName);
299 return FALSE;
300 }
301 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000302 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 int link_size;
304
305 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000306 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
307 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 if (link_size < 0) {
309 perror(srcName);
310 return FALSE;
311 }
312 link_val[link_size] = '\0';
313 status = symlink(link_val, destName);
314 if (status < 0) {
315 perror(destName);
316 return FALSE;
317 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000318#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000320 /* Try to set owner, but fail silently like GNU cp */
321 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000322 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000323#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 return TRUE;
325 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
326 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
327 if (mkfifo(destName, 0644) < 0) {
328 perror(destName);
329 return FALSE;
330 }
331 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
332 || S_ISSOCK(srcStatBuf.st_mode)) {
333 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
334 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
335 perror(destName);
336 return FALSE;
337 }
338 } else if (S_ISREG(srcStatBuf.st_mode)) {
339 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
340 rfd = open(srcName, O_RDONLY);
341 if (rfd < 0) {
342 perror(srcName);
343 return FALSE;
344 }
345
346 wfd =
347 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
348 srcStatBuf.st_mode);
349 if (wfd < 0) {
350 perror(destName);
351 close(rfd);
352 return FALSE;
353 }
354
355 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
356 if (fullWrite(wfd, buf, rcc) < 0)
357 goto error_exit;
358 }
359 if (rcc < 0) {
360 goto error_exit;
361 }
362
363 close(rfd);
364 if (close(wfd) < 0) {
365 return FALSE;
366 }
367 }
368
369 if (setModes == TRUE) {
370 /* This is fine, since symlinks never get here */
371 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
372 perror(destName);
373 exit FALSE;
374 }
375 if (chmod(destName, srcStatBuf.st_mode) < 0) {
376 perror(destName);
377 exit FALSE;
378 }
379 times.actime = srcStatBuf.st_atime;
380 times.modtime = srcStatBuf.st_mtime;
381 if (utime(destName, &times) < 0) {
382 perror(destName);
383 exit FALSE;
384 }
385 }
386
Erik Andersenfac10d72000-02-07 05:29:42 +0000387 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000388
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 error_exit:
390 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000391 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000393
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000395}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000396#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000397
398
399
Erik Andersen23dea9b2000-05-13 00:28:25 +0000400#if defined BB_TAR || defined BB_LS
401
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000402#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
403#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
404
405/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
406static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 0, 0, S_ISUID,
408 0, 0, S_ISGID,
409 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000410};
411
412/* The 9 mode bits to test */
413static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000414 S_IRUSR, S_IWUSR, S_IXUSR,
415 S_IRGRP, S_IWGRP, S_IXGRP,
416 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000417};
418
419#define MODE1 "rwxrwxrwx"
420#define MODE0 "---------"
421#define SMODE1 "..s..s..t"
422#define SMODE0 "..S..S..T"
423
Eric Andersencc8ed391999-10-05 16:24:54 +0000424/*
425 * Return the standard ls-like mode string from a file mode.
426 * This is static and so is overwritten on each call.
427 */
Eric Andersenf811e071999-10-09 00:25:00 +0000428const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000429{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000431
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 int i;
433
434 buf[0] = TYPECHAR(mode);
435 for (i = 0; i < 9; i++) {
436 if (mode & SBIT[i])
437 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
438 else
439 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
440 }
441 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000442}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000443#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000444
445
Erik Andersen23dea9b2000-05-13 00:28:25 +0000446#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000447/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000448 * Return the standard ls-like time string from a time_t
449 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000450 */
Eric Andersenf811e071999-10-09 00:25:00 +0000451const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000452{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 time_t now;
454 char *str;
455 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000456
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000458
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000460
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 strcpy(buf, &str[4]);
462 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000463
Erik Andersene49d5ec2000-02-08 19:58:47 +0000464 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
465 strcpy(&buf[7], &str[20]);
466 buf[11] = '\0';
467 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000468
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000470}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000471#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000472
Erik Andersen23dea9b2000-05-13 00:28:25 +0000473#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000474/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000475 * Write all of the supplied buffer out to a file.
476 * This does multiple writes as necessary.
477 * Returns the amount written, or -1 on an error.
478 */
Eric Andersenf811e071999-10-09 00:25:00 +0000479int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000480{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 int cc;
482 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000483
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000485
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 while (len > 0) {
487 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000488
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 if (cc < 0)
490 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000491
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 buf += cc;
493 total += cc;
494 len -= cc;
495 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000496
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000498}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000499#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000500
501
Erik Andersen23dea9b2000-05-13 00:28:25 +0000502#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000503/*
504 * Read all of the supplied buffer from a file.
505 * This does multiple reads as necessary.
506 * Returns the amount read, or -1 on an error.
507 * A short read is returned on an end of file.
508 */
Eric Andersenf811e071999-10-09 00:25:00 +0000509int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000510{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 int cc;
512 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000513
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000515
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 while (len > 0) {
517 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 if (cc < 0)
520 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000521
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 if (cc == 0)
523 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000524
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 buf += cc;
526 total += cc;
527 len -= cc;
528 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000529
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000531}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000532#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000533
534
Erik Andersen23dea9b2000-05-13 00:28:25 +0000535#if defined (BB_CHMOD_CHOWN_CHGRP) \
536 || defined (BB_CP_MV) \
537 || defined (BB_FIND) \
538 || defined (BB_INSMOD) \
539 || defined (BB_RM) \
540 || defined (BB_TAR)
541
Eric Andersencc8ed391999-10-05 16:24:54 +0000542/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000543 * Walk down all the directories under the specified
544 * location, and do something (something specified
545 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000546 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000547 * Unfortunatly, while nftw(3) could replace this and reduce
548 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000549 * and so isn't sufficiently portable to take over since glibc2.1
550 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000551 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000552int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 int recurse, int followLinks, int depthFirst,
554 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000555 struct stat * statbuf,
556 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000558 struct stat * statbuf,
559 void* userData),
560 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000561{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 int status;
563 struct stat statbuf;
564 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000565
Erik Andersene49d5ec2000-02-08 19:58:47 +0000566 if (followLinks == TRUE)
567 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000568 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000570
Eric Andersen2b69c401999-10-05 22:58:32 +0000571 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572#ifdef BB_DEBUG_PRINT_SCAFFOLD
573 fprintf(stderr,
574 "status=%d followLinks=%d TRUE=%d\n",
575 status, followLinks, TRUE);
576#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000577 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000578 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000579 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580
581 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
582 if (fileAction == NULL)
583 return TRUE;
584 else
Erik Andersen3364d782000-03-28 00:58:14 +0000585 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 }
587
588 if (recurse == FALSE) {
589 if (S_ISDIR(statbuf.st_mode)) {
590 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000591 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000592 else
593 return TRUE;
594 }
595 }
596
597 if (S_ISDIR(statbuf.st_mode)) {
598 DIR *dir;
599
600 dir = opendir(fileName);
601 if (!dir) {
602 perror(fileName);
603 return FALSE;
604 }
605 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000606 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 if (status == FALSE) {
608 perror(fileName);
609 return FALSE;
610 }
611 }
612 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000613 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614
615 if ((strcmp(next->d_name, "..") == 0)
616 || (strcmp(next->d_name, ".") == 0)) {
617 continue;
618 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000619 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000620 fprintf(stderr, name_too_long, "ftw");
621 return FALSE;
622 }
Erik Andersenc3660502000-04-18 21:18:57 +0000623 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 sprintf(nextFile, "%s/%s", fileName, next->d_name);
625 status =
626 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000627 fileAction, dirAction, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000628 if (status < 0) {
629 closedir(dir);
630 return FALSE;
631 }
632 }
633 status = closedir(dir);
634 if (status < 0) {
635 perror(fileName);
636 return FALSE;
637 }
638 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000639 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640 if (status == FALSE) {
641 perror(fileName);
642 return FALSE;
643 }
644 }
645 } else {
646 if (fileAction == NULL)
647 return TRUE;
648 else
Erik Andersen3364d782000-03-28 00:58:14 +0000649 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000650 }
651 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000652}
653
Erik Andersen23dea9b2000-05-13 00:28:25 +0000654#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000655
Eric Andersenf6be9441999-10-13 21:12:06 +0000656
657
Erik Andersen23dea9b2000-05-13 00:28:25 +0000658#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000659/*
660 * Attempt to create the directories along the specified path, except for
661 * the final component. The mode is given for the final directory only,
662 * while all previous ones get default protections. Errors are not reported
663 * here, as failures to restore files can be reported later.
664 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000666{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667 char *cp;
668 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000669 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000671
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 strcpy(buf, name);
673 for (cp = buf; *cp == '/'; cp++);
674 cp = strchr(cp, '/');
675 while (cp) {
676 cpOld = cp;
677 cp = strchr(cp + 1, '/');
678 *cpOld = '\0';
679 retVal = mkdir(buf, cp ? 0777 : mode);
680 if (retVal != 0 && errno != EEXIST) {
681 perror(buf);
682 return FALSE;
683 }
684 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000685 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000686 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000687}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000688#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000689
690
691
Erik Andersen23dea9b2000-05-13 00:28:25 +0000692#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000693/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000694
695
696
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000698{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 mode_t andMode =
700
701 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000702 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 mode_t mode = 0;
704 mode_t groups = 0;
705 char type;
706 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000707
708 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 for (;;) {
710 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000711 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000712 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000713 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000715 continue;
716 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000718 continue;
719 case 'o':
720 groups |= S_IRWXO;
721 continue;
722 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000724 continue;
725 case '+':
726 case '=':
727 case '-':
728 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 if (groups == 0) /* The default is "all" */
730 groups |=
731 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000732 break;
733 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000734 if (isdigit(c) && c >= '0' && c <= '7' &&
735 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000736 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000737 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000739 return (FALSE);
740 }
741 break;
742 }
743
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 while ((c = *s++) != '\0') {
745 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000746 case ',':
747 break;
748 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000750 continue;
751 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000753 continue;
754 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000755 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000756 continue;
757 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000758 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000759 continue;
760 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000761 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000762 continue;
763 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000764 *theMode &= andMode;
765 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000767 }
768 break;
769 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000771 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000772 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000773 /* fall through */
774 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000775 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000776 break;
777 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000778 andMode &= ~(mode & groups);
779 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000780 break;
781 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000783 *theMode &= andMode;
784 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000785 return (TRUE);
786}
Eric Andersence8f3b91999-10-20 07:03:36 +0000787
788
Erik Andersen23dea9b2000-05-13 00:28:25 +0000789#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000790
Eric Andersene674eb71999-10-19 20:52:57 +0000791
792
Erik Andersen23dea9b2000-05-13 00:28:25 +0000793
794
795#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR || defined BB_ID
796
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000797/* This parses entries in /etc/passwd and /etc/group. This is desirable
798 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
799 * increases target size and is often not needed or wanted for embedded
800 * systems.
801 *
802 * /etc/passwd entries look like this:
803 * root:x:0:0:root:/root:/bin/bash
804 * and /etc/group entries look like this:
805 * root:x:0:
806 *
807 * This uses buf as storage to hold things.
808 *
809 */
Erik Andersen5b46d792000-05-17 05:24:24 +0000810unsigned long my_getid(const char *filename, char *name, unsigned long id, unsigned long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000811{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000812 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000813 char *rname, *start, *end, buf[128];
Erik Andersen330fd2b2000-05-19 05:35:19 +0000814 unsigned long rid;
Erik Andersen5b46d792000-05-17 05:24:24 +0000815 unsigned long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000816
Erik Andersene49d5ec2000-02-08 19:58:47 +0000817 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000818 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000819 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000820 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000822 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000823
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000825 if (buf[0] == '#')
826 continue;
827
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000828 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000829 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000831 if (end == NULL)
832 continue;
833 *end = '\0';
834 rname = start;
835
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000836 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000837 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000838 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000839 if (end == NULL)
840 continue;
841
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000842 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000843 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000844 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000845 if (end == start)
846 continue;
847
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000848 /* gid in passwd */
849 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000850 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000851
Eric Andersend23f9ba1999-10-20 19:18:15 +0000852 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000853 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000854 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 fclose(file);
856 return (rid);
857 }
858 }
859 if (id != -1 && id == rid) {
860 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000861 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 fclose(file);
863 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000864 }
865 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000866 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000867 return (-1);
868}
869
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000870/* returns a uid given a username */
Erik Andersen5b46d792000-05-17 05:24:24 +0000871unsigned long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000872{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000873 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000874}
875
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000876/* returns a gid given a group name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000877unsigned long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000878{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000879 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000880}
881
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000882/* gets a username given a uid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000883void my_getpwuid(char *name, unsigned long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000884{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000885 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000886}
887
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000888/* gets a groupname given a gid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000889void my_getgrgid(char *group, unsigned long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000890{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000891 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000892}
893
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000894/* gets a gid given a user name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000895unsigned long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000896{
Erik Andersen5b46d792000-05-17 05:24:24 +0000897 unsigned long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000898 my_getid("/etc/passwd", name, -1, &gid);
899 return gid;
900}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000901
Erik Andersen23dea9b2000-05-13 00:28:25 +0000902#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */
903
904
905#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
Eric Andersen0460ff21999-10-25 23:32:44 +0000906
907
908#include <linux/kd.h>
909#include <sys/ioctl.h>
910
Erik Andersene49d5ec2000-02-08 19:58:47 +0000911int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000912{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000913 char arg;
914
915 arg = 0;
916 return (ioctl(fd, KDGKBTYPE, &arg) == 0
917 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000918}
919
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000921{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 int fd;
923
924 /* try read-only */
925 fd = open(fnam, O_RDWR);
926
927 /* if failed, try read-only */
928 if (fd < 0 && errno == EACCES)
929 fd = open(fnam, O_RDONLY);
930
931 /* if failed, try write-only */
932 if (fd < 0 && errno == EACCES)
933 fd = open(fnam, O_WRONLY);
934
935 /* if failed, fail */
936 if (fd < 0)
937 return -1;
938
939 /* if not a console, fail */
940 if (!is_a_console(fd)) {
941 close(fd);
942 return -1;
943 }
944
945 /* success */
946 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000947}
948
949/*
950 * Get an fd for use with kbd/console ioctls.
951 * We try several things because opening /dev/console will fail
952 * if someone else used X (which does a chown on /dev/console).
953 *
954 * if tty_name is non-NULL, try this one instead.
955 */
956
Erik Andersene49d5ec2000-02-08 19:58:47 +0000957int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000958{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000959 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000960
Erik Andersene49d5ec2000-02-08 19:58:47 +0000961 if (tty_name) {
962 if (-1 == (fd = open_a_console(tty_name)))
963 return -1;
964 else
965 return fd;
966 }
967
968 fd = open_a_console("/dev/tty");
969 if (fd >= 0)
970 return fd;
971
972 fd = open_a_console("/dev/tty0");
973 if (fd >= 0)
974 return fd;
975
976 fd = open_a_console("/dev/console");
977 if (fd >= 0)
978 return fd;
979
980 for (fd = 0; fd < 3; fd++)
981 if (is_a_console(fd))
982 return fd;
983
984 fprintf(stderr,
985 "Couldnt get a file descriptor referring to the console\n");
986 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000987}
988
989
Erik Andersen23dea9b2000-05-13 00:28:25 +0000990#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000991
992
Erik Andersen23dea9b2000-05-13 00:28:25 +0000993#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
994
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000995/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000997{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000998 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000999
Erik Andersene49d5ec2000-02-08 19:58:47 +00001000 while (*haystack) {
1001 if (!strncasecmp(haystack, needle, len))
1002 break;
1003 haystack++;
1004 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001005
Erik Andersene49d5ec2000-02-08 19:58:47 +00001006 if (!(*haystack))
1007 haystack = NULL;
1008
1009 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001010}
1011
Eric Andersenc1525e81999-10-29 00:07:31 +00001012/* This tries to find a needle in a haystack, but does so by
1013 * only trying to match literal strings (look 'ma, no regexps!)
1014 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001015 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +00001016 * -Erik Andersen
1017 */
1018extern int find_match(char *haystack, char *needle, int ignoreCase)
1019{
1020
Erik Andersene49d5ec2000-02-08 19:58:47 +00001021 if (ignoreCase == FALSE)
1022 haystack = strstr(haystack, needle);
1023 else
1024 haystack = stristr(haystack, needle);
1025 if (haystack == NULL)
1026 return FALSE;
1027 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001028}
1029
1030
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001031/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032extern int replace_match(char *haystack, char *needle, char *newNeedle,
1033 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +00001034{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 int foundOne = 0;
1036 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +00001037
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 if (ignoreCase == FALSE)
1039 where = strstr(haystack, needle);
1040 else
1041 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001042
Erik Andersene49d5ec2000-02-08 19:58:47 +00001043 if (strcmp(needle, newNeedle) == 0)
1044 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001045
Erik Andersen0d068a22000-03-21 22:32:57 +00001046 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +00001047 while (where != NULL) {
1048 foundOne++;
1049 strcpy(oldhayStack, haystack);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 for (slider = haystack, slider1 = oldhayStack; slider != where;
1051 slider++, slider1++);
1052 *slider = 0;
1053 haystack = strcat(haystack, newNeedle);
1054 slider1 += strlen(needle);
1055 haystack = strcat(haystack, slider1);
1056 where = strstr(slider, needle);
1057 }
1058 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001059
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 if (foundOne > 0)
1061 return TRUE;
1062 else
1063 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001064}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001065
1066#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001067
1068
Eric Andersen9f16d612000-06-12 23:11:16 +00001069#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001070/*
1071 * Routine to see if a text string is matched by a wildcard pattern.
1072 * Returns TRUE if the text is matched, or FALSE if it is not matched
1073 * or if the pattern is invalid.
1074 * * matches zero or more characters
1075 * ? matches a single character
1076 * [abc] matches 'a', 'b' or 'c'
1077 * \c quotes character c
1078 * Adapted from code written by Ingo Wilken, and
1079 * then taken from sash, Copyright (c) 1999 by David I. Bell
1080 * Permission is granted to use, distribute, or modify this source,
1081 * provided that this copyright notice remains intact.
1082 * Permission to distribute this code under the GPL has been granted.
1083 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001084extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001085{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 const char *retryPat;
1087 const char *retryText;
1088 int ch;
1089 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001090 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001091
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 retryPat = NULL;
1093 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001094
Erik Andersene49d5ec2000-02-08 19:58:47 +00001095 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001096 ch = *pattern++;
1097
Erik Andersene49d5ec2000-02-08 19:58:47 +00001098 switch (ch) {
1099 case '*':
1100 retryPat = pattern;
1101 retryText = text;
1102 break;
1103
1104 case '[':
1105 found = FALSE;
1106
1107 while ((ch = *pattern++) != ']') {
1108 if (ch == '\\')
1109 ch = *pattern++;
1110
1111 if (ch == '\0')
1112 return FALSE;
1113
1114 if (*text == ch)
1115 found = TRUE;
1116 }
Erik Andersen632bb572000-04-18 22:09:06 +00001117 len=strlen(text);
1118 if (found == FALSE && len!=0) {
1119 return FALSE;
1120 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001122 if (strlen(pattern)==0 && len==1) {
1123 return TRUE;
1124 }
1125 if (len!=0) {
1126 text++;
1127 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001128 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 }
1130
1131 /* fall into next case */
1132
1133 case '?':
1134 if (*text++ == '\0')
1135 return FALSE;
1136
1137 break;
1138
1139 case '\\':
1140 ch = *pattern++;
1141
1142 if (ch == '\0')
1143 return FALSE;
1144
1145 /* fall into next case */
1146
1147 default:
1148 if (*text == ch) {
1149 if (*text)
1150 text++;
1151 break;
1152 }
1153
1154 if (*text) {
1155 pattern = retryPat;
1156 text = ++retryText;
1157 break;
1158 }
1159
Eric Andersenb186d981999-12-03 09:19:54 +00001160 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001161 }
1162
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 if (pattern == NULL)
1164 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001165 }
1166
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001168}
Eric Andersen9f16d612000-06-12 23:11:16 +00001169#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001170
1171
Eric Andersen29d2e361999-11-06 06:07:27 +00001172
1173
Erik Andersen23dea9b2000-05-13 00:28:25 +00001174#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001175/*
1176 * Given a block device, find the mount table entry if that block device
1177 * is mounted.
1178 *
1179 * Given any other file (or directory), find the mount table entry for its
1180 * filesystem.
1181 */
1182extern struct mntent *findMountPoint(const char *name, const char *table)
1183{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 struct stat s;
1185 dev_t mountDevice;
1186 FILE *mountTable;
1187 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001188
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 if (stat(name, &s) != 0)
1190 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001191
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 if ((s.st_mode & S_IFMT) == S_IFBLK)
1193 mountDevice = s.st_rdev;
1194 else
1195 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001196
1197
Erik Andersene49d5ec2000-02-08 19:58:47 +00001198 if ((mountTable = setmntent(table, "r")) == 0)
1199 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001200
Erik Andersene49d5ec2000-02-08 19:58:47 +00001201 while ((mountEntry = getmntent(mountTable)) != 0) {
Eric Andersen10dc9d42000-06-26 10:45:52 +00001202 if (strcmp(mountEntry->mnt_fsname, "none") == 0) {
1203 continue;
1204 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001205 if (strcmp(name, mountEntry->mnt_dir) == 0
1206 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1207 break;
1208 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1209 break;
1210 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1211 break;
1212 }
1213 endmntent(mountTable);
1214 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001215}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001216#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001217
1218
1219
Erik Andersen23dea9b2000-05-13 00:28:25 +00001220#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001221/*
1222 * Read a number with a possible multiplier.
1223 * Returns -1 if the number format is illegal.
1224 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001225extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001226{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001227 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001228
Erik Andersene49d5ec2000-02-08 19:58:47 +00001229 if (!isDecimal(*cp))
1230 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001231
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001233
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234 while (isDecimal(*cp))
1235 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001236
Erik Andersene49d5ec2000-02-08 19:58:47 +00001237 switch (*cp++) {
1238 case 'M':
1239 case 'm': /* `tail' uses it traditionally */
1240 value *= 1048576;
1241 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001242
Erik Andersene49d5ec2000-02-08 19:58:47 +00001243 case 'k':
1244 value *= 1024;
1245 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001246
Erik Andersene49d5ec2000-02-08 19:58:47 +00001247 case 'b':
1248 value *= 512;
1249 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001250
Erik Andersene49d5ec2000-02-08 19:58:47 +00001251 case 'w':
1252 value *= 2;
1253 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001254
Erik Andersene49d5ec2000-02-08 19:58:47 +00001255 case '\0':
1256 return value;
1257
1258 default:
1259 return -1;
1260 }
1261
1262 if (*cp)
1263 return -1;
1264
Eric Andersen1792f8c1999-12-09 06:11:36 +00001265 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001266}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001267#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001268
Eric Andersen6805d5d1999-12-09 22:39:55 +00001269
Erik Andersen23dea9b2000-05-13 00:28:25 +00001270#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001271/* try to open up the specified device */
1272extern int device_open(char *device, int mode)
1273{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001274 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001275
Erik Andersene49d5ec2000-02-08 19:58:47 +00001276 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001277
Erik Andersene49d5ec2000-02-08 19:58:47 +00001278 /* Retry up to 5 times */
1279 for (f = 0; f < 5; f++)
1280 if ((fd = open(device, m, 0600)) >= 0)
1281 break;
1282 if (fd < 0)
1283 return fd;
1284 /* Reset original flags. */
1285 if (m != mode)
1286 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001287 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001288}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001289#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001290
1291
Erik Andersen23dea9b2000-05-13 00:28:25 +00001292#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001293#ifdef BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001294#include <linux/devps.h>
Erik Andersen23dea9b2000-05-13 00:28:25 +00001295#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001296
Erik Andersen23dea9b2000-05-13 00:28:25 +00001297#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001298/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001299 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001300 * This finds the pid of the specified process,
1301 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001302 *
Erik Andersen825aead2000-04-07 06:00:07 +00001303 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001304 */
Erik Andersen825aead2000-04-07 06:00:07 +00001305extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001306{
Erik Andersen825aead2000-04-07 06:00:07 +00001307 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001308 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001309 pid_t num_pids;
1310 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001311 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001312
1313 /* open device */
1314 fd = open(device, O_RDONLY);
1315 if (fd < 0)
1316 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1317
1318 /* Find out how many processes there are */
1319 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1320 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1321
1322 /* Allocate some memory -- grab a few extras just in case
1323 * some new processes start up while we wait. The kernel will
1324 * just ignore any extras if we give it too many, and will trunc.
1325 * the list if we give it too few. */
1326 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1327 pid_array[0] = num_pids+10;
1328
1329 /* Now grab the pid list */
1330 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1331 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1332
1333 /* Now search for a match */
1334 for (i=1; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001335 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001336 struct pid_info info;
1337
1338 info.pid = pid_array[i];
1339 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1340 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1341
Erik Andersen0a64de92000-04-18 20:00:03 +00001342 /* Make sure we only match on the process name */
1343 p=info.command_line+1;
1344 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1345 (p)++;
1346 }
1347 if (isspace(*(p)))
1348 *p='\0';
1349
Erik Andersenb9167cb2000-04-18 22:41:30 +00001350 if ((strstr(info.command_line, pidName) != NULL)
1351 && (strlen(pidName) == strlen(info.command_line))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001352 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1353 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001354 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001355 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001356 }
1357 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001358 if (pidList)
1359 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001360
1361 /* Free memory */
1362 free( pid_array);
1363
1364 /* close device */
1365 if (close (fd) != 0)
1366 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1367
Erik Andersen825aead2000-04-07 06:00:07 +00001368 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001369}
Erik Andersen269bba22000-03-08 14:50:47 +00001370#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001371#if ! defined BB_FEATURE_USE_PROCFS
1372#error Sorry, I depend on the /proc filesystem right now.
1373#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001374
Erik Andersen246cc6d2000-03-07 07:41:42 +00001375/* findPidByName()
1376 *
1377 * This finds the pid of the specified process.
1378 * Currently, it's implemented by rummaging through
1379 * the proc filesystem.
1380 *
Erik Andersen825aead2000-04-07 06:00:07 +00001381 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001382 */
Erik Andersen825aead2000-04-07 06:00:07 +00001383extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001384{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001385 DIR *dir;
1386 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001387 pid_t* pidList=NULL;
1388 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001389
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001390 dir = opendir("/proc");
1391 if (!dir)
1392 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1393
1394 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001395 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001396 char filename[256];
1397 char buffer[256];
Erik Andersenb9167cb2000-04-18 22:41:30 +00001398 char* p;
John Beppuf95ca971999-12-09 22:10:18 +00001399
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001400 /* If it isn't a number, we don't want it */
1401 if (!isdigit(*next->d_name))
1402 continue;
1403
Erik Andersen825aead2000-04-07 06:00:07 +00001404 /* Now open the status file */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001405 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001406 status = fopen(filename, "r");
1407 if (!status) {
1408 continue;
1409 }
1410 fgets(buffer, 256, status);
1411 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001412
Erik Andersen0a64de92000-04-18 20:00:03 +00001413 /* Make sure we only match on the process name */
1414 p=buffer+5; /* Skip the name */
1415 while ((p)++) {
1416 if (*p==0 || *p=='\n') {
1417 *p='\0';
1418 break;
1419 }
1420 }
1421 p=buffer+6; /* Skip the "Name:\t" */
1422
Erik Andersenb9167cb2000-04-18 22:41:30 +00001423 if ((strstr(p, pidName) != NULL)
1424 && (strlen(pidName) == strlen(p))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001425 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1426 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001427 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001428 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001429 }
John Beppuf95ca971999-12-09 22:10:18 +00001430 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001431 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001432 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001433 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001434}
Erik Andersen269bba22000-03-08 14:50:47 +00001435#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001436#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001437
Erik Andersen0d068a22000-03-21 22:32:57 +00001438/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001439extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001440{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001441 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001442
Erik Andersen0d068a22000-03-21 22:32:57 +00001443 if (cp == NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001444 fatalError(memory_exhausted, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001445 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001446}
1447
Erik Andersen23dea9b2000-05-13 00:28:25 +00001448#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001449extern int vdprintf(int d, const char *format, va_list ap)
1450{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001451 char buf[BUF_SIZE];
1452 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001453
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454 len = vsprintf(buf, format, ap);
1455 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001456}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001457#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001458
Erik Andersen23dea9b2000-05-13 00:28:25 +00001459#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen5cbdd712000-01-26 20:06:48 +00001460extern int del_loop(const char *device)
1461{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001462 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001463
Erik Andersene49d5ec2000-02-08 19:58:47 +00001464 if ((fd = open(device, O_RDONLY)) < 0) {
1465 perror(device);
1466 return (FALSE);
1467 }
1468 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1469 perror("ioctl: LOOP_CLR_FD");
1470 return (FALSE);
1471 }
1472 close(fd);
1473 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001474}
Erik Andersende7965c2000-01-26 23:49:21 +00001475
Erik Andersene49d5ec2000-02-08 19:58:47 +00001476extern int set_loop(const char *device, const char *file, int offset,
1477 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001478{
1479 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001480 int fd, ffd, mode;
1481
Erik Andersende7965c2000-01-26 23:49:21 +00001482 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001483 if ((ffd = open(file, mode)) < 0 && !*loopro
1484 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1485 perror(file);
1486 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001487 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001488 if ((fd = open(device, mode)) < 0) {
1489 close(ffd);
1490 perror(device);
1491 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001492 }
1493 *loopro = (mode == O_RDONLY);
1494
1495 memset(&loopinfo, 0, sizeof(loopinfo));
1496 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001497 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001498
1499 loopinfo.lo_offset = offset;
1500
1501 loopinfo.lo_encrypt_key_size = 0;
1502 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1503 perror("ioctl: LOOP_SET_FD");
1504 close(fd);
1505 close(ffd);
1506 return 1;
1507 }
1508 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1509 (void) ioctl(fd, LOOP_CLR_FD, 0);
1510 perror("ioctl: LOOP_SET_STATUS");
1511 close(fd);
1512 close(ffd);
1513 return 1;
1514 }
1515 close(fd);
1516 close(ffd);
1517 return 0;
1518}
1519
Erik Andersene49d5ec2000-02-08 19:58:47 +00001520extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001521{
1522 char dev[20];
1523 int i, fd;
1524 struct stat statbuf;
1525 struct loop_info loopinfo;
1526
Erik Andersene49d5ec2000-02-08 19:58:47 +00001527 for (i = 0; i <= 7; i++) {
1528 sprintf(dev, "/dev/loop%d", i);
1529 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1530 if ((fd = open(dev, O_RDONLY)) >= 0) {
1531 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1532 if (errno == ENXIO) { /* probably free */
1533 close(fd);
1534 return strdup(dev);
1535 }
1536 }
1537 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001538 }
Erik Andersende7965c2000-01-26 23:49:21 +00001539 }
Erik Andersende7965c2000-01-26 23:49:21 +00001540 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001541 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001542}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001543#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001544
Erik Andersen23dea9b2000-05-13 00:28:25 +00001545#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001546extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001547{
Erik Andersen016ffe92000-03-22 00:58:54 +00001548 DIR *dir;
1549 struct dirent *entry;
1550 struct stat statBuf, rootStat;
1551 char fileName[BUFSIZ];
1552
Erik Andersenec5bd902000-03-22 07:12:05 +00001553 if (stat("/", &rootStat) != 0) {
1554 errorMsg("could not stat '/'\n");
1555 return( FALSE);
1556 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001557
Erik Andersenec5bd902000-03-22 07:12:05 +00001558 dir = opendir("/dev");
1559 if (!dir) {
1560 errorMsg("could not open '/dev'\n");
1561 return( FALSE);
1562 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001563
1564 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001565
Erik Andersen016ffe92000-03-22 00:58:54 +00001566 /* Must skip ".." since that is "/", and so we
1567 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001568 if (strcmp(entry->d_name, "..") == 0)
1569 continue;
1570
1571 sprintf( fileName, "/dev/%s", entry->d_name);
1572
1573 if (stat(fileName, &statBuf) != 0)
1574 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001575 /* Some char devices have the same dev_t as block
1576 * devices, so make sure this is a block device */
1577 if (! S_ISBLK(statBuf.st_mode))
1578 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001579 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001580 strcpy(name, fileName);
1581 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001582 }
1583 }
1584
Erik Andersenec5bd902000-03-22 07:12:05 +00001585 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001586}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001587#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001588
John Beppuc7ccfc92000-06-12 23:39:04 +00001589const unsigned int CSTRING_BUFFER_LENGTH = 1024;
John Beppu5a728cf2000-04-17 04:22:09 +00001590/* recursive parser that returns cstrings of arbitrary length
1591 * from a FILE*
1592 */
1593static char *
1594cstring_alloc(FILE* f, int depth)
1595{
1596 char *cstring;
1597 char buffer[CSTRING_BUFFER_LENGTH];
1598 int target = CSTRING_BUFFER_LENGTH * depth;
Eric Andersen86ab8a32000-06-02 03:21:42 +00001599 int c, i, len, size;
John Beppu5a728cf2000-04-17 04:22:09 +00001600
1601 /* fill buffer */
1602 i = 0;
Eric Andersen86ab8a32000-06-02 03:21:42 +00001603 while ((c = fgetc(f)) != EOF) {
1604 buffer[i] = (char) c;
John Beppu5a728cf2000-04-17 04:22:09 +00001605 if (buffer[i++] == 0x0a) { break; }
1606 if (i == CSTRING_BUFFER_LENGTH) { break; }
1607 }
1608 len = i;
1609
1610 /* recurse or malloc? */
1611 if (len == CSTRING_BUFFER_LENGTH) {
1612 cstring = cstring_alloc(f, (depth + 1));
1613 } else {
1614 /* [special case] EOF */
1615 if ((depth | len) == 0) { return NULL; }
1616
1617 /* malloc */
1618 size = target + len + 1;
1619 cstring = malloc(size);
1620 if (!cstring) { return NULL; }
1621 cstring[size - 1] = 0;
1622 }
1623
1624 /* copy buffer */
1625 if (cstring) {
1626 memcpy(&cstring[target], buffer, len);
1627 }
1628 return cstring;
1629}
1630
1631/*
1632 * wrapper around recursive cstring_alloc
1633 * it's the caller's responsibility to free the cstring
1634 */
1635char *
1636cstring_lineFromFile(FILE *f)
1637{
1638 return cstring_alloc(f, 0);
1639}
Erik Andersen016ffe92000-03-22 00:58:54 +00001640
Eric Andersencc8ed391999-10-05 16:24:54 +00001641/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001642/*
1643Local Variables:
1644c-file-style: "linux"
1645c-basic-offset: 4
1646tab-width: 4
1647End:
1648*/