blob: 5899fe954370aa8d4fbf747e81991164593ba9bd [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
37#define BB_DECLARE_EXTERN
38#include "messages.c"
39
Eric Andersen2b69c401999-10-05 22:58:32 +000040#include <stdio.h>
41#include <string.h>
42#include <errno.h>
43#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000044#include <dirent.h>
45#include <time.h>
46#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000047#include <sys/stat.h>
48#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000049#include <ctype.h>
Erik Andersend07ee462000-02-21 21:26:32 +000050#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Erik Andersen5cbdd712000-01-26 20:06:48 +000052#if defined BB_FEATURE_MOUNT_LOOP
53#include <fcntl.h>
54#include <sys/ioctl.h>
55#include <linux/loop.h>
56#endif
57
Erik Andersen246cc6d2000-03-07 07:41:42 +000058/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
59 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000060#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000061//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
62#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
63#endif
64
Erik Andersen5cbdd712000-01-26 20:06:48 +000065
Eric Andersend0246fb1999-11-04 21:18:07 +000066#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000067# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000068const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000069# else
70# if defined BB_MTAB
71const char mtab_file[] = "/etc/mtab";
72# else
Erik Andersen269bba22000-03-08 14:50:47 +000073# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000074const char mtab_file[] = "/dev/mtab";
75# else
Erik Andersen269bba22000-03-08 14:50:47 +000076# 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 +000077# endif
78# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000079# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000080#endif
81
82
Eric Andersend73dc5b1999-11-10 23:13:02 +000083extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000084{
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
86 BB_VER, BB_BT);
87 fprintf(stderr, "Usage: %s\n", usage);
88 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000089}
90
Erik Andersen1ad302a2000-03-24 00:54:46 +000091extern void errorMsg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000092{
93 va_list p;
94
95 va_start(p, s);
96 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000097 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000098 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +000099 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000100}
101
Erik Andersen1ad302a2000-03-24 00:54:46 +0000102extern void fatalError(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000103{
104 va_list p;
105
106 va_start(p, s);
107 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000108 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000109 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000110 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000111 exit( FALSE);
112}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000113
Erik Andersen298854f2000-03-23 01:09:18 +0000114#if defined BB_INIT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000115/* Returns kernel version encoded as major*65536 + minor*256 + patch,
116 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000117 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000118 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000119extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000120{
Erik Andersend07ee462000-02-21 21:26:32 +0000121 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000123
Erik Andersend07ee462000-02-21 21:26:32 +0000124 if (uname(&name) == -1) {
125 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 return (0);
127 }
Erik Andersend07ee462000-02-21 21:26:32 +0000128 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000130}
Erik Andersen298854f2000-03-23 01:09:18 +0000131#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000132
Erik Andersen029011b2000-03-04 21:19:32 +0000133#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000134
Erik Andersen029011b2000-03-04 21:19:32 +0000135#define HASH_SIZE 311 /* Should be prime */
136#define hash_inode(i) ((i) % HASH_SIZE)
137
138static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
139
140/*
141 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
142 * `ino_dev_hashtable', else return 0
143 *
144 * If NAME is a non-NULL pointer to a character pointer, and there is
145 * a match, then set *NAME to the value of the name slot in that
146 * bucket.
147 */
148int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
149{
150 ino_dev_hashtable_bucket_t *bucket;
151
152 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
153 while (bucket != NULL) {
154 if ((bucket->ino == statbuf->st_ino) &&
155 (bucket->dev == statbuf->st_dev))
156 {
157 if (name) *name = bucket->name;
158 return 1;
159 }
160 bucket = bucket->next;
161 }
162 return 0;
163}
164
165/* Add statbuf to statbuf hash table */
166void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
167{
168 int i;
169 size_t s;
170 ino_dev_hashtable_bucket_t *bucket;
171
172 i = hash_inode(statbuf->st_ino);
173 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000174 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000175 bucket->ino = statbuf->st_ino;
176 bucket->dev = statbuf->st_dev;
177 if (name)
178 strcpy(bucket->name, name);
179 else
180 bucket->name[0] = '\0';
181 bucket->next = ino_dev_hashtable[i];
182 ino_dev_hashtable[i] = bucket;
183}
184
185/* Clear statbuf hash table */
186void reset_ino_dev_hashtable(void)
187{
188 int i;
189 ino_dev_hashtable_bucket_t *bucket;
190
191 for (i = 0; i < HASH_SIZE; i++) {
192 while (ino_dev_hashtable[i] != NULL) {
193 bucket = ino_dev_hashtable[i]->next;
194 free(ino_dev_hashtable[i]);
195 ino_dev_hashtable[i] = bucket;
196 }
197 }
198}
199
200#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000201
Erik Andersenfac10d72000-02-07 05:29:42 +0000202#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000203/*
204 * Return TRUE if a fileName is a directory.
205 * Nonexistant files return FALSE.
206 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000207int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000208{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000210 int didMalloc = 0;
211
212 if (statBuf == NULL) {
213 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
214 ++didMalloc;
215 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000216
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000218 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000220 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000221
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000222 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
223 status = FALSE;
224 }
225 else status = TRUE;
226
227 if (didMalloc) {
228 free(statBuf);
229 statBuf = NULL;
230 }
231 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000232}
Erik Andersenfac10d72000-02-07 05:29:42 +0000233#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000234
Erik Andersenfac10d72000-02-07 05:29:42 +0000235#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000236/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000237 * Copy one file to another, while possibly preserving its modes, times, and
238 * modes. Returns TRUE if successful, or FALSE on a failure with an error
239 * message output. (Failure is not indicated if attributes cannot be set.)
240 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000241 */
242int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000244 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000245{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 int rfd;
247 int wfd;
248 int rcc;
249 int status;
250 char buf[BUF_SIZE];
251 struct stat srcStatBuf;
252 struct stat dstStatBuf;
253 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000254
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 if (followLinks == TRUE)
256 status = stat(srcName, &srcStatBuf);
257 else
258 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000259
Erik Andersenfac10d72000-02-07 05:29:42 +0000260 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261 perror(srcName);
262 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000263 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264
265 if (followLinks == TRUE)
266 status = stat(destName, &dstStatBuf);
267 else
268 status = lstat(destName, &dstStatBuf);
269
Erik Andersen59b9e872000-05-10 05:05:45 +0000270 if (status < 0 || forceFlag==TRUE) {
271 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 dstStatBuf.st_ino = -1;
273 dstStatBuf.st_dev = -1;
274 }
275
276 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
277 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
278 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
279 return FALSE;
280 }
281
282 if (S_ISDIR(srcStatBuf.st_mode)) {
283 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
284 /* Make sure the directory is writable */
285 status = mkdir(destName, 0777777 ^ umask(0));
286 if (status < 0 && errno != EEXIST) {
287 perror(destName);
288 return FALSE;
289 }
290 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000291 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292 int link_size;
293
294 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000295 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
296 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 if (link_size < 0) {
298 perror(srcName);
299 return FALSE;
300 }
301 link_val[link_size] = '\0';
302 status = symlink(link_val, destName);
303 if (status < 0) {
304 perror(destName);
305 return FALSE;
306 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000307#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000309 /* Try to set owner, but fail silently like GNU cp */
310 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000312#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 return TRUE;
314 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
315 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
316 if (mkfifo(destName, 0644) < 0) {
317 perror(destName);
318 return FALSE;
319 }
320 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
321 || S_ISSOCK(srcStatBuf.st_mode)) {
322 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
323 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
324 perror(destName);
325 return FALSE;
326 }
327 } else if (S_ISREG(srcStatBuf.st_mode)) {
328 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
329 rfd = open(srcName, O_RDONLY);
330 if (rfd < 0) {
331 perror(srcName);
332 return FALSE;
333 }
334
335 wfd =
336 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
337 srcStatBuf.st_mode);
338 if (wfd < 0) {
339 perror(destName);
340 close(rfd);
341 return FALSE;
342 }
343
344 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
345 if (fullWrite(wfd, buf, rcc) < 0)
346 goto error_exit;
347 }
348 if (rcc < 0) {
349 goto error_exit;
350 }
351
352 close(rfd);
353 if (close(wfd) < 0) {
354 return FALSE;
355 }
356 }
357
358 if (setModes == TRUE) {
359 /* This is fine, since symlinks never get here */
360 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
361 perror(destName);
362 exit FALSE;
363 }
364 if (chmod(destName, srcStatBuf.st_mode) < 0) {
365 perror(destName);
366 exit FALSE;
367 }
368 times.actime = srcStatBuf.st_atime;
369 times.modtime = srcStatBuf.st_mtime;
370 if (utime(destName, &times) < 0) {
371 perror(destName);
372 exit FALSE;
373 }
374 }
375
Erik Andersenfac10d72000-02-07 05:29:42 +0000376 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000377
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378 error_exit:
379 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000380 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000382
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000384}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000386
387
388
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000389#if defined BB_TAR || defined BB_LS
390
391#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
392#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
393
394/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
395static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 0, 0, S_ISUID,
397 0, 0, S_ISGID,
398 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000399};
400
401/* The 9 mode bits to test */
402static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 S_IRUSR, S_IWUSR, S_IXUSR,
404 S_IRGRP, S_IWGRP, S_IXGRP,
405 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000406};
407
408#define MODE1 "rwxrwxrwx"
409#define MODE0 "---------"
410#define SMODE1 "..s..s..t"
411#define SMODE0 "..S..S..T"
412
Eric Andersencc8ed391999-10-05 16:24:54 +0000413/*
414 * Return the standard ls-like mode string from a file mode.
415 * This is static and so is overwritten on each call.
416 */
Eric Andersenf811e071999-10-09 00:25:00 +0000417const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000418{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000420
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421 int i;
422
423 buf[0] = TYPECHAR(mode);
424 for (i = 0; i < 9; i++) {
425 if (mode & SBIT[i])
426 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
427 else
428 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
429 }
430 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000431}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000433
434
Eric Andersen1792f8c1999-12-09 06:11:36 +0000435#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000436/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000437 * Return the standard ls-like time string from a time_t
438 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000439 */
Eric Andersenf811e071999-10-09 00:25:00 +0000440const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000441{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 time_t now;
443 char *str;
444 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000445
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000447
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000449
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 strcpy(buf, &str[4]);
451 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000452
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
454 strcpy(&buf[7], &str[20]);
455 buf[11] = '\0';
456 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000457
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000459}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000460#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000461
Erik Andersenfac10d72000-02-07 05:29:42 +0000462#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000463/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000464 * Write all of the supplied buffer out to a file.
465 * This does multiple writes as necessary.
466 * Returns the amount written, or -1 on an error.
467 */
Eric Andersenf811e071999-10-09 00:25:00 +0000468int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000469{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470 int cc;
471 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000472
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000474
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475 while (len > 0) {
476 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000477
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 if (cc < 0)
479 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000480
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 buf += cc;
482 total += cc;
483 len -= cc;
484 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000485
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000487}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000489
490
Eric Andersen1792f8c1999-12-09 06:11:36 +0000491#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000492/*
493 * Read all of the supplied buffer from a file.
494 * This does multiple reads as necessary.
495 * Returns the amount read, or -1 on an error.
496 * A short read is returned on an end of file.
497 */
Eric Andersenf811e071999-10-09 00:25:00 +0000498int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000499{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 int cc;
501 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000502
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000504
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 while (len > 0) {
506 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000507
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 if (cc < 0)
509 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000510
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 if (cc == 0)
512 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000513
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 buf += cc;
515 total += cc;
516 len -= cc;
517 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000520}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000522
523
Erik Andersenfac10d72000-02-07 05:29:42 +0000524#if defined (BB_CHMOD_CHOWN_CHGRP) \
Erik Andersen9a8195c2000-04-18 23:32:10 +0000525 || defined (BB_CP_MV) \
526 || defined (BB_FIND) \
527 || defined (BB_INSMOD) \
528 || defined (BB_RM) \
529 || defined (BB_TAR)
530
Eric Andersencc8ed391999-10-05 16:24:54 +0000531/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000532 * Walk down all the directories under the specified
533 * location, and do something (something specified
534 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000535 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000536 * Unfortunatly, while nftw(3) could replace this and reduce
537 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000538 * and so isn't sufficiently portable to take over since glibc2.1
539 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000540 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000541int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 int recurse, int followLinks, int depthFirst,
543 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000544 struct stat * statbuf,
545 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000547 struct stat * statbuf,
548 void* userData),
549 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000550{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 int status;
552 struct stat statbuf;
553 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000554
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 if (followLinks == TRUE)
556 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000557 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000559
Eric Andersen2b69c401999-10-05 22:58:32 +0000560 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561#ifdef BB_DEBUG_PRINT_SCAFFOLD
562 fprintf(stderr,
563 "status=%d followLinks=%d TRUE=%d\n",
564 status, followLinks, TRUE);
565#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000566 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000567 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000568 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569
570 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
571 if (fileAction == NULL)
572 return TRUE;
573 else
Erik Andersen3364d782000-03-28 00:58:14 +0000574 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 }
576
577 if (recurse == FALSE) {
578 if (S_ISDIR(statbuf.st_mode)) {
579 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000580 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581 else
582 return TRUE;
583 }
584 }
585
586 if (S_ISDIR(statbuf.st_mode)) {
587 DIR *dir;
588
589 dir = opendir(fileName);
590 if (!dir) {
591 perror(fileName);
592 return FALSE;
593 }
594 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000595 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 if (status == FALSE) {
597 perror(fileName);
598 return FALSE;
599 }
600 }
601 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000602 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603
604 if ((strcmp(next->d_name, "..") == 0)
605 || (strcmp(next->d_name, ".") == 0)) {
606 continue;
607 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000608 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 fprintf(stderr, name_too_long, "ftw");
610 return FALSE;
611 }
Erik Andersenc3660502000-04-18 21:18:57 +0000612 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 sprintf(nextFile, "%s/%s", fileName, next->d_name);
614 status =
615 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000616 fileAction, dirAction, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 if (status < 0) {
618 closedir(dir);
619 return FALSE;
620 }
621 }
622 status = closedir(dir);
623 if (status < 0) {
624 perror(fileName);
625 return FALSE;
626 }
627 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000628 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629 if (status == FALSE) {
630 perror(fileName);
631 return FALSE;
632 }
633 }
634 } else {
635 if (fileAction == NULL)
636 return TRUE;
637 else
Erik Andersen3364d782000-03-28 00:58:14 +0000638 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639 }
640 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000641}
642
Erik Andersene49d5ec2000-02-08 19:58:47 +0000643#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000644
Eric Andersenf6be9441999-10-13 21:12:06 +0000645
646
Erik Andersena8991081999-12-29 03:34:00 +0000647#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000648/*
649 * Attempt to create the directories along the specified path, except for
650 * the final component. The mode is given for the final directory only,
651 * while all previous ones get default protections. Errors are not reported
652 * here, as failures to restore files can be reported later.
653 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000654extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000655{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000656 char *cp;
657 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000658 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000660
Erik Andersene49d5ec2000-02-08 19:58:47 +0000661 strcpy(buf, name);
662 for (cp = buf; *cp == '/'; cp++);
663 cp = strchr(cp, '/');
664 while (cp) {
665 cpOld = cp;
666 cp = strchr(cp + 1, '/');
667 *cpOld = '\0';
668 retVal = mkdir(buf, cp ? 0777 : mode);
669 if (retVal != 0 && errno != EEXIST) {
670 perror(buf);
671 return FALSE;
672 }
673 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000674 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000676}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000678
679
680
681#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000682/* [ugoa]{+|-|=}[rwxst] */
683
684
685
Erik Andersene49d5ec2000-02-08 19:58:47 +0000686extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000687{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000688 mode_t andMode =
689
690 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000691 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692 mode_t mode = 0;
693 mode_t groups = 0;
694 char type;
695 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000696
697 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000698 for (;;) {
699 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000700 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000701 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000702 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000704 continue;
705 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000707 continue;
708 case 'o':
709 groups |= S_IRWXO;
710 continue;
711 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000713 continue;
714 case '+':
715 case '=':
716 case '-':
717 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 if (groups == 0) /* The default is "all" */
719 groups |=
720 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000721 break;
722 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 if (isdigit(c) && c >= '0' && c <= '7' &&
724 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000725 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000726 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000728 return (FALSE);
729 }
730 break;
731 }
732
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 while ((c = *s++) != '\0') {
734 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000735 case ',':
736 break;
737 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000739 continue;
740 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000742 continue;
743 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000745 continue;
746 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000748 continue;
749 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000750 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000751 continue;
752 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000753 *theMode &= andMode;
754 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000755 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000756 }
757 break;
758 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000760 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000761 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000762 /* fall through */
763 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000764 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000765 break;
766 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000767 andMode &= ~(mode & groups);
768 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000769 break;
770 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000772 *theMode &= andMode;
773 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000774 return (TRUE);
775}
Eric Andersence8f3b91999-10-20 07:03:36 +0000776
777
Erik Andersene49d5ec2000-02-08 19:58:47 +0000778#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000779
Eric Andersene674eb71999-10-19 20:52:57 +0000780
781
Eric Andersend23f9ba1999-10-20 19:18:15 +0000782
783
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000784#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR || defined BB_ID
Eric Andersend23f9ba1999-10-20 19:18:15 +0000785
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000786/* This parses entries in /etc/passwd and /etc/group. This is desirable
787 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
788 * increases target size and is often not needed or wanted for embedded
789 * systems.
790 *
791 * /etc/passwd entries look like this:
792 * root:x:0:0:root:/root:/bin/bash
793 * and /etc/group entries look like this:
794 * root:x:0:
795 *
796 * This uses buf as storage to hold things.
797 *
798 */
799uid_t my_getid(const char *filename, char *name, uid_t id, gid_t *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000800{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000801 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000802 char *rname, *start, *end, buf[128];
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000803 id_t rid;
804 gid_t rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000805
Erik Andersene49d5ec2000-02-08 19:58:47 +0000806 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000807 if (file == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000808 perror(filename);
809 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000810 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000811
Erik Andersene49d5ec2000-02-08 19:58:47 +0000812 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000813 if (buf[0] == '#')
814 continue;
815
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000816 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000817 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000819 if (end == NULL)
820 continue;
821 *end = '\0';
822 rname = start;
823
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000824 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000825 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000827 if (end == NULL)
828 continue;
829
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000830 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000831 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000832 rid = (uid_t) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000833 if (end == start)
834 continue;
835
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000836 /* gid in passwd */
837 start = end + 1;
838 rgid = (gid_t) strtol(start, &end, 10);
839
Eric Andersend23f9ba1999-10-20 19:18:15 +0000840 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000842 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843 fclose(file);
844 return (rid);
845 }
846 }
847 if (id != -1 && id == rid) {
848 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000849 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000850 fclose(file);
851 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000852 }
853 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000854 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000855 return (-1);
856}
857
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000858/* returns a uid given a username */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859uid_t my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000860{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000861 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000862}
863
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000864/* returns a gid given a group name */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000865gid_t my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000866{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000867 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000868}
869
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000870/* gets a username given a uid */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871void my_getpwuid(char *name, uid_t uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000872{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000873 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000874}
875
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000876/* gets a groupname given a gid */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877void my_getgrgid(char *group, gid_t gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000878{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000879 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000880}
881
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000882/* gets a gid given a user name */
883gid_t my_getpwnamegid(char *name)
884{
885 gid_t gid;
886 my_getid("/etc/passwd", name, -1, &gid);
887 return gid;
888}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000889
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000890#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */
Eric Andersenaa0765e1999-10-22 04:30:20 +0000891
Eric Andersen0460ff21999-10-25 23:32:44 +0000892
893#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
894
895
896#include <linux/kd.h>
897#include <sys/ioctl.h>
898
Erik Andersene49d5ec2000-02-08 19:58:47 +0000899int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000900{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000901 char arg;
902
903 arg = 0;
904 return (ioctl(fd, KDGKBTYPE, &arg) == 0
905 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000906}
907
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000909{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910 int fd;
911
912 /* try read-only */
913 fd = open(fnam, O_RDWR);
914
915 /* if failed, try read-only */
916 if (fd < 0 && errno == EACCES)
917 fd = open(fnam, O_RDONLY);
918
919 /* if failed, try write-only */
920 if (fd < 0 && errno == EACCES)
921 fd = open(fnam, O_WRONLY);
922
923 /* if failed, fail */
924 if (fd < 0)
925 return -1;
926
927 /* if not a console, fail */
928 if (!is_a_console(fd)) {
929 close(fd);
930 return -1;
931 }
932
933 /* success */
934 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000935}
936
937/*
938 * Get an fd for use with kbd/console ioctls.
939 * We try several things because opening /dev/console will fail
940 * if someone else used X (which does a chown on /dev/console).
941 *
942 * if tty_name is non-NULL, try this one instead.
943 */
944
Erik Andersene49d5ec2000-02-08 19:58:47 +0000945int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000946{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000948
Erik Andersene49d5ec2000-02-08 19:58:47 +0000949 if (tty_name) {
950 if (-1 == (fd = open_a_console(tty_name)))
951 return -1;
952 else
953 return fd;
954 }
955
956 fd = open_a_console("/dev/tty");
957 if (fd >= 0)
958 return fd;
959
960 fd = open_a_console("/dev/tty0");
961 if (fd >= 0)
962 return fd;
963
964 fd = open_a_console("/dev/console");
965 if (fd >= 0)
966 return fd;
967
968 for (fd = 0; fd < 3; fd++)
969 if (is_a_console(fd))
970 return fd;
971
972 fprintf(stderr,
973 "Couldnt get a file descriptor referring to the console\n");
974 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000975}
976
977
Erik Andersene49d5ec2000-02-08 19:58:47 +0000978#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000979
980
Erik Andersene49d5ec2000-02-08 19:58:47 +0000981#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000982
983/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000984char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000985{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000986 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000987
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988 while (*haystack) {
989 if (!strncasecmp(haystack, needle, len))
990 break;
991 haystack++;
992 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000993
Erik Andersene49d5ec2000-02-08 19:58:47 +0000994 if (!(*haystack))
995 haystack = NULL;
996
997 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000998}
999
Eric Andersenc1525e81999-10-29 00:07:31 +00001000/* This tries to find a needle in a haystack, but does so by
1001 * only trying to match literal strings (look 'ma, no regexps!)
1002 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001003 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +00001004 * -Erik Andersen
1005 */
1006extern int find_match(char *haystack, char *needle, int ignoreCase)
1007{
1008
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 if (ignoreCase == FALSE)
1010 haystack = strstr(haystack, needle);
1011 else
1012 haystack = stristr(haystack, needle);
1013 if (haystack == NULL)
1014 return FALSE;
1015 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001016}
1017
1018
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001019/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001020extern int replace_match(char *haystack, char *needle, char *newNeedle,
1021 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +00001022{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 int foundOne = 0;
1024 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +00001025
Erik Andersene49d5ec2000-02-08 19:58:47 +00001026 if (ignoreCase == FALSE)
1027 where = strstr(haystack, needle);
1028 else
1029 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001030
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 if (strcmp(needle, newNeedle) == 0)
1032 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001033
Erik Andersen0d068a22000-03-21 22:32:57 +00001034 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 while (where != NULL) {
1036 foundOne++;
1037 strcpy(oldhayStack, haystack);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 for (slider = haystack, slider1 = oldhayStack; slider != where;
1039 slider++, slider1++);
1040 *slider = 0;
1041 haystack = strcat(haystack, newNeedle);
1042 slider1 += strlen(needle);
1043 haystack = strcat(haystack, slider1);
1044 where = strstr(slider, needle);
1045 }
1046 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001047
Erik Andersene49d5ec2000-02-08 19:58:47 +00001048 if (foundOne > 0)
1049 return TRUE;
1050 else
1051 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001052}
1053
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001055
1056
Eric Andersenb186d981999-12-03 09:19:54 +00001057#if defined BB_FIND
1058/*
1059 * Routine to see if a text string is matched by a wildcard pattern.
1060 * Returns TRUE if the text is matched, or FALSE if it is not matched
1061 * or if the pattern is invalid.
1062 * * matches zero or more characters
1063 * ? matches a single character
1064 * [abc] matches 'a', 'b' or 'c'
1065 * \c quotes character c
1066 * Adapted from code written by Ingo Wilken, and
1067 * then taken from sash, Copyright (c) 1999 by David I. Bell
1068 * Permission is granted to use, distribute, or modify this source,
1069 * provided that this copyright notice remains intact.
1070 * Permission to distribute this code under the GPL has been granted.
1071 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001073{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001074 const char *retryPat;
1075 const char *retryText;
1076 int ch;
1077 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001078 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001079
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 retryPat = NULL;
1081 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001082
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001084 ch = *pattern++;
1085
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 switch (ch) {
1087 case '*':
1088 retryPat = pattern;
1089 retryText = text;
1090 break;
1091
1092 case '[':
1093 found = FALSE;
1094
1095 while ((ch = *pattern++) != ']') {
1096 if (ch == '\\')
1097 ch = *pattern++;
1098
1099 if (ch == '\0')
1100 return FALSE;
1101
1102 if (*text == ch)
1103 found = TRUE;
1104 }
Erik Andersen632bb572000-04-18 22:09:06 +00001105 len=strlen(text);
1106 if (found == FALSE && len!=0) {
1107 return FALSE;
1108 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001109 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001110 if (strlen(pattern)==0 && len==1) {
1111 return TRUE;
1112 }
1113 if (len!=0) {
1114 text++;
1115 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001116 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 }
1118
1119 /* fall into next case */
1120
1121 case '?':
1122 if (*text++ == '\0')
1123 return FALSE;
1124
1125 break;
1126
1127 case '\\':
1128 ch = *pattern++;
1129
1130 if (ch == '\0')
1131 return FALSE;
1132
1133 /* fall into next case */
1134
1135 default:
1136 if (*text == ch) {
1137 if (*text)
1138 text++;
1139 break;
1140 }
1141
1142 if (*text) {
1143 pattern = retryPat;
1144 text = ++retryText;
1145 break;
1146 }
1147
Eric Andersenb186d981999-12-03 09:19:54 +00001148 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001149 }
1150
Erik Andersene49d5ec2000-02-08 19:58:47 +00001151 if (pattern == NULL)
1152 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001153 }
1154
Erik Andersene49d5ec2000-02-08 19:58:47 +00001155 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001156}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157#endif /* BB_FIND */
Eric Andersenb186d981999-12-03 09:19:54 +00001158
1159
Eric Andersen29d2e361999-11-06 06:07:27 +00001160
1161
Erik Andersen7dc16072000-01-04 01:10:25 +00001162#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001163/*
1164 * Given a block device, find the mount table entry if that block device
1165 * is mounted.
1166 *
1167 * Given any other file (or directory), find the mount table entry for its
1168 * filesystem.
1169 */
1170extern struct mntent *findMountPoint(const char *name, const char *table)
1171{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 struct stat s;
1173 dev_t mountDevice;
1174 FILE *mountTable;
1175 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001176
Erik Andersene49d5ec2000-02-08 19:58:47 +00001177 if (stat(name, &s) != 0)
1178 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001179
Erik Andersene49d5ec2000-02-08 19:58:47 +00001180 if ((s.st_mode & S_IFMT) == S_IFBLK)
1181 mountDevice = s.st_rdev;
1182 else
1183 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001184
1185
Erik Andersene49d5ec2000-02-08 19:58:47 +00001186 if ((mountTable = setmntent(table, "r")) == 0)
1187 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001188
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 while ((mountEntry = getmntent(mountTable)) != 0) {
1190 if (strcmp(name, mountEntry->mnt_dir) == 0
1191 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1192 break;
1193 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1194 break;
1195 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1196 break;
1197 }
1198 endmntent(mountTable);
1199 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001200}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001201#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001202
1203
1204
Eric Andersen1792f8c1999-12-09 06:11:36 +00001205#if defined BB_DD || defined BB_TAIL
1206/*
1207 * Read a number with a possible multiplier.
1208 * Returns -1 if the number format is illegal.
1209 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001211{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001212 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001213
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214 if (!isDecimal(*cp))
1215 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001216
Erik Andersene49d5ec2000-02-08 19:58:47 +00001217 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001218
Erik Andersene49d5ec2000-02-08 19:58:47 +00001219 while (isDecimal(*cp))
1220 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001221
Erik Andersene49d5ec2000-02-08 19:58:47 +00001222 switch (*cp++) {
1223 case 'M':
1224 case 'm': /* `tail' uses it traditionally */
1225 value *= 1048576;
1226 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001227
Erik Andersene49d5ec2000-02-08 19:58:47 +00001228 case 'k':
1229 value *= 1024;
1230 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001231
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 case 'b':
1233 value *= 512;
1234 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001235
Erik Andersene49d5ec2000-02-08 19:58:47 +00001236 case 'w':
1237 value *= 2;
1238 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001239
Erik Andersene49d5ec2000-02-08 19:58:47 +00001240 case '\0':
1241 return value;
1242
1243 default:
1244 return -1;
1245 }
1246
1247 if (*cp)
1248 return -1;
1249
Eric Andersen1792f8c1999-12-09 06:11:36 +00001250 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001251}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001252#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001253
Eric Andersen6805d5d1999-12-09 22:39:55 +00001254
Erik Andersen31cf8e02000-02-07 20:31:19 +00001255#if defined BB_INIT || defined BB_SYSLOGD
1256/* try to open up the specified device */
1257extern int device_open(char *device, int mode)
1258{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001259 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001260
Erik Andersene49d5ec2000-02-08 19:58:47 +00001261 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001262
Erik Andersene49d5ec2000-02-08 19:58:47 +00001263 /* Retry up to 5 times */
1264 for (f = 0; f < 5; f++)
1265 if ((fd = open(device, m, 0600)) >= 0)
1266 break;
1267 if (fd < 0)
1268 return fd;
1269 /* Reset original flags. */
1270 if (m != mode)
1271 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001272 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001273}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001274#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001275
1276
Erik Andersen6273f652000-03-17 01:12:41 +00001277#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001278#ifdef BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001279#include <linux/devps.h>
Eric Andersen6805d5d1999-12-09 22:39:55 +00001280#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001281
Erik Andersen269bba22000-03-08 14:50:47 +00001282#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001283/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001284 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001285 * This finds the pid of the specified process,
1286 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001287 *
Erik Andersen825aead2000-04-07 06:00:07 +00001288 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001289 */
Erik Andersen825aead2000-04-07 06:00:07 +00001290extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001291{
Erik Andersen825aead2000-04-07 06:00:07 +00001292 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001293 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001294 pid_t num_pids;
1295 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001296 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001297
1298 /* open device */
1299 fd = open(device, O_RDONLY);
1300 if (fd < 0)
1301 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1302
1303 /* Find out how many processes there are */
1304 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1305 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1306
1307 /* Allocate some memory -- grab a few extras just in case
1308 * some new processes start up while we wait. The kernel will
1309 * just ignore any extras if we give it too many, and will trunc.
1310 * the list if we give it too few. */
1311 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1312 pid_array[0] = num_pids+10;
1313
1314 /* Now grab the pid list */
1315 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1316 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1317
1318 /* Now search for a match */
1319 for (i=1; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001320 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001321 struct pid_info info;
1322
1323 info.pid = pid_array[i];
1324 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1325 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1326
Erik Andersen0a64de92000-04-18 20:00:03 +00001327 /* Make sure we only match on the process name */
1328 p=info.command_line+1;
1329 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1330 (p)++;
1331 }
1332 if (isspace(*(p)))
1333 *p='\0';
1334
Erik Andersenb9167cb2000-04-18 22:41:30 +00001335 if ((strstr(info.command_line, pidName) != NULL)
1336 && (strlen(pidName) == strlen(info.command_line))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001337 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1338 if (pidList==NULL)
1339 fatalError("out of memory\n");
1340 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001341 }
1342 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001343 if (pidList)
1344 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001345
1346 /* Free memory */
1347 free( pid_array);
1348
1349 /* close device */
1350 if (close (fd) != 0)
1351 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1352
Erik Andersen825aead2000-04-07 06:00:07 +00001353 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001354}
Erik Andersen269bba22000-03-08 14:50:47 +00001355#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001356#if ! defined BB_FEATURE_USE_PROCFS
1357#error Sorry, I depend on the /proc filesystem right now.
1358#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001359
Erik Andersen246cc6d2000-03-07 07:41:42 +00001360/* findPidByName()
1361 *
1362 * This finds the pid of the specified process.
1363 * Currently, it's implemented by rummaging through
1364 * the proc filesystem.
1365 *
Erik Andersen825aead2000-04-07 06:00:07 +00001366 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001367 */
Erik Andersen825aead2000-04-07 06:00:07 +00001368extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001369{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001370 DIR *dir;
1371 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001372 pid_t* pidList=NULL;
1373 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001374
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001375 dir = opendir("/proc");
1376 if (!dir)
1377 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1378
1379 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001380 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001381 char filename[256];
1382 char buffer[256];
Erik Andersenb9167cb2000-04-18 22:41:30 +00001383 char* p;
John Beppuf95ca971999-12-09 22:10:18 +00001384
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001385 /* If it isn't a number, we don't want it */
1386 if (!isdigit(*next->d_name))
1387 continue;
1388
Erik Andersen825aead2000-04-07 06:00:07 +00001389 /* Now open the status file */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001390 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001391 status = fopen(filename, "r");
1392 if (!status) {
1393 continue;
1394 }
1395 fgets(buffer, 256, status);
1396 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001397
Erik Andersen0a64de92000-04-18 20:00:03 +00001398 /* Make sure we only match on the process name */
1399 p=buffer+5; /* Skip the name */
1400 while ((p)++) {
1401 if (*p==0 || *p=='\n') {
1402 *p='\0';
1403 break;
1404 }
1405 }
1406 p=buffer+6; /* Skip the "Name:\t" */
1407
Erik Andersenb9167cb2000-04-18 22:41:30 +00001408 if ((strstr(p, pidName) != NULL)
1409 && (strlen(pidName) == strlen(p))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001410 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1411 if (pidList==NULL)
1412 fatalError("out of memory\n");
1413 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001414 }
John Beppuf95ca971999-12-09 22:10:18 +00001415 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001416 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001417 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001418 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001419}
Erik Andersen269bba22000-03-08 14:50:47 +00001420#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen6273f652000-03-17 01:12:41 +00001421#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001422
Erik Andersen0d068a22000-03-21 22:32:57 +00001423/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001424extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001425{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001426 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001427
Erik Andersen0d068a22000-03-21 22:32:57 +00001428 if (cp == NULL)
Erik Andersen825aead2000-04-07 06:00:07 +00001429 fatalError("out of memory\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001430 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001431}
1432
Erik Andersen7dc16072000-01-04 01:10:25 +00001433#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1434extern int vdprintf(int d, const char *format, va_list ap)
1435{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001436 char buf[BUF_SIZE];
1437 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001438
Erik Andersene49d5ec2000-02-08 19:58:47 +00001439 len = vsprintf(buf, format, ap);
1440 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001441}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001442#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001443
Erik Andersen5cbdd712000-01-26 20:06:48 +00001444#if defined BB_FEATURE_MOUNT_LOOP
1445extern int del_loop(const char *device)
1446{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001447 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001448
Erik Andersene49d5ec2000-02-08 19:58:47 +00001449 if ((fd = open(device, O_RDONLY)) < 0) {
1450 perror(device);
1451 return (FALSE);
1452 }
1453 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1454 perror("ioctl: LOOP_CLR_FD");
1455 return (FALSE);
1456 }
1457 close(fd);
1458 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001459}
Erik Andersende7965c2000-01-26 23:49:21 +00001460
Erik Andersene49d5ec2000-02-08 19:58:47 +00001461extern int set_loop(const char *device, const char *file, int offset,
1462 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001463{
1464 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001465 int fd, ffd, mode;
1466
Erik Andersende7965c2000-01-26 23:49:21 +00001467 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001468 if ((ffd = open(file, mode)) < 0 && !*loopro
1469 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1470 perror(file);
1471 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001472 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001473 if ((fd = open(device, mode)) < 0) {
1474 close(ffd);
1475 perror(device);
1476 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001477 }
1478 *loopro = (mode == O_RDONLY);
1479
1480 memset(&loopinfo, 0, sizeof(loopinfo));
1481 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001482 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001483
1484 loopinfo.lo_offset = offset;
1485
1486 loopinfo.lo_encrypt_key_size = 0;
1487 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1488 perror("ioctl: LOOP_SET_FD");
1489 close(fd);
1490 close(ffd);
1491 return 1;
1492 }
1493 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1494 (void) ioctl(fd, LOOP_CLR_FD, 0);
1495 perror("ioctl: LOOP_SET_STATUS");
1496 close(fd);
1497 close(ffd);
1498 return 1;
1499 }
1500 close(fd);
1501 close(ffd);
1502 return 0;
1503}
1504
Erik Andersene49d5ec2000-02-08 19:58:47 +00001505extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001506{
1507 char dev[20];
1508 int i, fd;
1509 struct stat statbuf;
1510 struct loop_info loopinfo;
1511
Erik Andersene49d5ec2000-02-08 19:58:47 +00001512 for (i = 0; i <= 7; i++) {
1513 sprintf(dev, "/dev/loop%d", i);
1514 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1515 if ((fd = open(dev, O_RDONLY)) >= 0) {
1516 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1517 if (errno == ENXIO) { /* probably free */
1518 close(fd);
1519 return strdup(dev);
1520 }
1521 }
1522 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001523 }
Erik Andersende7965c2000-01-26 23:49:21 +00001524 }
Erik Andersende7965c2000-01-26 23:49:21 +00001525 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001526 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001527}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001528#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001529
Erik Andersen691f49d2000-05-08 17:56:25 +00001530#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001531extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001532{
Erik Andersen016ffe92000-03-22 00:58:54 +00001533 DIR *dir;
1534 struct dirent *entry;
1535 struct stat statBuf, rootStat;
1536 char fileName[BUFSIZ];
1537
Erik Andersenec5bd902000-03-22 07:12:05 +00001538 if (stat("/", &rootStat) != 0) {
1539 errorMsg("could not stat '/'\n");
1540 return( FALSE);
1541 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001542
Erik Andersenec5bd902000-03-22 07:12:05 +00001543 dir = opendir("/dev");
1544 if (!dir) {
1545 errorMsg("could not open '/dev'\n");
1546 return( FALSE);
1547 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001548
1549 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001550
Erik Andersen016ffe92000-03-22 00:58:54 +00001551 /* Must skip ".." since that is "/", and so we
1552 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001553 if (strcmp(entry->d_name, "..") == 0)
1554 continue;
1555
1556 sprintf( fileName, "/dev/%s", entry->d_name);
1557
1558 if (stat(fileName, &statBuf) != 0)
1559 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001560 /* Some char devices have the same dev_t as block
1561 * devices, so make sure this is a block device */
1562 if (! S_ISBLK(statBuf.st_mode))
1563 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001564 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001565 strcpy(name, fileName);
1566 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001567 }
1568 }
1569
Erik Andersenec5bd902000-03-22 07:12:05 +00001570 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001571}
1572#endif
1573
John Beppu5a728cf2000-04-17 04:22:09 +00001574const unsigned int CSTRING_BUFFER_LENGTH = 128;
1575/* recursive parser that returns cstrings of arbitrary length
1576 * from a FILE*
1577 */
1578static char *
1579cstring_alloc(FILE* f, int depth)
1580{
1581 char *cstring;
1582 char buffer[CSTRING_BUFFER_LENGTH];
1583 int target = CSTRING_BUFFER_LENGTH * depth;
1584 int i, len;
1585 int size;
1586
1587 /* fill buffer */
1588 i = 0;
1589 while ((buffer[i] = fgetc(f)) != EOF) {
1590 if (buffer[i++] == 0x0a) { break; }
1591 if (i == CSTRING_BUFFER_LENGTH) { break; }
1592 }
1593 len = i;
1594
1595 /* recurse or malloc? */
1596 if (len == CSTRING_BUFFER_LENGTH) {
1597 cstring = cstring_alloc(f, (depth + 1));
1598 } else {
1599 /* [special case] EOF */
1600 if ((depth | len) == 0) { return NULL; }
1601
1602 /* malloc */
1603 size = target + len + 1;
1604 cstring = malloc(size);
1605 if (!cstring) { return NULL; }
1606 cstring[size - 1] = 0;
1607 }
1608
1609 /* copy buffer */
1610 if (cstring) {
1611 memcpy(&cstring[target], buffer, len);
1612 }
1613 return cstring;
1614}
1615
1616/*
1617 * wrapper around recursive cstring_alloc
1618 * it's the caller's responsibility to free the cstring
1619 */
1620char *
1621cstring_lineFromFile(FILE *f)
1622{
1623 return cstring_alloc(f, 0);
1624}
Erik Andersen016ffe92000-03-22 00:58:54 +00001625
Eric Andersencc8ed391999-10-05 16:24:54 +00001626/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001627/*
1628Local Variables:
1629c-file-style: "linux"
1630c-basic-offset: 4
1631tab-width: 4
1632End:
1633*/