blob: c0dd98f74920644ea1207e1f61ab66fc17be3bd9 [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
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000036#define bb_need_memory_exhausted
Erik Andersenfac10d72000-02-07 05:29:42 +000037#endif
38#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
Erik Andersen23dea9b2000-05-13 00:28:25 +0000134#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000135
Erik Andersen029011b2000-03-04 21:19:32 +0000136#define HASH_SIZE 311 /* Should be prime */
137#define hash_inode(i) ((i) % HASH_SIZE)
138
139static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
140
141/*
142 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
143 * `ino_dev_hashtable', else return 0
144 *
145 * If NAME is a non-NULL pointer to a character pointer, and there is
146 * a match, then set *NAME to the value of the name slot in that
147 * bucket.
148 */
149int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
150{
151 ino_dev_hashtable_bucket_t *bucket;
152
153 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
154 while (bucket != NULL) {
155 if ((bucket->ino == statbuf->st_ino) &&
156 (bucket->dev == statbuf->st_dev))
157 {
158 if (name) *name = bucket->name;
159 return 1;
160 }
161 bucket = bucket->next;
162 }
163 return 0;
164}
165
166/* Add statbuf to statbuf hash table */
167void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
168{
169 int i;
170 size_t s;
171 ino_dev_hashtable_bucket_t *bucket;
172
173 i = hash_inode(statbuf->st_ino);
174 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000175 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000176 bucket->ino = statbuf->st_ino;
177 bucket->dev = statbuf->st_dev;
178 if (name)
179 strcpy(bucket->name, name);
180 else
181 bucket->name[0] = '\0';
182 bucket->next = ino_dev_hashtable[i];
183 ino_dev_hashtable[i] = bucket;
184}
185
186/* Clear statbuf hash table */
187void reset_ino_dev_hashtable(void)
188{
189 int i;
190 ino_dev_hashtable_bucket_t *bucket;
191
192 for (i = 0; i < HASH_SIZE; i++) {
193 while (ino_dev_hashtable[i] != NULL) {
194 bucket = ino_dev_hashtable[i]->next;
195 free(ino_dev_hashtable[i]);
196 ino_dev_hashtable[i] = bucket;
197 }
198 }
199}
200
Erik Andersen23dea9b2000-05-13 00:28:25 +0000201#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000202
Erik Andersen23dea9b2000-05-13 00:28:25 +0000203#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000204/*
205 * Return TRUE if a fileName is a directory.
206 * Nonexistant files return FALSE.
207 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000208int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000209{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000211 int didMalloc = 0;
212
213 if (statBuf == NULL) {
214 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
215 ++didMalloc;
216 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000217
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000219 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000221 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000222
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000223 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
224 status = FALSE;
225 }
226 else status = TRUE;
227
228 if (didMalloc) {
229 free(statBuf);
230 statBuf = NULL;
231 }
232 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000233}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000234#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000235
Erik Andersen23dea9b2000-05-13 00:28:25 +0000236#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000237/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000238 * Copy one file to another, while possibly preserving its modes, times, and
239 * modes. Returns TRUE if successful, or FALSE on a failure with an error
240 * message output. (Failure is not indicated if attributes cannot be set.)
241 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000242 */
243int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000245 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000246{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 int rfd;
248 int wfd;
249 int rcc;
250 int status;
251 char buf[BUF_SIZE];
252 struct stat srcStatBuf;
253 struct stat dstStatBuf;
254 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000255
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256 if (followLinks == TRUE)
257 status = stat(srcName, &srcStatBuf);
258 else
259 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000260
Erik Andersenfac10d72000-02-07 05:29:42 +0000261 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 perror(srcName);
263 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000264 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265
266 if (followLinks == TRUE)
267 status = stat(destName, &dstStatBuf);
268 else
269 status = lstat(destName, &dstStatBuf);
270
Erik Andersen59b9e872000-05-10 05:05:45 +0000271 if (status < 0 || forceFlag==TRUE) {
272 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 dstStatBuf.st_ino = -1;
274 dstStatBuf.st_dev = -1;
275 }
276
277 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
278 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
279 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
280 return FALSE;
281 }
282
283 if (S_ISDIR(srcStatBuf.st_mode)) {
284 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
285 /* Make sure the directory is writable */
286 status = mkdir(destName, 0777777 ^ umask(0));
287 if (status < 0 && errno != EEXIST) {
288 perror(destName);
289 return FALSE;
290 }
291 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000292 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 int link_size;
294
295 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000296 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
297 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 if (link_size < 0) {
299 perror(srcName);
300 return FALSE;
301 }
302 link_val[link_size] = '\0';
303 status = symlink(link_val, destName);
304 if (status < 0) {
305 perror(destName);
306 return FALSE;
307 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000308#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000310 /* Try to set owner, but fail silently like GNU cp */
311 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000313#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 return TRUE;
315 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
316 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
317 if (mkfifo(destName, 0644) < 0) {
318 perror(destName);
319 return FALSE;
320 }
321 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
322 || S_ISSOCK(srcStatBuf.st_mode)) {
323 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
324 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
325 perror(destName);
326 return FALSE;
327 }
328 } else if (S_ISREG(srcStatBuf.st_mode)) {
329 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
330 rfd = open(srcName, O_RDONLY);
331 if (rfd < 0) {
332 perror(srcName);
333 return FALSE;
334 }
335
336 wfd =
337 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
338 srcStatBuf.st_mode);
339 if (wfd < 0) {
340 perror(destName);
341 close(rfd);
342 return FALSE;
343 }
344
345 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
346 if (fullWrite(wfd, buf, rcc) < 0)
347 goto error_exit;
348 }
349 if (rcc < 0) {
350 goto error_exit;
351 }
352
353 close(rfd);
354 if (close(wfd) < 0) {
355 return FALSE;
356 }
357 }
358
359 if (setModes == TRUE) {
360 /* This is fine, since symlinks never get here */
361 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
362 perror(destName);
363 exit FALSE;
364 }
365 if (chmod(destName, srcStatBuf.st_mode) < 0) {
366 perror(destName);
367 exit FALSE;
368 }
369 times.actime = srcStatBuf.st_atime;
370 times.modtime = srcStatBuf.st_mtime;
371 if (utime(destName, &times) < 0) {
372 perror(destName);
373 exit FALSE;
374 }
375 }
376
Erik Andersenfac10d72000-02-07 05:29:42 +0000377 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000378
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 error_exit:
380 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000381 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000383
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000385}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000386#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
388
389
Erik Andersen23dea9b2000-05-13 00:28:25 +0000390#if defined BB_TAR || defined BB_LS
391
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000392#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
393#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
394
395/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
396static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397 0, 0, S_ISUID,
398 0, 0, S_ISGID,
399 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000400};
401
402/* The 9 mode bits to test */
403static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 S_IRUSR, S_IWUSR, S_IXUSR,
405 S_IRGRP, S_IWGRP, S_IXGRP,
406 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000407};
408
409#define MODE1 "rwxrwxrwx"
410#define MODE0 "---------"
411#define SMODE1 "..s..s..t"
412#define SMODE0 "..S..S..T"
413
Eric Andersencc8ed391999-10-05 16:24:54 +0000414/*
415 * Return the standard ls-like mode string from a file mode.
416 * This is static and so is overwritten on each call.
417 */
Eric Andersenf811e071999-10-09 00:25:00 +0000418const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000419{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000421
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 int i;
423
424 buf[0] = TYPECHAR(mode);
425 for (i = 0; i < 9; i++) {
426 if (mode & SBIT[i])
427 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
428 else
429 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
430 }
431 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000432}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000433#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000434
435
Erik Andersen23dea9b2000-05-13 00:28:25 +0000436#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000437/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000438 * Return the standard ls-like time string from a time_t
439 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000440 */
Eric Andersenf811e071999-10-09 00:25:00 +0000441const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000442{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 time_t now;
444 char *str;
445 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000446
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000448
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000450
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 strcpy(buf, &str[4]);
452 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000453
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
455 strcpy(&buf[7], &str[20]);
456 buf[11] = '\0';
457 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000458
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000460}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000461#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000462
Erik Andersen23dea9b2000-05-13 00:28:25 +0000463#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000464/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000465 * Write all of the supplied buffer out to a file.
466 * This does multiple writes as necessary.
467 * Returns the amount written, or -1 on an error.
468 */
Eric Andersenf811e071999-10-09 00:25:00 +0000469int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000470{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 int cc;
472 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000473
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000475
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 while (len > 0) {
477 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000478
Erik Andersene49d5ec2000-02-08 19:58:47 +0000479 if (cc < 0)
480 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000481
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 buf += cc;
483 total += cc;
484 len -= cc;
485 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000486
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000488}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000489#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000490
491
Erik Andersen23dea9b2000-05-13 00:28:25 +0000492#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000493/*
494 * Read all of the supplied buffer from a file.
495 * This does multiple reads as necessary.
496 * Returns the amount read, or -1 on an error.
497 * A short read is returned on an end of file.
498 */
Eric Andersenf811e071999-10-09 00:25:00 +0000499int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000500{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000501 int cc;
502 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000503
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000505
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 while (len > 0) {
507 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000508
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 if (cc < 0)
510 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000511
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 if (cc == 0)
513 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000514
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 buf += cc;
516 total += cc;
517 len -= cc;
518 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000519
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000521}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000522#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000523
524
Erik Andersen23dea9b2000-05-13 00:28:25 +0000525#if defined (BB_CHMOD_CHOWN_CHGRP) \
526 || defined (BB_CP_MV) \
527 || defined (BB_FIND) \
528 || defined (BB_INSMOD) \
529 || defined (BB_RM) \
530 || defined (BB_TAR)
531
Eric Andersencc8ed391999-10-05 16:24:54 +0000532/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000533 * Walk down all the directories under the specified
534 * location, and do something (something specified
535 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000536 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000537 * Unfortunatly, while nftw(3) could replace this and reduce
538 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000539 * and so isn't sufficiently portable to take over since glibc2.1
540 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000541 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000542int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 int recurse, int followLinks, int depthFirst,
544 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000545 struct stat * statbuf,
546 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000548 struct stat * statbuf,
549 void* userData),
550 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000551{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 int status;
553 struct stat statbuf;
554 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000555
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 if (followLinks == TRUE)
557 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000558 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000560
Eric Andersen2b69c401999-10-05 22:58:32 +0000561 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562#ifdef BB_DEBUG_PRINT_SCAFFOLD
563 fprintf(stderr,
564 "status=%d followLinks=%d TRUE=%d\n",
565 status, followLinks, TRUE);
566#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000567 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000568 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000569 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570
571 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
572 if (fileAction == NULL)
573 return TRUE;
574 else
Erik Andersen3364d782000-03-28 00:58:14 +0000575 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 }
577
578 if (recurse == FALSE) {
579 if (S_ISDIR(statbuf.st_mode)) {
580 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000581 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 else
583 return TRUE;
584 }
585 }
586
587 if (S_ISDIR(statbuf.st_mode)) {
588 DIR *dir;
589
590 dir = opendir(fileName);
591 if (!dir) {
592 perror(fileName);
593 return FALSE;
594 }
595 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000596 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 if (status == FALSE) {
598 perror(fileName);
599 return FALSE;
600 }
601 }
602 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000603 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604
605 if ((strcmp(next->d_name, "..") == 0)
606 || (strcmp(next->d_name, ".") == 0)) {
607 continue;
608 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000609 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000610 fprintf(stderr, name_too_long, "ftw");
611 return FALSE;
612 }
Erik Andersenc3660502000-04-18 21:18:57 +0000613 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614 sprintf(nextFile, "%s/%s", fileName, next->d_name);
615 status =
616 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000617 fileAction, dirAction, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 if (status < 0) {
619 closedir(dir);
620 return FALSE;
621 }
622 }
623 status = closedir(dir);
624 if (status < 0) {
625 perror(fileName);
626 return FALSE;
627 }
628 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000629 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000630 if (status == FALSE) {
631 perror(fileName);
632 return FALSE;
633 }
634 }
635 } else {
636 if (fileAction == NULL)
637 return TRUE;
638 else
Erik Andersen3364d782000-03-28 00:58:14 +0000639 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640 }
641 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000642}
643
Erik Andersen23dea9b2000-05-13 00:28:25 +0000644#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000645
Eric Andersenf6be9441999-10-13 21:12:06 +0000646
647
Erik Andersen23dea9b2000-05-13 00:28:25 +0000648#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000649/*
650 * Attempt to create the directories along the specified path, except for
651 * the final component. The mode is given for the final directory only,
652 * while all previous ones get default protections. Errors are not reported
653 * here, as failures to restore files can be reported later.
654 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000656{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 char *cp;
658 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000659 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000661
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 strcpy(buf, name);
663 for (cp = buf; *cp == '/'; cp++);
664 cp = strchr(cp, '/');
665 while (cp) {
666 cpOld = cp;
667 cp = strchr(cp + 1, '/');
668 *cpOld = '\0';
669 retVal = mkdir(buf, cp ? 0777 : mode);
670 if (retVal != 0 && errno != EEXIST) {
671 perror(buf);
672 return FALSE;
673 }
674 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000675 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000677}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000678#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000679
680
681
Erik Andersen23dea9b2000-05-13 00:28:25 +0000682#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000683/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000684
685
686
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000688{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 mode_t andMode =
690
691 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000692 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 mode_t mode = 0;
694 mode_t groups = 0;
695 char type;
696 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000697
698 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 for (;;) {
700 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000701 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000702 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000703 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000704 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000705 continue;
706 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000708 continue;
709 case 'o':
710 groups |= S_IRWXO;
711 continue;
712 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000714 continue;
715 case '+':
716 case '=':
717 case '-':
718 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 if (groups == 0) /* The default is "all" */
720 groups |=
721 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000722 break;
723 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000724 if (isdigit(c) && c >= '0' && c <= '7' &&
725 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000726 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000727 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000729 return (FALSE);
730 }
731 break;
732 }
733
Erik Andersene49d5ec2000-02-08 19:58:47 +0000734 while ((c = *s++) != '\0') {
735 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000736 case ',':
737 break;
738 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000739 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000740 continue;
741 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000743 continue;
744 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000746 continue;
747 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000748 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000749 continue;
750 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000751 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000752 continue;
753 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000754 *theMode &= andMode;
755 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000756 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000757 }
758 break;
759 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000760 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000761 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000762 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000763 /* fall through */
764 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000765 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000766 break;
767 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000768 andMode &= ~(mode & groups);
769 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000770 break;
771 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000772 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000773 *theMode &= andMode;
774 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000775 return (TRUE);
776}
Eric Andersence8f3b91999-10-20 07:03:36 +0000777
778
Erik Andersen23dea9b2000-05-13 00:28:25 +0000779#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000780
Eric Andersene674eb71999-10-19 20:52:57 +0000781
782
Erik Andersen23dea9b2000-05-13 00:28:25 +0000783
784
785#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR || defined BB_ID
786
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000787/* This parses entries in /etc/passwd and /etc/group. This is desirable
788 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
789 * increases target size and is often not needed or wanted for embedded
790 * systems.
791 *
792 * /etc/passwd entries look like this:
793 * root:x:0:0:root:/root:/bin/bash
794 * and /etc/group entries look like this:
795 * root:x:0:
796 *
797 * This uses buf as storage to hold things.
798 *
799 */
Erik Andersen5b46d792000-05-17 05:24:24 +0000800unsigned long my_getid(const char *filename, char *name, unsigned long id, unsigned long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000801{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000802 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000803 char *rname, *start, *end, buf[128];
Erik Andersen330fd2b2000-05-19 05:35:19 +0000804 unsigned long rid;
Erik Andersen5b46d792000-05-17 05:24:24 +0000805 unsigned long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000806
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000808 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000809 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000810 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000812 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000813
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000815 if (buf[0] == '#')
816 continue;
817
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000818 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000819 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000821 if (end == NULL)
822 continue;
823 *end = '\0';
824 rname = start;
825
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000826 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000827 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000828 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000829 if (end == NULL)
830 continue;
831
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000832 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000833 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000834 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000835 if (end == start)
836 continue;
837
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000838 /* gid in passwd */
839 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000840 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000841
Eric Andersend23f9ba1999-10-20 19:18:15 +0000842 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000844 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 fclose(file);
846 return (rid);
847 }
848 }
849 if (id != -1 && id == rid) {
850 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000851 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 fclose(file);
853 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000854 }
855 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000856 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000857 return (-1);
858}
859
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000860/* returns a uid given a username */
Erik Andersen5b46d792000-05-17 05:24:24 +0000861unsigned long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000862{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000863 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000864}
865
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000866/* returns a gid given a group name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000867unsigned long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000868{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000869 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000870}
871
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000872/* gets a username given a uid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000873void my_getpwuid(char *name, unsigned long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000874{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000875 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000876}
877
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000878/* gets a groupname given a gid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000879void my_getgrgid(char *group, unsigned long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000880{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000881 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000882}
883
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000884/* gets a gid given a user name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000885unsigned long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000886{
Erik Andersen5b46d792000-05-17 05:24:24 +0000887 unsigned long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000888 my_getid("/etc/passwd", name, -1, &gid);
889 return gid;
890}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000891
Erik Andersen23dea9b2000-05-13 00:28:25 +0000892#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */
893
894
895#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
Eric Andersen0460ff21999-10-25 23:32:44 +0000896
897
898#include <linux/kd.h>
899#include <sys/ioctl.h>
900
Erik Andersene49d5ec2000-02-08 19:58:47 +0000901int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000902{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 char arg;
904
905 arg = 0;
906 return (ioctl(fd, KDGKBTYPE, &arg) == 0
907 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000908}
909
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000911{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000912 int fd;
913
914 /* try read-only */
915 fd = open(fnam, O_RDWR);
916
917 /* if failed, try read-only */
918 if (fd < 0 && errno == EACCES)
919 fd = open(fnam, O_RDONLY);
920
921 /* if failed, try write-only */
922 if (fd < 0 && errno == EACCES)
923 fd = open(fnam, O_WRONLY);
924
925 /* if failed, fail */
926 if (fd < 0)
927 return -1;
928
929 /* if not a console, fail */
930 if (!is_a_console(fd)) {
931 close(fd);
932 return -1;
933 }
934
935 /* success */
936 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000937}
938
939/*
940 * Get an fd for use with kbd/console ioctls.
941 * We try several things because opening /dev/console will fail
942 * if someone else used X (which does a chown on /dev/console).
943 *
944 * if tty_name is non-NULL, try this one instead.
945 */
946
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000948{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000949 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000950
Erik Andersene49d5ec2000-02-08 19:58:47 +0000951 if (tty_name) {
952 if (-1 == (fd = open_a_console(tty_name)))
953 return -1;
954 else
955 return fd;
956 }
957
958 fd = open_a_console("/dev/tty");
959 if (fd >= 0)
960 return fd;
961
962 fd = open_a_console("/dev/tty0");
963 if (fd >= 0)
964 return fd;
965
966 fd = open_a_console("/dev/console");
967 if (fd >= 0)
968 return fd;
969
970 for (fd = 0; fd < 3; fd++)
971 if (is_a_console(fd))
972 return fd;
973
974 fprintf(stderr,
975 "Couldnt get a file descriptor referring to the console\n");
976 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000977}
978
979
Erik Andersen23dea9b2000-05-13 00:28:25 +0000980#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000981
982
Erik Andersen23dea9b2000-05-13 00:28:25 +0000983#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
984
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000985/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000986char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000987{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000989
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 while (*haystack) {
991 if (!strncasecmp(haystack, needle, len))
992 break;
993 haystack++;
994 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000995
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996 if (!(*haystack))
997 haystack = NULL;
998
999 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001000}
1001
Eric Andersenc1525e81999-10-29 00:07:31 +00001002/* This tries to find a needle in a haystack, but does so by
1003 * only trying to match literal strings (look 'ma, no regexps!)
1004 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001005 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +00001006 * -Erik Andersen
1007 */
1008extern int find_match(char *haystack, char *needle, int ignoreCase)
1009{
1010
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 if (ignoreCase == FALSE)
1012 haystack = strstr(haystack, needle);
1013 else
1014 haystack = stristr(haystack, needle);
1015 if (haystack == NULL)
1016 return FALSE;
1017 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001018}
1019
1020
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001021/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001022extern int replace_match(char *haystack, char *needle, char *newNeedle,
1023 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +00001024{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001025 int foundOne = 0;
1026 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +00001027
Erik Andersene49d5ec2000-02-08 19:58:47 +00001028 if (ignoreCase == FALSE)
1029 where = strstr(haystack, needle);
1030 else
1031 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001032
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033 if (strcmp(needle, newNeedle) == 0)
1034 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001035
Erik Andersen0d068a22000-03-21 22:32:57 +00001036 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 while (where != NULL) {
1038 foundOne++;
1039 strcpy(oldhayStack, haystack);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001040 for (slider = haystack, slider1 = oldhayStack; slider != where;
1041 slider++, slider1++);
1042 *slider = 0;
1043 haystack = strcat(haystack, newNeedle);
1044 slider1 += strlen(needle);
1045 haystack = strcat(haystack, slider1);
1046 where = strstr(slider, needle);
1047 }
1048 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001049
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 if (foundOne > 0)
1051 return TRUE;
1052 else
1053 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001054}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001055
1056#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001057
1058
Erik Andersen23dea9b2000-05-13 00:28:25 +00001059#if defined BB_FIND
Eric Andersenb186d981999-12-03 09:19:54 +00001060/*
1061 * Routine to see if a text string is matched by a wildcard pattern.
1062 * Returns TRUE if the text is matched, or FALSE if it is not matched
1063 * or if the pattern is invalid.
1064 * * matches zero or more characters
1065 * ? matches a single character
1066 * [abc] matches 'a', 'b' or 'c'
1067 * \c quotes character c
1068 * Adapted from code written by Ingo Wilken, and
1069 * then taken from sash, Copyright (c) 1999 by David I. Bell
1070 * Permission is granted to use, distribute, or modify this source,
1071 * provided that this copyright notice remains intact.
1072 * Permission to distribute this code under the GPL has been granted.
1073 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001074extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001075{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001076 const char *retryPat;
1077 const char *retryText;
1078 int ch;
1079 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001080 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001081
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082 retryPat = NULL;
1083 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001084
Erik Andersene49d5ec2000-02-08 19:58:47 +00001085 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001086 ch = *pattern++;
1087
Erik Andersene49d5ec2000-02-08 19:58:47 +00001088 switch (ch) {
1089 case '*':
1090 retryPat = pattern;
1091 retryText = text;
1092 break;
1093
1094 case '[':
1095 found = FALSE;
1096
1097 while ((ch = *pattern++) != ']') {
1098 if (ch == '\\')
1099 ch = *pattern++;
1100
1101 if (ch == '\0')
1102 return FALSE;
1103
1104 if (*text == ch)
1105 found = TRUE;
1106 }
Erik Andersen632bb572000-04-18 22:09:06 +00001107 len=strlen(text);
1108 if (found == FALSE && len!=0) {
1109 return FALSE;
1110 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001111 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001112 if (strlen(pattern)==0 && len==1) {
1113 return TRUE;
1114 }
1115 if (len!=0) {
1116 text++;
1117 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001118 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001119 }
1120
1121 /* fall into next case */
1122
1123 case '?':
1124 if (*text++ == '\0')
1125 return FALSE;
1126
1127 break;
1128
1129 case '\\':
1130 ch = *pattern++;
1131
1132 if (ch == '\0')
1133 return FALSE;
1134
1135 /* fall into next case */
1136
1137 default:
1138 if (*text == ch) {
1139 if (*text)
1140 text++;
1141 break;
1142 }
1143
1144 if (*text) {
1145 pattern = retryPat;
1146 text = ++retryText;
1147 break;
1148 }
1149
Eric Andersenb186d981999-12-03 09:19:54 +00001150 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001151 }
1152
Erik Andersene49d5ec2000-02-08 19:58:47 +00001153 if (pattern == NULL)
1154 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001155 }
1156
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001158}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001159#endif /* BB_FIND */
Eric Andersenb186d981999-12-03 09:19:54 +00001160
1161
Eric Andersen29d2e361999-11-06 06:07:27 +00001162
1163
Erik Andersen23dea9b2000-05-13 00:28:25 +00001164#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001165/*
1166 * Given a block device, find the mount table entry if that block device
1167 * is mounted.
1168 *
1169 * Given any other file (or directory), find the mount table entry for its
1170 * filesystem.
1171 */
1172extern struct mntent *findMountPoint(const char *name, const char *table)
1173{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001174 struct stat s;
1175 dev_t mountDevice;
1176 FILE *mountTable;
1177 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001178
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179 if (stat(name, &s) != 0)
1180 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001181
Erik Andersene49d5ec2000-02-08 19:58:47 +00001182 if ((s.st_mode & S_IFMT) == S_IFBLK)
1183 mountDevice = s.st_rdev;
1184 else
1185 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001186
1187
Erik Andersene49d5ec2000-02-08 19:58:47 +00001188 if ((mountTable = setmntent(table, "r")) == 0)
1189 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001190
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 while ((mountEntry = getmntent(mountTable)) != 0) {
1192 if (strcmp(name, mountEntry->mnt_dir) == 0
1193 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1194 break;
1195 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1196 break;
1197 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1198 break;
1199 }
1200 endmntent(mountTable);
1201 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001202}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001203#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001204
1205
1206
Erik Andersen23dea9b2000-05-13 00:28:25 +00001207#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001208/*
1209 * Read a number with a possible multiplier.
1210 * Returns -1 if the number format is illegal.
1211 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001212extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001213{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001215
Erik Andersene49d5ec2000-02-08 19:58:47 +00001216 if (!isDecimal(*cp))
1217 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001218
Erik Andersene49d5ec2000-02-08 19:58:47 +00001219 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001220
Erik Andersene49d5ec2000-02-08 19:58:47 +00001221 while (isDecimal(*cp))
1222 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001223
Erik Andersene49d5ec2000-02-08 19:58:47 +00001224 switch (*cp++) {
1225 case 'M':
1226 case 'm': /* `tail' uses it traditionally */
1227 value *= 1048576;
1228 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001229
Erik Andersene49d5ec2000-02-08 19:58:47 +00001230 case 'k':
1231 value *= 1024;
1232 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001233
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234 case 'b':
1235 value *= 512;
1236 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001237
Erik Andersene49d5ec2000-02-08 19:58:47 +00001238 case 'w':
1239 value *= 2;
1240 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001241
Erik Andersene49d5ec2000-02-08 19:58:47 +00001242 case '\0':
1243 return value;
1244
1245 default:
1246 return -1;
1247 }
1248
1249 if (*cp)
1250 return -1;
1251
Eric Andersen1792f8c1999-12-09 06:11:36 +00001252 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001253}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001254#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001255
Eric Andersen6805d5d1999-12-09 22:39:55 +00001256
Erik Andersen23dea9b2000-05-13 00:28:25 +00001257#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001258/* try to open up the specified device */
1259extern int device_open(char *device, int mode)
1260{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001261 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001262
Erik Andersene49d5ec2000-02-08 19:58:47 +00001263 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001264
Erik Andersene49d5ec2000-02-08 19:58:47 +00001265 /* Retry up to 5 times */
1266 for (f = 0; f < 5; f++)
1267 if ((fd = open(device, m, 0600)) >= 0)
1268 break;
1269 if (fd < 0)
1270 return fd;
1271 /* Reset original flags. */
1272 if (m != mode)
1273 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001274 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001275}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001276#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001277
1278
Erik Andersen23dea9b2000-05-13 00:28:25 +00001279#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001280#ifdef BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001281#include <linux/devps.h>
Erik Andersen23dea9b2000-05-13 00:28:25 +00001282#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001283
Erik Andersen23dea9b2000-05-13 00:28:25 +00001284#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001285/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001286 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001287 * This finds the pid of the specified process,
1288 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001289 *
Erik Andersen825aead2000-04-07 06:00:07 +00001290 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001291 */
Erik Andersen825aead2000-04-07 06:00:07 +00001292extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001293{
Erik Andersen825aead2000-04-07 06:00:07 +00001294 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001295 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001296 pid_t num_pids;
1297 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001298 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001299
1300 /* open device */
1301 fd = open(device, O_RDONLY);
1302 if (fd < 0)
1303 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1304
1305 /* Find out how many processes there are */
1306 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1307 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1308
1309 /* Allocate some memory -- grab a few extras just in case
1310 * some new processes start up while we wait. The kernel will
1311 * just ignore any extras if we give it too many, and will trunc.
1312 * the list if we give it too few. */
1313 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1314 pid_array[0] = num_pids+10;
1315
1316 /* Now grab the pid list */
1317 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1318 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1319
1320 /* Now search for a match */
1321 for (i=1; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001322 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001323 struct pid_info info;
1324
1325 info.pid = pid_array[i];
1326 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1327 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1328
Erik Andersen0a64de92000-04-18 20:00:03 +00001329 /* Make sure we only match on the process name */
1330 p=info.command_line+1;
1331 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1332 (p)++;
1333 }
1334 if (isspace(*(p)))
1335 *p='\0';
1336
Erik Andersenb9167cb2000-04-18 22:41:30 +00001337 if ((strstr(info.command_line, pidName) != NULL)
1338 && (strlen(pidName) == strlen(info.command_line))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001339 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1340 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001341 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001342 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001343 }
1344 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001345 if (pidList)
1346 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001347
1348 /* Free memory */
1349 free( pid_array);
1350
1351 /* close device */
1352 if (close (fd) != 0)
1353 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1354
Erik Andersen825aead2000-04-07 06:00:07 +00001355 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001356}
Erik Andersen269bba22000-03-08 14:50:47 +00001357#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001358#if ! defined BB_FEATURE_USE_PROCFS
1359#error Sorry, I depend on the /proc filesystem right now.
1360#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001361
Erik Andersen246cc6d2000-03-07 07:41:42 +00001362/* findPidByName()
1363 *
1364 * This finds the pid of the specified process.
1365 * Currently, it's implemented by rummaging through
1366 * the proc filesystem.
1367 *
Erik Andersen825aead2000-04-07 06:00:07 +00001368 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001369 */
Erik Andersen825aead2000-04-07 06:00:07 +00001370extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001371{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001372 DIR *dir;
1373 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001374 pid_t* pidList=NULL;
1375 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001376
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001377 dir = opendir("/proc");
1378 if (!dir)
1379 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1380
1381 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001382 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001383 char filename[256];
1384 char buffer[256];
Erik Andersenb9167cb2000-04-18 22:41:30 +00001385 char* p;
John Beppuf95ca971999-12-09 22:10:18 +00001386
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001387 /* If it isn't a number, we don't want it */
1388 if (!isdigit(*next->d_name))
1389 continue;
1390
Erik Andersen825aead2000-04-07 06:00:07 +00001391 /* Now open the status file */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001392 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001393 status = fopen(filename, "r");
1394 if (!status) {
1395 continue;
1396 }
1397 fgets(buffer, 256, status);
1398 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001399
Erik Andersen0a64de92000-04-18 20:00:03 +00001400 /* Make sure we only match on the process name */
1401 p=buffer+5; /* Skip the name */
1402 while ((p)++) {
1403 if (*p==0 || *p=='\n') {
1404 *p='\0';
1405 break;
1406 }
1407 }
1408 p=buffer+6; /* Skip the "Name:\t" */
1409
Erik Andersenb9167cb2000-04-18 22:41:30 +00001410 if ((strstr(p, pidName) != NULL)
1411 && (strlen(pidName) == strlen(p))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001412 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1413 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001414 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001415 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001416 }
John Beppuf95ca971999-12-09 22:10:18 +00001417 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001418 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001419 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001420 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001421}
Erik Andersen269bba22000-03-08 14:50:47 +00001422#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001423#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001424
Erik Andersen0d068a22000-03-21 22:32:57 +00001425/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001426extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001427{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001428 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001429
Erik Andersen0d068a22000-03-21 22:32:57 +00001430 if (cp == NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001431 fatalError(memory_exhausted, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001433}
1434
Erik Andersen23dea9b2000-05-13 00:28:25 +00001435#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001436extern int vdprintf(int d, const char *format, va_list ap)
1437{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001438 char buf[BUF_SIZE];
1439 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001440
Erik Andersene49d5ec2000-02-08 19:58:47 +00001441 len = vsprintf(buf, format, ap);
1442 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001443}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001444#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001445
Erik Andersen23dea9b2000-05-13 00:28:25 +00001446#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen5cbdd712000-01-26 20:06:48 +00001447extern int del_loop(const char *device)
1448{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001449 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001450
Erik Andersene49d5ec2000-02-08 19:58:47 +00001451 if ((fd = open(device, O_RDONLY)) < 0) {
1452 perror(device);
1453 return (FALSE);
1454 }
1455 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1456 perror("ioctl: LOOP_CLR_FD");
1457 return (FALSE);
1458 }
1459 close(fd);
1460 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001461}
Erik Andersende7965c2000-01-26 23:49:21 +00001462
Erik Andersene49d5ec2000-02-08 19:58:47 +00001463extern int set_loop(const char *device, const char *file, int offset,
1464 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001465{
1466 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001467 int fd, ffd, mode;
1468
Erik Andersende7965c2000-01-26 23:49:21 +00001469 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001470 if ((ffd = open(file, mode)) < 0 && !*loopro
1471 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1472 perror(file);
1473 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001474 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001475 if ((fd = open(device, mode)) < 0) {
1476 close(ffd);
1477 perror(device);
1478 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001479 }
1480 *loopro = (mode == O_RDONLY);
1481
1482 memset(&loopinfo, 0, sizeof(loopinfo));
1483 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001484 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001485
1486 loopinfo.lo_offset = offset;
1487
1488 loopinfo.lo_encrypt_key_size = 0;
1489 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1490 perror("ioctl: LOOP_SET_FD");
1491 close(fd);
1492 close(ffd);
1493 return 1;
1494 }
1495 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1496 (void) ioctl(fd, LOOP_CLR_FD, 0);
1497 perror("ioctl: LOOP_SET_STATUS");
1498 close(fd);
1499 close(ffd);
1500 return 1;
1501 }
1502 close(fd);
1503 close(ffd);
1504 return 0;
1505}
1506
Erik Andersene49d5ec2000-02-08 19:58:47 +00001507extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001508{
1509 char dev[20];
1510 int i, fd;
1511 struct stat statbuf;
1512 struct loop_info loopinfo;
1513
Erik Andersene49d5ec2000-02-08 19:58:47 +00001514 for (i = 0; i <= 7; i++) {
1515 sprintf(dev, "/dev/loop%d", i);
1516 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1517 if ((fd = open(dev, O_RDONLY)) >= 0) {
1518 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1519 if (errno == ENXIO) { /* probably free */
1520 close(fd);
1521 return strdup(dev);
1522 }
1523 }
1524 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001525 }
Erik Andersende7965c2000-01-26 23:49:21 +00001526 }
Erik Andersende7965c2000-01-26 23:49:21 +00001527 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001528 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001529}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001530#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001531
Erik Andersen23dea9b2000-05-13 00:28:25 +00001532#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001533extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001534{
Erik Andersen016ffe92000-03-22 00:58:54 +00001535 DIR *dir;
1536 struct dirent *entry;
1537 struct stat statBuf, rootStat;
1538 char fileName[BUFSIZ];
1539
Erik Andersenec5bd902000-03-22 07:12:05 +00001540 if (stat("/", &rootStat) != 0) {
1541 errorMsg("could not stat '/'\n");
1542 return( FALSE);
1543 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001544
Erik Andersenec5bd902000-03-22 07:12:05 +00001545 dir = opendir("/dev");
1546 if (!dir) {
1547 errorMsg("could not open '/dev'\n");
1548 return( FALSE);
1549 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001550
1551 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001552
Erik Andersen016ffe92000-03-22 00:58:54 +00001553 /* Must skip ".." since that is "/", and so we
1554 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001555 if (strcmp(entry->d_name, "..") == 0)
1556 continue;
1557
1558 sprintf( fileName, "/dev/%s", entry->d_name);
1559
1560 if (stat(fileName, &statBuf) != 0)
1561 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001562 /* Some char devices have the same dev_t as block
1563 * devices, so make sure this is a block device */
1564 if (! S_ISBLK(statBuf.st_mode))
1565 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001566 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001567 strcpy(name, fileName);
1568 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001569 }
1570 }
1571
Erik Andersenec5bd902000-03-22 07:12:05 +00001572 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001573}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001574#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001575
John Beppu5a728cf2000-04-17 04:22:09 +00001576const unsigned int CSTRING_BUFFER_LENGTH = 128;
1577/* recursive parser that returns cstrings of arbitrary length
1578 * from a FILE*
1579 */
1580static char *
1581cstring_alloc(FILE* f, int depth)
1582{
1583 char *cstring;
1584 char buffer[CSTRING_BUFFER_LENGTH];
1585 int target = CSTRING_BUFFER_LENGTH * depth;
Eric Andersen86ab8a32000-06-02 03:21:42 +00001586 int c, i, len, size;
John Beppu5a728cf2000-04-17 04:22:09 +00001587
1588 /* fill buffer */
1589 i = 0;
Eric Andersen86ab8a32000-06-02 03:21:42 +00001590 while ((c = fgetc(f)) != EOF) {
1591 buffer[i] = (char) c;
John Beppu5a728cf2000-04-17 04:22:09 +00001592 if (buffer[i++] == 0x0a) { break; }
1593 if (i == CSTRING_BUFFER_LENGTH) { break; }
1594 }
1595 len = i;
1596
1597 /* recurse or malloc? */
1598 if (len == CSTRING_BUFFER_LENGTH) {
1599 cstring = cstring_alloc(f, (depth + 1));
1600 } else {
1601 /* [special case] EOF */
1602 if ((depth | len) == 0) { return NULL; }
1603
1604 /* malloc */
1605 size = target + len + 1;
1606 cstring = malloc(size);
1607 if (!cstring) { return NULL; }
1608 cstring[size - 1] = 0;
1609 }
1610
1611 /* copy buffer */
1612 if (cstring) {
1613 memcpy(&cstring[target], buffer, len);
1614 }
1615 return cstring;
1616}
1617
1618/*
1619 * wrapper around recursive cstring_alloc
1620 * it's the caller's responsibility to free the cstring
1621 */
1622char *
1623cstring_lineFromFile(FILE *f)
1624{
1625 return cstring_alloc(f, 0);
1626}
Erik Andersen016ffe92000-03-22 00:58:54 +00001627
Eric Andersencc8ed391999-10-05 16:24:54 +00001628/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001629/*
1630Local Variables:
1631c-file-style: "linux"
1632c-basic-offset: 4
1633tab-width: 4
1634End:
1635*/