blob: a582f708c88e9a7891bb006cb3232612fd661980 [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 Andersene49d5ec2000-02-08 19:58:47 +000050#include <sys/param.h> /* for PATH_MAX */
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 Andersen298854f2000-03-23 01:09:18 +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 Andersen298854f2000-03-23 01:09:18 +0000132#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000133
Erik Andersen029011b2000-03-04 21:19:32 +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
201#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000202
Erik Andersenfac10d72000-02-07 05:29:42 +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 Andersenfac10d72000-02-07 05:29:42 +0000234#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000235
Erik Andersenfac10d72000-02-07 05:29:42 +0000236#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000237/*
238 * Copy one file to another, while possibly preserving its modes, times,
239 * and modes. Returns TRUE if successful, or FALSE on a failure with an
Erik Andersen029011b2000-03-04 21:19:32 +0000240 * error message output. (Failure is not indicated if the attributes cannot
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000241 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +0000242 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000243 */
244int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245copyFile(const char *srcName, const char *destName,
246 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000247{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 int rfd;
249 int wfd;
250 int rcc;
251 int status;
252 char buf[BUF_SIZE];
253 struct stat srcStatBuf;
254 struct stat dstStatBuf;
255 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000256
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 if (followLinks == TRUE)
258 status = stat(srcName, &srcStatBuf);
259 else
260 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000261
Erik Andersenfac10d72000-02-07 05:29:42 +0000262 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263 perror(srcName);
264 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000265 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266
267 if (followLinks == TRUE)
268 status = stat(destName, &dstStatBuf);
269 else
270 status = lstat(destName, &dstStatBuf);
271
272 if (status < 0) {
273 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)) {
292 char link_val[PATH_MAX + 1];
293 int link_size;
294
295 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
296 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
297 link_size = readlink(srcName, &link_val[0], PATH_MAX);
298 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) {
310 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
311 perror(destName);
312 return FALSE;
313 }
314 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000315#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 return TRUE;
317 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
318 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
319 if (mkfifo(destName, 0644) < 0) {
320 perror(destName);
321 return FALSE;
322 }
323 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
324 || S_ISSOCK(srcStatBuf.st_mode)) {
325 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
326 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
327 perror(destName);
328 return FALSE;
329 }
330 } else if (S_ISREG(srcStatBuf.st_mode)) {
331 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
332 rfd = open(srcName, O_RDONLY);
333 if (rfd < 0) {
334 perror(srcName);
335 return FALSE;
336 }
337
338 wfd =
339 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
340 srcStatBuf.st_mode);
341 if (wfd < 0) {
342 perror(destName);
343 close(rfd);
344 return FALSE;
345 }
346
347 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
348 if (fullWrite(wfd, buf, rcc) < 0)
349 goto error_exit;
350 }
351 if (rcc < 0) {
352 goto error_exit;
353 }
354
355 close(rfd);
356 if (close(wfd) < 0) {
357 return FALSE;
358 }
359 }
360
361 if (setModes == TRUE) {
362 /* This is fine, since symlinks never get here */
363 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
364 perror(destName);
365 exit FALSE;
366 }
367 if (chmod(destName, srcStatBuf.st_mode) < 0) {
368 perror(destName);
369 exit FALSE;
370 }
371 times.actime = srcStatBuf.st_atime;
372 times.modtime = srcStatBuf.st_mtime;
373 if (utime(destName, &times) < 0) {
374 perror(destName);
375 exit FALSE;
376 }
377 }
378
Erik Andersenfac10d72000-02-07 05:29:42 +0000379 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000380
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 error_exit:
382 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000383 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000385
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000387}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000389
390
391
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000392#if defined BB_TAR || defined BB_LS
393
394#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
395#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
396
397/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
398static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399 0, 0, S_ISUID,
400 0, 0, S_ISGID,
401 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000402};
403
404/* The 9 mode bits to test */
405static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 S_IRUSR, S_IWUSR, S_IXUSR,
407 S_IRGRP, S_IWGRP, S_IXGRP,
408 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000409};
410
411#define MODE1 "rwxrwxrwx"
412#define MODE0 "---------"
413#define SMODE1 "..s..s..t"
414#define SMODE0 "..S..S..T"
415
Eric Andersencc8ed391999-10-05 16:24:54 +0000416/*
417 * Return the standard ls-like mode string from a file mode.
418 * This is static and so is overwritten on each call.
419 */
Eric Andersenf811e071999-10-09 00:25:00 +0000420const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000421{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000423
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 int i;
425
426 buf[0] = TYPECHAR(mode);
427 for (i = 0; i < 9; i++) {
428 if (mode & SBIT[i])
429 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
430 else
431 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
432 }
433 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000434}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000436
437
Eric Andersen1792f8c1999-12-09 06:11:36 +0000438#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000439/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000440 * Return the standard ls-like time string from a time_t
441 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000442 */
Eric Andersenf811e071999-10-09 00:25:00 +0000443const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000444{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 time_t now;
446 char *str;
447 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000448
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000450
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000452
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 strcpy(buf, &str[4]);
454 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000455
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
457 strcpy(&buf[7], &str[20]);
458 buf[11] = '\0';
459 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000460
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000462}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000464
Erik Andersenfac10d72000-02-07 05:29:42 +0000465#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000466/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000467 * Write all of the supplied buffer out to a file.
468 * This does multiple writes as necessary.
469 * Returns the amount written, or -1 on an error.
470 */
Eric Andersenf811e071999-10-09 00:25:00 +0000471int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000472{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 int cc;
474 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000475
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000477
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 while (len > 0) {
479 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000480
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 if (cc < 0)
482 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000483
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 buf += cc;
485 total += cc;
486 len -= cc;
487 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000488
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000490}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000492
493
Eric Andersen1792f8c1999-12-09 06:11:36 +0000494#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000495/*
496 * Read all of the supplied buffer from a file.
497 * This does multiple reads as necessary.
498 * Returns the amount read, or -1 on an error.
499 * A short read is returned on an end of file.
500 */
Eric Andersenf811e071999-10-09 00:25:00 +0000501int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000502{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 int cc;
504 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000505
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000507
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 while (len > 0) {
509 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000510
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 if (cc < 0)
512 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000513
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 if (cc == 0)
515 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000516
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 buf += cc;
518 total += cc;
519 len -= cc;
520 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000521
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000523}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000525
526
Erik Andersenfac10d72000-02-07 05:29:42 +0000527#if defined (BB_CHMOD_CHOWN_CHGRP) \
528 || defined (BB_CP_MV) \
529 || defined (BB_FIND) \
530 || defined (BB_LS) \
531 || defined (BB_INSMOD)
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,
545 struct stat * statbuf),
546 int (*dirAction) (const char *fileName,
547 struct stat * statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000548{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 int status;
550 struct stat statbuf;
551 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000552
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 if (followLinks == TRUE)
554 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000555 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000557
Eric Andersen2b69c401999-10-05 22:58:32 +0000558 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559#ifdef BB_DEBUG_PRINT_SCAFFOLD
560 fprintf(stderr,
561 "status=%d followLinks=%d TRUE=%d\n",
562 status, followLinks, TRUE);
563#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000564 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000565 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000566 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567
568 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
569 if (fileAction == NULL)
570 return TRUE;
571 else
572 return fileAction(fileName, &statbuf);
573 }
574
575 if (recurse == FALSE) {
576 if (S_ISDIR(statbuf.st_mode)) {
577 if (dirAction != NULL)
578 return (dirAction(fileName, &statbuf));
579 else
580 return TRUE;
581 }
582 }
583
584 if (S_ISDIR(statbuf.st_mode)) {
585 DIR *dir;
586
587 dir = opendir(fileName);
588 if (!dir) {
589 perror(fileName);
590 return FALSE;
591 }
592 if (dirAction != NULL && depthFirst == FALSE) {
593 status = dirAction(fileName, &statbuf);
594 if (status == FALSE) {
595 perror(fileName);
596 return FALSE;
597 }
598 }
599 while ((next = readdir(dir)) != NULL) {
600 char nextFile[PATH_MAX + 1];
601
602 if ((strcmp(next->d_name, "..") == 0)
603 || (strcmp(next->d_name, ".") == 0)) {
604 continue;
605 }
606 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
607 fprintf(stderr, name_too_long, "ftw");
608 return FALSE;
609 }
610 sprintf(nextFile, "%s/%s", fileName, next->d_name);
611 status =
612 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
613 fileAction, dirAction);
614 if (status < 0) {
615 closedir(dir);
616 return FALSE;
617 }
618 }
619 status = closedir(dir);
620 if (status < 0) {
621 perror(fileName);
622 return FALSE;
623 }
624 if (dirAction != NULL && depthFirst == TRUE) {
625 status = dirAction(fileName, &statbuf);
626 if (status == FALSE) {
627 perror(fileName);
628 return FALSE;
629 }
630 }
631 } else {
632 if (fileAction == NULL)
633 return TRUE;
634 else
635 return fileAction(fileName, &statbuf);
636 }
637 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000638}
639
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000641
Eric Andersenf6be9441999-10-13 21:12:06 +0000642
643
Erik Andersena8991081999-12-29 03:34:00 +0000644#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000645/*
646 * Attempt to create the directories along the specified path, except for
647 * the final component. The mode is given for the final directory only,
648 * while all previous ones get default protections. Errors are not reported
649 * here, as failures to restore files can be reported later.
650 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000652{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 char *cp;
654 char *cpOld;
655 char buf[PATH_MAX + 1];
656 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000657
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 strcpy(buf, name);
659 for (cp = buf; *cp == '/'; cp++);
660 cp = strchr(cp, '/');
661 while (cp) {
662 cpOld = cp;
663 cp = strchr(cp + 1, '/');
664 *cpOld = '\0';
665 retVal = mkdir(buf, cp ? 0777 : mode);
666 if (retVal != 0 && errno != EEXIST) {
667 perror(buf);
668 return FALSE;
669 }
670 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000671 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000673}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000674#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000675
676
677
678#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000679/* [ugoa]{+|-|=}[rwxst] */
680
681
682
Erik Andersene49d5ec2000-02-08 19:58:47 +0000683extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000684{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 mode_t andMode =
686
687 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000688 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 mode_t mode = 0;
690 mode_t groups = 0;
691 char type;
692 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000693
694 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 for (;;) {
696 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000697 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000698 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000699 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000701 continue;
702 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000704 continue;
705 case 'o':
706 groups |= S_IRWXO;
707 continue;
708 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000710 continue;
711 case '+':
712 case '=':
713 case '-':
714 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 if (groups == 0) /* The default is "all" */
716 groups |=
717 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000718 break;
719 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 if (isdigit(c) && c >= '0' && c <= '7' &&
721 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000722 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000723 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000724 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000725 return (FALSE);
726 }
727 break;
728 }
729
Erik Andersene49d5ec2000-02-08 19:58:47 +0000730 while ((c = *s++) != '\0') {
731 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000732 case ',':
733 break;
734 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000735 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000736 continue;
737 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000739 continue;
740 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000742 continue;
743 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000745 continue;
746 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000747 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000748 continue;
749 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000750 *theMode &= andMode;
751 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000753 }
754 break;
755 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000756 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000757 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000758 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000759 /* fall through */
760 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000761 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000762 break;
763 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000764 andMode &= ~(mode & groups);
765 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000766 break;
767 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000768 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000769 *theMode &= andMode;
770 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000771 return (TRUE);
772}
Eric Andersence8f3b91999-10-20 07:03:36 +0000773
774
Erik Andersene49d5ec2000-02-08 19:58:47 +0000775#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000776
Eric Andersene674eb71999-10-19 20:52:57 +0000777
778
Eric Andersend23f9ba1999-10-20 19:18:15 +0000779
780
781
782
783#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
784
785/* Use this to avoid needing the glibc NSS stuff
786 * This uses storage buf to hold things.
787 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788uid_t my_getid(const char *filename, char *name, uid_t id)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000789{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000790 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000791 char *rname, *start, *end, buf[128];
792 uid_t rid;
793
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000795 if (file == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 perror(filename);
797 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000798 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000799
Erik Andersene49d5ec2000-02-08 19:58:47 +0000800 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000801 if (buf[0] == '#')
802 continue;
803
804 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000805 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000806 if (end == NULL)
807 continue;
808 *end = '\0';
809 rname = start;
810
811 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000812 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000813 if (end == NULL)
814 continue;
815
816 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000817 rid = (uid_t) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000818 if (end == start)
819 continue;
820
821 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000822 if (0 == strcmp(rname, name)) {
823 fclose(file);
824 return (rid);
825 }
826 }
827 if (id != -1 && id == rid) {
828 strncpy(name, rname, 8);
829 fclose(file);
830 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000831 }
832 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000833 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000834 return (-1);
835}
836
Erik Andersene49d5ec2000-02-08 19:58:47 +0000837uid_t my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000838{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839 return my_getid("/etc/passwd", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000840}
841
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842gid_t my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000843{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000844 return my_getid("/etc/group", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000845}
846
Erik Andersene49d5ec2000-02-08 19:58:47 +0000847void my_getpwuid(char *name, uid_t uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000848{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 my_getid("/etc/passwd", name, uid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000850}
851
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852void my_getgrgid(char *group, gid_t gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000853{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000854 my_getid("/etc/group", group, gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000855}
856
857
Erik Andersene49d5ec2000-02-08 19:58:47 +0000858#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000859
860
Eric Andersenaa0765e1999-10-22 04:30:20 +0000861
Eric Andersen0460ff21999-10-25 23:32:44 +0000862
863#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
864
865
866#include <linux/kd.h>
867#include <sys/ioctl.h>
868
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000870{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871 char arg;
872
873 arg = 0;
874 return (ioctl(fd, KDGKBTYPE, &arg) == 0
875 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000876}
877
Erik Andersene49d5ec2000-02-08 19:58:47 +0000878static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000879{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880 int fd;
881
882 /* try read-only */
883 fd = open(fnam, O_RDWR);
884
885 /* if failed, try read-only */
886 if (fd < 0 && errno == EACCES)
887 fd = open(fnam, O_RDONLY);
888
889 /* if failed, try write-only */
890 if (fd < 0 && errno == EACCES)
891 fd = open(fnam, O_WRONLY);
892
893 /* if failed, fail */
894 if (fd < 0)
895 return -1;
896
897 /* if not a console, fail */
898 if (!is_a_console(fd)) {
899 close(fd);
900 return -1;
901 }
902
903 /* success */
904 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000905}
906
907/*
908 * Get an fd for use with kbd/console ioctls.
909 * We try several things because opening /dev/console will fail
910 * if someone else used X (which does a chown on /dev/console).
911 *
912 * if tty_name is non-NULL, try this one instead.
913 */
914
Erik Andersene49d5ec2000-02-08 19:58:47 +0000915int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000916{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000917 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000918
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919 if (tty_name) {
920 if (-1 == (fd = open_a_console(tty_name)))
921 return -1;
922 else
923 return fd;
924 }
925
926 fd = open_a_console("/dev/tty");
927 if (fd >= 0)
928 return fd;
929
930 fd = open_a_console("/dev/tty0");
931 if (fd >= 0)
932 return fd;
933
934 fd = open_a_console("/dev/console");
935 if (fd >= 0)
936 return fd;
937
938 for (fd = 0; fd < 3; fd++)
939 if (is_a_console(fd))
940 return fd;
941
942 fprintf(stderr,
943 "Couldnt get a file descriptor referring to the console\n");
944 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000945}
946
947
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000949
950
Erik Andersene49d5ec2000-02-08 19:58:47 +0000951#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000952
953/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000955{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000956 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000957
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 while (*haystack) {
959 if (!strncasecmp(haystack, needle, len))
960 break;
961 haystack++;
962 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000963
Erik Andersene49d5ec2000-02-08 19:58:47 +0000964 if (!(*haystack))
965 haystack = NULL;
966
967 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000968}
969
Eric Andersenc1525e81999-10-29 00:07:31 +0000970/* This tries to find a needle in a haystack, but does so by
971 * only trying to match literal strings (look 'ma, no regexps!)
972 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000973 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000974 * -Erik Andersen
975 */
976extern int find_match(char *haystack, char *needle, int ignoreCase)
977{
978
Erik Andersene49d5ec2000-02-08 19:58:47 +0000979 if (ignoreCase == FALSE)
980 haystack = strstr(haystack, needle);
981 else
982 haystack = stristr(haystack, needle);
983 if (haystack == NULL)
984 return FALSE;
985 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000986}
987
988
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000989/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990extern int replace_match(char *haystack, char *needle, char *newNeedle,
991 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +0000992{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000993 int foundOne = 0;
994 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000995
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996 if (ignoreCase == FALSE)
997 where = strstr(haystack, needle);
998 else
999 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001000
Erik Andersene49d5ec2000-02-08 19:58:47 +00001001 if (strcmp(needle, newNeedle) == 0)
1002 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001003
Erik Andersen0d068a22000-03-21 22:32:57 +00001004 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005 while (where != NULL) {
1006 foundOne++;
1007 strcpy(oldhayStack, haystack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001008#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 if (strlen(newNeedle) > strlen(needle)) {
1010 haystack =
1011 (char *) realloc(haystack,
1012 (unsigned) (strlen(haystack) -
1013 strlen(needle) +
1014 strlen(newNeedle)));
1015 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001016#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001017 for (slider = haystack, slider1 = oldhayStack; slider != where;
1018 slider++, slider1++);
1019 *slider = 0;
1020 haystack = strcat(haystack, newNeedle);
1021 slider1 += strlen(needle);
1022 haystack = strcat(haystack, slider1);
1023 where = strstr(slider, needle);
1024 }
1025 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001026
Erik Andersene49d5ec2000-02-08 19:58:47 +00001027 if (foundOne > 0)
1028 return TRUE;
1029 else
1030 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001031}
1032
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001034
1035
Eric Andersenb186d981999-12-03 09:19:54 +00001036#if defined BB_FIND
1037/*
1038 * Routine to see if a text string is matched by a wildcard pattern.
1039 * Returns TRUE if the text is matched, or FALSE if it is not matched
1040 * or if the pattern is invalid.
1041 * * matches zero or more characters
1042 * ? matches a single character
1043 * [abc] matches 'a', 'b' or 'c'
1044 * \c quotes character c
1045 * Adapted from code written by Ingo Wilken, and
1046 * then taken from sash, Copyright (c) 1999 by David I. Bell
1047 * Permission is granted to use, distribute, or modify this source,
1048 * provided that this copyright notice remains intact.
1049 * Permission to distribute this code under the GPL has been granted.
1050 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001051extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001052{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001053 const char *retryPat;
1054 const char *retryText;
1055 int ch;
1056 int found;
Eric Andersenb186d981999-12-03 09:19:54 +00001057
Erik Andersene49d5ec2000-02-08 19:58:47 +00001058 retryPat = NULL;
1059 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001060
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001062 ch = *pattern++;
1063
Erik Andersene49d5ec2000-02-08 19:58:47 +00001064 switch (ch) {
1065 case '*':
1066 retryPat = pattern;
1067 retryText = text;
1068 break;
1069
1070 case '[':
1071 found = FALSE;
1072
1073 while ((ch = *pattern++) != ']') {
1074 if (ch == '\\')
1075 ch = *pattern++;
1076
1077 if (ch == '\0')
1078 return FALSE;
1079
1080 if (*text == ch)
1081 found = TRUE;
1082 }
1083
1084 //if (!found)
1085 if (found == TRUE) {
1086 pattern = retryPat;
1087 text = ++retryText;
1088 }
1089
1090 /* fall into next case */
1091
1092 case '?':
1093 if (*text++ == '\0')
1094 return FALSE;
1095
1096 break;
1097
1098 case '\\':
1099 ch = *pattern++;
1100
1101 if (ch == '\0')
1102 return FALSE;
1103
1104 /* fall into next case */
1105
1106 default:
1107 if (*text == ch) {
1108 if (*text)
1109 text++;
1110 break;
1111 }
1112
1113 if (*text) {
1114 pattern = retryPat;
1115 text = ++retryText;
1116 break;
1117 }
1118
Eric Andersenb186d981999-12-03 09:19:54 +00001119 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001120 }
1121
Erik Andersene49d5ec2000-02-08 19:58:47 +00001122 if (pattern == NULL)
1123 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001124 }
1125
Erik Andersene49d5ec2000-02-08 19:58:47 +00001126 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001127}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128#endif /* BB_FIND */
Eric Andersenb186d981999-12-03 09:19:54 +00001129
1130
Eric Andersen29d2e361999-11-06 06:07:27 +00001131
1132
Erik Andersen7dc16072000-01-04 01:10:25 +00001133#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001134/*
1135 * Given a block device, find the mount table entry if that block device
1136 * is mounted.
1137 *
1138 * Given any other file (or directory), find the mount table entry for its
1139 * filesystem.
1140 */
1141extern struct mntent *findMountPoint(const char *name, const char *table)
1142{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001143 struct stat s;
1144 dev_t mountDevice;
1145 FILE *mountTable;
1146 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001147
Erik Andersene49d5ec2000-02-08 19:58:47 +00001148 if (stat(name, &s) != 0)
1149 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001150
Erik Andersene49d5ec2000-02-08 19:58:47 +00001151 if ((s.st_mode & S_IFMT) == S_IFBLK)
1152 mountDevice = s.st_rdev;
1153 else
1154 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001155
1156
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157 if ((mountTable = setmntent(table, "r")) == 0)
1158 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001159
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160 while ((mountEntry = getmntent(mountTable)) != 0) {
1161 if (strcmp(name, mountEntry->mnt_dir) == 0
1162 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1163 break;
1164 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1165 break;
1166 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1167 break;
1168 }
1169 endmntent(mountTable);
1170 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001171}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001173
1174
1175
Eric Andersen1792f8c1999-12-09 06:11:36 +00001176#if defined BB_DD || defined BB_TAIL
1177/*
1178 * Read a number with a possible multiplier.
1179 * Returns -1 if the number format is illegal.
1180 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001182{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001184
Erik Andersene49d5ec2000-02-08 19:58:47 +00001185 if (!isDecimal(*cp))
1186 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001187
Erik Andersene49d5ec2000-02-08 19:58:47 +00001188 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001189
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 while (isDecimal(*cp))
1191 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001192
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 switch (*cp++) {
1194 case 'M':
1195 case 'm': /* `tail' uses it traditionally */
1196 value *= 1048576;
1197 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001198
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 case 'k':
1200 value *= 1024;
1201 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001202
Erik Andersene49d5ec2000-02-08 19:58:47 +00001203 case 'b':
1204 value *= 512;
1205 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001206
Erik Andersene49d5ec2000-02-08 19:58:47 +00001207 case 'w':
1208 value *= 2;
1209 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001210
Erik Andersene49d5ec2000-02-08 19:58:47 +00001211 case '\0':
1212 return value;
1213
1214 default:
1215 return -1;
1216 }
1217
1218 if (*cp)
1219 return -1;
1220
Eric Andersen1792f8c1999-12-09 06:11:36 +00001221 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001222}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001223#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001224
Eric Andersen6805d5d1999-12-09 22:39:55 +00001225
Erik Andersen31cf8e02000-02-07 20:31:19 +00001226#if defined BB_INIT || defined BB_SYSLOGD
1227/* try to open up the specified device */
1228extern int device_open(char *device, int mode)
1229{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001230 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001231
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001233
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234 /* Retry up to 5 times */
1235 for (f = 0; f < 5; f++)
1236 if ((fd = open(device, m, 0600)) >= 0)
1237 break;
1238 if (fd < 0)
1239 return fd;
1240 /* Reset original flags. */
1241 if (m != mode)
1242 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001243 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001244}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001245#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001246
1247
Erik Andersen6273f652000-03-17 01:12:41 +00001248#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001249#ifdef BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001250#include <linux/devps.h>
Eric Andersen6805d5d1999-12-09 22:39:55 +00001251#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001252
Erik Andersen269bba22000-03-08 14:50:47 +00001253#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001254/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001255 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001256 * This finds the pid of the specified process,
1257 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001258 *
1259 * [return]
1260 * 0 failure
Erik Andersen246cc6d2000-03-07 07:41:42 +00001261 * pid when the pid is found.
John Beppuf95ca971999-12-09 22:10:18 +00001262 */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001263extern pid_t findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001264{
Erik Andersen246cc6d2000-03-07 07:41:42 +00001265 int fd, i;
1266 char device[] = "/dev/ps";
1267 pid_t thePid = 0;
1268 pid_t num_pids;
1269 pid_t* pid_array = NULL;
1270
1271 /* open device */
1272 fd = open(device, O_RDONLY);
1273 if (fd < 0)
1274 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1275
1276 /* Find out how many processes there are */
1277 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1278 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1279
1280 /* Allocate some memory -- grab a few extras just in case
1281 * some new processes start up while we wait. The kernel will
1282 * just ignore any extras if we give it too many, and will trunc.
1283 * the list if we give it too few. */
1284 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1285 pid_array[0] = num_pids+10;
1286
1287 /* Now grab the pid list */
1288 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1289 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1290
1291 /* Now search for a match */
1292 for (i=1; i<pid_array[0] ; i++) {
1293 struct pid_info info;
1294
1295 info.pid = pid_array[i];
1296 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1297 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1298
1299 if ((strstr(info.command_line, pidName) != NULL)) {
1300 thePid = info.pid;
1301 break;
1302 }
1303 }
1304
1305 /* Free memory */
1306 free( pid_array);
1307
1308 /* close device */
1309 if (close (fd) != 0)
1310 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1311
1312 return thePid;
1313}
Erik Andersen269bba22000-03-08 14:50:47 +00001314#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001315#if ! defined BB_FEATURE_USE_PROCFS
1316#error Sorry, I depend on the /proc filesystem right now.
1317#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001318
Erik Andersen246cc6d2000-03-07 07:41:42 +00001319/* findPidByName()
1320 *
1321 * This finds the pid of the specified process.
1322 * Currently, it's implemented by rummaging through
1323 * the proc filesystem.
1324 *
1325 * [return]
1326 * 0 failure
1327 * pid when the pid is found.
1328 */
1329extern pid_t findPidByName( char* pidName)
1330{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001331 DIR *dir;
1332 struct dirent *next;
John Beppuf95ca971999-12-09 22:10:18 +00001333
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001334 dir = opendir("/proc");
1335 if (!dir)
1336 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1337
1338 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001339 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001340 char filename[256];
1341 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001342
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001343 /* If it isn't a number, we don't want it */
1344 if (!isdigit(*next->d_name))
1345 continue;
1346
1347 /* Now open the command line file */
1348 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001349 status = fopen(filename, "r");
1350 if (!status) {
1351 continue;
1352 }
1353 fgets(buffer, 256, status);
1354 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001355
Erik Andersen246cc6d2000-03-07 07:41:42 +00001356 if ((strstr(buffer, pidName) != NULL)) {
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001357 return strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001358 }
John Beppuf95ca971999-12-09 22:10:18 +00001359 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001360 return 0;
John Beppuf95ca971999-12-09 22:10:18 +00001361}
Erik Andersen269bba22000-03-08 14:50:47 +00001362#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen6273f652000-03-17 01:12:41 +00001363#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001364
Erik Andersen0d068a22000-03-21 22:32:57 +00001365/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001366extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001367{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001368 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001369
Erik Andersen0d068a22000-03-21 22:32:57 +00001370 if (cp == NULL)
1371 fatalError("out of memory");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001372 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001373}
1374
Erik Andersen7dc16072000-01-04 01:10:25 +00001375#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1376extern int vdprintf(int d, const char *format, va_list ap)
1377{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001378 char buf[BUF_SIZE];
1379 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001380
Erik Andersene49d5ec2000-02-08 19:58:47 +00001381 len = vsprintf(buf, format, ap);
1382 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001383}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001384#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001385
Erik Andersen5cbdd712000-01-26 20:06:48 +00001386#if defined BB_FEATURE_MOUNT_LOOP
1387extern int del_loop(const char *device)
1388{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001389 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001390
Erik Andersene49d5ec2000-02-08 19:58:47 +00001391 if ((fd = open(device, O_RDONLY)) < 0) {
1392 perror(device);
1393 return (FALSE);
1394 }
1395 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1396 perror("ioctl: LOOP_CLR_FD");
1397 return (FALSE);
1398 }
1399 close(fd);
1400 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001401}
Erik Andersende7965c2000-01-26 23:49:21 +00001402
Erik Andersene49d5ec2000-02-08 19:58:47 +00001403extern int set_loop(const char *device, const char *file, int offset,
1404 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001405{
1406 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001407 int fd, ffd, mode;
1408
Erik Andersende7965c2000-01-26 23:49:21 +00001409 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001410 if ((ffd = open(file, mode)) < 0 && !*loopro
1411 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1412 perror(file);
1413 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001414 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001415 if ((fd = open(device, mode)) < 0) {
1416 close(ffd);
1417 perror(device);
1418 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001419 }
1420 *loopro = (mode == O_RDONLY);
1421
1422 memset(&loopinfo, 0, sizeof(loopinfo));
1423 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001424 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001425
1426 loopinfo.lo_offset = offset;
1427
1428 loopinfo.lo_encrypt_key_size = 0;
1429 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1430 perror("ioctl: LOOP_SET_FD");
1431 close(fd);
1432 close(ffd);
1433 return 1;
1434 }
1435 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1436 (void) ioctl(fd, LOOP_CLR_FD, 0);
1437 perror("ioctl: LOOP_SET_STATUS");
1438 close(fd);
1439 close(ffd);
1440 return 1;
1441 }
1442 close(fd);
1443 close(ffd);
1444 return 0;
1445}
1446
Erik Andersene49d5ec2000-02-08 19:58:47 +00001447extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001448{
1449 char dev[20];
1450 int i, fd;
1451 struct stat statbuf;
1452 struct loop_info loopinfo;
1453
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454 for (i = 0; i <= 7; i++) {
1455 sprintf(dev, "/dev/loop%d", i);
1456 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1457 if ((fd = open(dev, O_RDONLY)) >= 0) {
1458 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1459 if (errno == ENXIO) { /* probably free */
1460 close(fd);
1461 return strdup(dev);
1462 }
1463 }
1464 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001465 }
Erik Andersende7965c2000-01-26 23:49:21 +00001466 }
Erik Andersende7965c2000-01-26 23:49:21 +00001467 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001468 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001469}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001470#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001471
Erik Andersenec5bd902000-03-22 07:12:05 +00001472#if defined BB_MOUNT || defined BB_DF
1473extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001474{
Erik Andersen016ffe92000-03-22 00:58:54 +00001475 DIR *dir;
1476 struct dirent *entry;
1477 struct stat statBuf, rootStat;
1478 char fileName[BUFSIZ];
1479
Erik Andersenec5bd902000-03-22 07:12:05 +00001480 if (stat("/", &rootStat) != 0) {
1481 errorMsg("could not stat '/'\n");
1482 return( FALSE);
1483 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001484
Erik Andersenec5bd902000-03-22 07:12:05 +00001485 dir = opendir("/dev");
1486 if (!dir) {
1487 errorMsg("could not open '/dev'\n");
1488 return( FALSE);
1489 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001490
1491 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001492
Erik Andersen016ffe92000-03-22 00:58:54 +00001493 /* Must skip ".." since that is "/", and so we
1494 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001495 if (strcmp(entry->d_name, "..") == 0)
1496 continue;
1497
1498 sprintf( fileName, "/dev/%s", entry->d_name);
1499
1500 if (stat(fileName, &statBuf) != 0)
1501 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001502 /* Some char devices have the same dev_t as block
1503 * devices, so make sure this is a block device */
1504 if (! S_ISBLK(statBuf.st_mode))
1505 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001506 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001507 strcpy(name, fileName);
1508 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001509 }
1510 }
1511
Erik Andersenec5bd902000-03-22 07:12:05 +00001512 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001513}
1514#endif
1515
1516
Erik Andersene132f4b2000-02-09 04:16:43 +00001517#if defined BB_MTAB
1518#define whine_if_fstab_is_missing() {}
1519#else
1520extern void whine_if_fstab_is_missing()
1521{
1522 struct stat statBuf;
1523
1524 if (stat("/etc/fstab", &statBuf) < 0)
1525 fprintf(stderr,
1526 "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1527}
1528#endif
1529
Eric Andersencc8ed391999-10-05 16:24:54 +00001530/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001531/*
1532Local Variables:
1533c-file-style: "linux"
1534c-basic-offset: 4
1535tab-width: 4
1536End:
1537*/