blob: 11ee7c68017bae7cfe99fe7fa054b4ef6a948fca [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
Eric Andersend23f9ba1999-10-20 19:18:15 +0000115/* Returns kernel version encoded as major*65536 + minor*256 + patch,
116 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000117 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000118 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000119extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000120{
Erik Andersend07ee462000-02-21 21:26:32 +0000121 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000123
Erik Andersend07ee462000-02-21 21:26:32 +0000124 if (uname(&name) == -1) {
125 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 return (0);
127 }
Erik Andersend07ee462000-02-21 21:26:32 +0000128 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000130}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000131
132
Erik Andersen029011b2000-03-04 21:19:32 +0000133#define HASH_SIZE 311 /* Should be prime */
134#define hash_inode(i) ((i) % HASH_SIZE)
135
136static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
137
138/*
139 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
140 * `ino_dev_hashtable', else return 0
141 *
142 * If NAME is a non-NULL pointer to a character pointer, and there is
143 * a match, then set *NAME to the value of the name slot in that
144 * bucket.
145 */
146int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
147{
148 ino_dev_hashtable_bucket_t *bucket;
149
150 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
151 while (bucket != NULL) {
152 if ((bucket->ino == statbuf->st_ino) &&
153 (bucket->dev == statbuf->st_dev))
154 {
155 if (name) *name = bucket->name;
156 return 1;
157 }
158 bucket = bucket->next;
159 }
160 return 0;
161}
162
163/* Add statbuf to statbuf hash table */
164void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
165{
166 int i;
167 size_t s;
168 ino_dev_hashtable_bucket_t *bucket;
169
170 i = hash_inode(statbuf->st_ino);
171 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000172 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000173 bucket->ino = statbuf->st_ino;
174 bucket->dev = statbuf->st_dev;
175 if (name)
176 strcpy(bucket->name, name);
177 else
178 bucket->name[0] = '\0';
179 bucket->next = ino_dev_hashtable[i];
180 ino_dev_hashtable[i] = bucket;
181}
182
183/* Clear statbuf hash table */
184void reset_ino_dev_hashtable(void)
185{
186 int i;
187 ino_dev_hashtable_bucket_t *bucket;
188
189 for (i = 0; i < HASH_SIZE; i++) {
190 while (ino_dev_hashtable[i] != NULL) {
191 bucket = ino_dev_hashtable[i]->next;
192 free(ino_dev_hashtable[i]);
193 ino_dev_hashtable[i] = bucket;
194 }
195 }
196}
197
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000198
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000199/*
200 * Return TRUE if a fileName is a directory.
201 * Nonexistant files return FALSE.
202 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000203int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000204{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000206 int didMalloc = 0;
207
208 if (statBuf == NULL) {
209 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
210 ++didMalloc;
211 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000212
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000214 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000216 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000217
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000218 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
219 status = FALSE;
220 }
221 else status = TRUE;
222
223 if (didMalloc) {
224 free(statBuf);
225 statBuf = NULL;
226 }
227 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000228}
229
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000230/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000231 * Copy one file to another, while possibly preserving its modes, times, and
232 * modes. Returns TRUE if successful, or FALSE on a failure with an error
233 * message output. (Failure is not indicated if attributes cannot be set.)
234 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000235 */
236int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000238 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000239{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 int rfd;
241 int wfd;
242 int rcc;
243 int status;
244 char buf[BUF_SIZE];
245 struct stat srcStatBuf;
246 struct stat dstStatBuf;
247 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000248
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 if (followLinks == TRUE)
250 status = stat(srcName, &srcStatBuf);
251 else
252 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000253
Erik Andersenfac10d72000-02-07 05:29:42 +0000254 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 perror(srcName);
256 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000257 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258
259 if (followLinks == TRUE)
260 status = stat(destName, &dstStatBuf);
261 else
262 status = lstat(destName, &dstStatBuf);
263
Erik Andersen59b9e872000-05-10 05:05:45 +0000264 if (status < 0 || forceFlag==TRUE) {
265 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 dstStatBuf.st_ino = -1;
267 dstStatBuf.st_dev = -1;
268 }
269
270 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
271 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
272 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
273 return FALSE;
274 }
275
276 if (S_ISDIR(srcStatBuf.st_mode)) {
277 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
278 /* Make sure the directory is writable */
279 status = mkdir(destName, 0777777 ^ umask(0));
280 if (status < 0 && errno != EEXIST) {
281 perror(destName);
282 return FALSE;
283 }
284 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000285 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 int link_size;
287
288 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000289 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
290 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 if (link_size < 0) {
292 perror(srcName);
293 return FALSE;
294 }
295 link_val[link_size] = '\0';
296 status = symlink(link_val, destName);
297 if (status < 0) {
298 perror(destName);
299 return FALSE;
300 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000301#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000303 /* Try to set owner, but fail silently like GNU cp */
304 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000306#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 return TRUE;
308 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
309 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
310 if (mkfifo(destName, 0644) < 0) {
311 perror(destName);
312 return FALSE;
313 }
314 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
315 || S_ISSOCK(srcStatBuf.st_mode)) {
316 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
317 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
318 perror(destName);
319 return FALSE;
320 }
321 } else if (S_ISREG(srcStatBuf.st_mode)) {
322 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
323 rfd = open(srcName, O_RDONLY);
324 if (rfd < 0) {
325 perror(srcName);
326 return FALSE;
327 }
328
329 wfd =
330 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
331 srcStatBuf.st_mode);
332 if (wfd < 0) {
333 perror(destName);
334 close(rfd);
335 return FALSE;
336 }
337
338 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
339 if (fullWrite(wfd, buf, rcc) < 0)
340 goto error_exit;
341 }
342 if (rcc < 0) {
343 goto error_exit;
344 }
345
346 close(rfd);
347 if (close(wfd) < 0) {
348 return FALSE;
349 }
350 }
351
352 if (setModes == TRUE) {
353 /* This is fine, since symlinks never get here */
354 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
355 perror(destName);
356 exit FALSE;
357 }
358 if (chmod(destName, srcStatBuf.st_mode) < 0) {
359 perror(destName);
360 exit FALSE;
361 }
362 times.actime = srcStatBuf.st_atime;
363 times.modtime = srcStatBuf.st_mtime;
364 if (utime(destName, &times) < 0) {
365 perror(destName);
366 exit FALSE;
367 }
368 }
369
Erik Andersenfac10d72000-02-07 05:29:42 +0000370 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000371
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 error_exit:
373 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000374 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000376
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000378}
Eric Andersencc8ed391999-10-05 16:24:54 +0000379
380
381
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000382#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
383#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
384
385/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
386static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 0, 0, S_ISUID,
388 0, 0, S_ISGID,
389 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000390};
391
392/* The 9 mode bits to test */
393static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 S_IRUSR, S_IWUSR, S_IXUSR,
395 S_IRGRP, S_IWGRP, S_IXGRP,
396 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000397};
398
399#define MODE1 "rwxrwxrwx"
400#define MODE0 "---------"
401#define SMODE1 "..s..s..t"
402#define SMODE0 "..S..S..T"
403
Eric Andersencc8ed391999-10-05 16:24:54 +0000404/*
405 * Return the standard ls-like mode string from a file mode.
406 * This is static and so is overwritten on each call.
407 */
Eric Andersenf811e071999-10-09 00:25:00 +0000408const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000409{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000411
Erik Andersene49d5ec2000-02-08 19:58:47 +0000412 int i;
413
414 buf[0] = TYPECHAR(mode);
415 for (i = 0; i < 9; i++) {
416 if (mode & SBIT[i])
417 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
418 else
419 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
420 }
421 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000422}
423
424
Eric Andersencc8ed391999-10-05 16:24:54 +0000425/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000426 * Return the standard ls-like time string from a time_t
427 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000428 */
Eric Andersenf811e071999-10-09 00:25:00 +0000429const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000430{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 time_t now;
432 char *str;
433 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000434
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000436
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000438
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439 strcpy(buf, &str[4]);
440 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000441
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
443 strcpy(&buf[7], &str[20]);
444 buf[11] = '\0';
445 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000446
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000448}
449
Eric Andersen17d49ef1999-10-06 20:25:32 +0000450/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000451 * Write all of the supplied buffer out to a file.
452 * This does multiple writes as necessary.
453 * Returns the amount written, or -1 on an error.
454 */
Eric Andersenf811e071999-10-09 00:25:00 +0000455int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000456{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 int cc;
458 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000459
Erik Andersene49d5ec2000-02-08 19:58:47 +0000460 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000461
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462 while (len > 0) {
463 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000464
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465 if (cc < 0)
466 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000467
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 buf += cc;
469 total += cc;
470 len -= cc;
471 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000472
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000474}
475
476
477/*
478 * Read all of the supplied buffer from a file.
479 * This does multiple reads as necessary.
480 * Returns the amount read, or -1 on an error.
481 * A short read is returned on an end of file.
482 */
Eric Andersenf811e071999-10-09 00:25:00 +0000483int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000484{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 int cc;
486 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000487
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000489
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490 while (len > 0) {
491 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000492
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 if (cc < 0)
494 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000495
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 if (cc == 0)
497 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000498
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499 buf += cc;
500 total += cc;
501 len -= cc;
502 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000503
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000505}
506
507
508/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000509 * Walk down all the directories under the specified
510 * location, and do something (something specified
511 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000512 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000513 * Unfortunatly, while nftw(3) could replace this and reduce
514 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000515 * and so isn't sufficiently portable to take over since glibc2.1
516 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000517 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000518int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 int recurse, int followLinks, int depthFirst,
520 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000521 struct stat * statbuf,
522 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000524 struct stat * statbuf,
525 void* userData),
526 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000527{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 int status;
529 struct stat statbuf;
530 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000531
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532 if (followLinks == TRUE)
533 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000534 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000536
Eric Andersen2b69c401999-10-05 22:58:32 +0000537 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000538#ifdef BB_DEBUG_PRINT_SCAFFOLD
539 fprintf(stderr,
540 "status=%d followLinks=%d TRUE=%d\n",
541 status, followLinks, TRUE);
542#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000543 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000544 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000545 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546
547 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
548 if (fileAction == NULL)
549 return TRUE;
550 else
Erik Andersen3364d782000-03-28 00:58:14 +0000551 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 }
553
554 if (recurse == FALSE) {
555 if (S_ISDIR(statbuf.st_mode)) {
556 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000557 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 else
559 return TRUE;
560 }
561 }
562
563 if (S_ISDIR(statbuf.st_mode)) {
564 DIR *dir;
565
566 dir = opendir(fileName);
567 if (!dir) {
568 perror(fileName);
569 return FALSE;
570 }
571 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000572 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 if (status == FALSE) {
574 perror(fileName);
575 return FALSE;
576 }
577 }
578 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000579 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580
581 if ((strcmp(next->d_name, "..") == 0)
582 || (strcmp(next->d_name, ".") == 0)) {
583 continue;
584 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000585 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 fprintf(stderr, name_too_long, "ftw");
587 return FALSE;
588 }
Erik Andersenc3660502000-04-18 21:18:57 +0000589 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 sprintf(nextFile, "%s/%s", fileName, next->d_name);
591 status =
592 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000593 fileAction, dirAction, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 if (status < 0) {
595 closedir(dir);
596 return FALSE;
597 }
598 }
599 status = closedir(dir);
600 if (status < 0) {
601 perror(fileName);
602 return FALSE;
603 }
604 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000605 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606 if (status == FALSE) {
607 perror(fileName);
608 return FALSE;
609 }
610 }
611 } else {
612 if (fileAction == NULL)
613 return TRUE;
614 else
Erik Andersen3364d782000-03-28 00:58:14 +0000615 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 }
617 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000618}
619
Eric Andersencc8ed391999-10-05 16:24:54 +0000620
Eric Andersenf6be9441999-10-13 21:12:06 +0000621
622
Eric Andersenf6be9441999-10-13 21:12:06 +0000623/*
624 * Attempt to create the directories along the specified path, except for
625 * the final component. The mode is given for the final directory only,
626 * while all previous ones get default protections. Errors are not reported
627 * here, as failures to restore files can be reported later.
628 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000630{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000631 char *cp;
632 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000633 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000634 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000635
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 strcpy(buf, name);
637 for (cp = buf; *cp == '/'; cp++);
638 cp = strchr(cp, '/');
639 while (cp) {
640 cpOld = cp;
641 cp = strchr(cp + 1, '/');
642 *cpOld = '\0';
643 retVal = mkdir(buf, cp ? 0777 : mode);
644 if (retVal != 0 && errno != EEXIST) {
645 perror(buf);
646 return FALSE;
647 }
648 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000649 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000650 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000651}
Eric Andersenf6be9441999-10-13 21:12:06 +0000652
653
654
Eric Andersence8f3b91999-10-20 07:03:36 +0000655/* [ugoa]{+|-|=}[rwxst] */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000656extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000657{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 mode_t andMode =
659
660 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000661 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 mode_t mode = 0;
663 mode_t groups = 0;
664 char type;
665 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000666
667 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000668 for (;;) {
669 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000670 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000671 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000672 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000674 continue;
675 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000677 continue;
678 case 'o':
679 groups |= S_IRWXO;
680 continue;
681 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000683 continue;
684 case '+':
685 case '=':
686 case '-':
687 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000688 if (groups == 0) /* The default is "all" */
689 groups |=
690 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000691 break;
692 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 if (isdigit(c) && c >= '0' && c <= '7' &&
694 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000695 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000696 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000698 return (FALSE);
699 }
700 break;
701 }
702
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 while ((c = *s++) != '\0') {
704 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000705 case ',':
706 break;
707 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000709 continue;
710 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000712 continue;
713 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000715 continue;
716 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000718 continue;
719 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000720 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000721 continue;
722 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000723 *theMode &= andMode;
724 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000725 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000726 }
727 break;
728 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000730 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000731 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000732 /* fall through */
733 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000734 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000735 break;
736 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000737 andMode &= ~(mode & groups);
738 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000739 break;
740 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000742 *theMode &= andMode;
743 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000744 return (TRUE);
745}
Eric Andersence8f3b91999-10-20 07:03:36 +0000746
747
Eric Andersenf6be9441999-10-13 21:12:06 +0000748
Eric Andersene674eb71999-10-19 20:52:57 +0000749
750
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000751/* This parses entries in /etc/passwd and /etc/group. This is desirable
752 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
753 * increases target size and is often not needed or wanted for embedded
754 * systems.
755 *
756 * /etc/passwd entries look like this:
757 * root:x:0:0:root:/root:/bin/bash
758 * and /etc/group entries look like this:
759 * root:x:0:
760 *
761 * This uses buf as storage to hold things.
762 *
763 */
764uid_t my_getid(const char *filename, char *name, uid_t id, gid_t *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000765{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000766 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000767 char *rname, *start, *end, buf[128];
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000768 id_t rid;
769 gid_t rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000770
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000772 if (file == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 perror(filename);
774 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000775 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000776
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000778 if (buf[0] == '#')
779 continue;
780
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000781 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000782 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000784 if (end == NULL)
785 continue;
786 *end = '\0';
787 rname = start;
788
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000789 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000790 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000791 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000792 if (end == NULL)
793 continue;
794
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000795 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000796 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000797 rid = (uid_t) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000798 if (end == start)
799 continue;
800
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000801 /* gid in passwd */
802 start = end + 1;
803 rgid = (gid_t) strtol(start, &end, 10);
804
Eric Andersend23f9ba1999-10-20 19:18:15 +0000805 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000806 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000807 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000808 fclose(file);
809 return (rid);
810 }
811 }
812 if (id != -1 && id == rid) {
813 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000814 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 fclose(file);
816 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000817 }
818 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000819 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000820 return (-1);
821}
822
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000823/* returns a uid given a username */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824uid_t my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000825{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000826 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000827}
828
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000829/* returns a gid given a group name */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830gid_t my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000831{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000832 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000833}
834
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000835/* gets a username given a uid */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836void my_getpwuid(char *name, uid_t uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000837{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000838 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000839}
840
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000841/* gets a groupname given a gid */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842void my_getgrgid(char *group, gid_t gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000843{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000844 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000845}
846
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000847/* gets a gid given a user name */
848gid_t my_getpwnamegid(char *name)
849{
850 gid_t gid;
851 my_getid("/etc/passwd", name, -1, &gid);
852 return gid;
853}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000854
Eric Andersen0460ff21999-10-25 23:32:44 +0000855
856
857#include <linux/kd.h>
858#include <sys/ioctl.h>
859
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000861{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 char arg;
863
864 arg = 0;
865 return (ioctl(fd, KDGKBTYPE, &arg) == 0
866 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000867}
868
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000870{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871 int fd;
872
873 /* try read-only */
874 fd = open(fnam, O_RDWR);
875
876 /* if failed, try read-only */
877 if (fd < 0 && errno == EACCES)
878 fd = open(fnam, O_RDONLY);
879
880 /* if failed, try write-only */
881 if (fd < 0 && errno == EACCES)
882 fd = open(fnam, O_WRONLY);
883
884 /* if failed, fail */
885 if (fd < 0)
886 return -1;
887
888 /* if not a console, fail */
889 if (!is_a_console(fd)) {
890 close(fd);
891 return -1;
892 }
893
894 /* success */
895 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000896}
897
898/*
899 * Get an fd for use with kbd/console ioctls.
900 * We try several things because opening /dev/console will fail
901 * if someone else used X (which does a chown on /dev/console).
902 *
903 * if tty_name is non-NULL, try this one instead.
904 */
905
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000907{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000909
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910 if (tty_name) {
911 if (-1 == (fd = open_a_console(tty_name)))
912 return -1;
913 else
914 return fd;
915 }
916
917 fd = open_a_console("/dev/tty");
918 if (fd >= 0)
919 return fd;
920
921 fd = open_a_console("/dev/tty0");
922 if (fd >= 0)
923 return fd;
924
925 fd = open_a_console("/dev/console");
926 if (fd >= 0)
927 return fd;
928
929 for (fd = 0; fd < 3; fd++)
930 if (is_a_console(fd))
931 return fd;
932
933 fprintf(stderr,
934 "Couldnt get a file descriptor referring to the console\n");
935 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000936}
937
938
Eric Andersen0460ff21999-10-25 23:32:44 +0000939
940
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000941#if !defined BB_REGEXP
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000942/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000943char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000944{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000945 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000946
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947 while (*haystack) {
948 if (!strncasecmp(haystack, needle, len))
949 break;
950 haystack++;
951 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000952
Erik Andersene49d5ec2000-02-08 19:58:47 +0000953 if (!(*haystack))
954 haystack = NULL;
955
956 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000957}
958
Eric Andersenc1525e81999-10-29 00:07:31 +0000959/* This tries to find a needle in a haystack, but does so by
960 * only trying to match literal strings (look 'ma, no regexps!)
961 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000962 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000963 * -Erik Andersen
964 */
965extern int find_match(char *haystack, char *needle, int ignoreCase)
966{
967
Erik Andersene49d5ec2000-02-08 19:58:47 +0000968 if (ignoreCase == FALSE)
969 haystack = strstr(haystack, needle);
970 else
971 haystack = stristr(haystack, needle);
972 if (haystack == NULL)
973 return FALSE;
974 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000975}
976
977
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000978/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000979extern int replace_match(char *haystack, char *needle, char *newNeedle,
980 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +0000981{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000982 int foundOne = 0;
983 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000984
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985 if (ignoreCase == FALSE)
986 where = strstr(haystack, needle);
987 else
988 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000989
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 if (strcmp(needle, newNeedle) == 0)
991 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000992
Erik Andersen0d068a22000-03-21 22:32:57 +0000993 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000994 while (where != NULL) {
995 foundOne++;
996 strcpy(oldhayStack, haystack);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000997 for (slider = haystack, slider1 = oldhayStack; slider != where;
998 slider++, slider1++);
999 *slider = 0;
1000 haystack = strcat(haystack, newNeedle);
1001 slider1 += strlen(needle);
1002 haystack = strcat(haystack, slider1);
1003 where = strstr(slider, needle);
1004 }
1005 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001006
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007 if (foundOne > 0)
1008 return TRUE;
1009 else
1010 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001011}
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001012#endif /* ! BB_REGEXP */
Eric Andersen29d2e361999-11-06 06:07:27 +00001013
1014
Eric Andersenb186d981999-12-03 09:19:54 +00001015/*
1016 * Routine to see if a text string is matched by a wildcard pattern.
1017 * Returns TRUE if the text is matched, or FALSE if it is not matched
1018 * or if the pattern is invalid.
1019 * * matches zero or more characters
1020 * ? matches a single character
1021 * [abc] matches 'a', 'b' or 'c'
1022 * \c quotes character c
1023 * Adapted from code written by Ingo Wilken, and
1024 * then taken from sash, Copyright (c) 1999 by David I. Bell
1025 * Permission is granted to use, distribute, or modify this source,
1026 * provided that this copyright notice remains intact.
1027 * Permission to distribute this code under the GPL has been granted.
1028 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001030{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 const char *retryPat;
1032 const char *retryText;
1033 int ch;
1034 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001035 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001036
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 retryPat = NULL;
1038 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001039
Erik Andersene49d5ec2000-02-08 19:58:47 +00001040 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001041 ch = *pattern++;
1042
Erik Andersene49d5ec2000-02-08 19:58:47 +00001043 switch (ch) {
1044 case '*':
1045 retryPat = pattern;
1046 retryText = text;
1047 break;
1048
1049 case '[':
1050 found = FALSE;
1051
1052 while ((ch = *pattern++) != ']') {
1053 if (ch == '\\')
1054 ch = *pattern++;
1055
1056 if (ch == '\0')
1057 return FALSE;
1058
1059 if (*text == ch)
1060 found = TRUE;
1061 }
Erik Andersen632bb572000-04-18 22:09:06 +00001062 len=strlen(text);
1063 if (found == FALSE && len!=0) {
1064 return FALSE;
1065 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001066 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001067 if (strlen(pattern)==0 && len==1) {
1068 return TRUE;
1069 }
1070 if (len!=0) {
1071 text++;
1072 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001073 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001074 }
1075
1076 /* fall into next case */
1077
1078 case '?':
1079 if (*text++ == '\0')
1080 return FALSE;
1081
1082 break;
1083
1084 case '\\':
1085 ch = *pattern++;
1086
1087 if (ch == '\0')
1088 return FALSE;
1089
1090 /* fall into next case */
1091
1092 default:
1093 if (*text == ch) {
1094 if (*text)
1095 text++;
1096 break;
1097 }
1098
1099 if (*text) {
1100 pattern = retryPat;
1101 text = ++retryText;
1102 break;
1103 }
1104
Eric Andersenb186d981999-12-03 09:19:54 +00001105 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001106 }
1107
Erik Andersene49d5ec2000-02-08 19:58:47 +00001108 if (pattern == NULL)
1109 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001110 }
1111
Erik Andersene49d5ec2000-02-08 19:58:47 +00001112 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001113}
Eric Andersenb186d981999-12-03 09:19:54 +00001114
1115
Eric Andersen29d2e361999-11-06 06:07:27 +00001116
1117
Eric Andersen29d2e361999-11-06 06:07:27 +00001118/*
1119 * Given a block device, find the mount table entry if that block device
1120 * is mounted.
1121 *
1122 * Given any other file (or directory), find the mount table entry for its
1123 * filesystem.
1124 */
1125extern struct mntent *findMountPoint(const char *name, const char *table)
1126{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 struct stat s;
1128 dev_t mountDevice;
1129 FILE *mountTable;
1130 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001131
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 if (stat(name, &s) != 0)
1133 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001134
Erik Andersene49d5ec2000-02-08 19:58:47 +00001135 if ((s.st_mode & S_IFMT) == S_IFBLK)
1136 mountDevice = s.st_rdev;
1137 else
1138 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001139
1140
Erik Andersene49d5ec2000-02-08 19:58:47 +00001141 if ((mountTable = setmntent(table, "r")) == 0)
1142 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001143
Erik Andersene49d5ec2000-02-08 19:58:47 +00001144 while ((mountEntry = getmntent(mountTable)) != 0) {
1145 if (strcmp(name, mountEntry->mnt_dir) == 0
1146 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1147 break;
1148 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1149 break;
1150 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1151 break;
1152 }
1153 endmntent(mountTable);
1154 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001155}
Eric Andersen29d2e361999-11-06 06:07:27 +00001156
1157
1158
Eric Andersen1792f8c1999-12-09 06:11:36 +00001159/*
1160 * Read a number with a possible multiplier.
1161 * Returns -1 if the number format is illegal.
1162 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001164{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001165 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001166
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 if (!isDecimal(*cp))
1168 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001169
Erik Andersene49d5ec2000-02-08 19:58:47 +00001170 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001171
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 while (isDecimal(*cp))
1173 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001174
Erik Andersene49d5ec2000-02-08 19:58:47 +00001175 switch (*cp++) {
1176 case 'M':
1177 case 'm': /* `tail' uses it traditionally */
1178 value *= 1048576;
1179 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001180
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 case 'k':
1182 value *= 1024;
1183 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001184
Erik Andersene49d5ec2000-02-08 19:58:47 +00001185 case 'b':
1186 value *= 512;
1187 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001188
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 case 'w':
1190 value *= 2;
1191 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001192
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 case '\0':
1194 return value;
1195
1196 default:
1197 return -1;
1198 }
1199
1200 if (*cp)
1201 return -1;
1202
Eric Andersen1792f8c1999-12-09 06:11:36 +00001203 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001204}
Eric Andersen1792f8c1999-12-09 06:11:36 +00001205
Eric Andersen6805d5d1999-12-09 22:39:55 +00001206
Erik Andersen31cf8e02000-02-07 20:31:19 +00001207/* try to open up the specified device */
1208extern int device_open(char *device, int mode)
1209{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001211
Erik Andersene49d5ec2000-02-08 19:58:47 +00001212 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001213
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214 /* Retry up to 5 times */
1215 for (f = 0; f < 5; f++)
1216 if ((fd = open(device, m, 0600)) >= 0)
1217 break;
1218 if (fd < 0)
1219 return fd;
1220 /* Reset original flags. */
1221 if (m != mode)
1222 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001223 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001224}
Erik Andersen31cf8e02000-02-07 20:31:19 +00001225
1226
Erik Andersen269bba22000-03-08 14:50:47 +00001227#ifdef BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001228#include <linux/devps.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +00001229
Erik Andersen246cc6d2000-03-07 07:41:42 +00001230/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001231 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001232 * This finds the pid of the specified process,
1233 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001234 *
Erik Andersen825aead2000-04-07 06:00:07 +00001235 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001236 */
Erik Andersen825aead2000-04-07 06:00:07 +00001237extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001238{
Erik Andersen825aead2000-04-07 06:00:07 +00001239 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001240 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001241 pid_t num_pids;
1242 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001243 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001244
1245 /* open device */
1246 fd = open(device, O_RDONLY);
1247 if (fd < 0)
1248 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1249
1250 /* Find out how many processes there are */
1251 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1252 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1253
1254 /* Allocate some memory -- grab a few extras just in case
1255 * some new processes start up while we wait. The kernel will
1256 * just ignore any extras if we give it too many, and will trunc.
1257 * the list if we give it too few. */
1258 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1259 pid_array[0] = num_pids+10;
1260
1261 /* Now grab the pid list */
1262 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1263 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1264
1265 /* Now search for a match */
1266 for (i=1; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001267 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001268 struct pid_info info;
1269
1270 info.pid = pid_array[i];
1271 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1272 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1273
Erik Andersen0a64de92000-04-18 20:00:03 +00001274 /* Make sure we only match on the process name */
1275 p=info.command_line+1;
1276 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1277 (p)++;
1278 }
1279 if (isspace(*(p)))
1280 *p='\0';
1281
Erik Andersenb9167cb2000-04-18 22:41:30 +00001282 if ((strstr(info.command_line, pidName) != NULL)
1283 && (strlen(pidName) == strlen(info.command_line))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001284 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1285 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001286 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001287 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001288 }
1289 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001290 if (pidList)
1291 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001292
1293 /* Free memory */
1294 free( pid_array);
1295
1296 /* close device */
1297 if (close (fd) != 0)
1298 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1299
Erik Andersen825aead2000-04-07 06:00:07 +00001300 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001301}
Erik Andersen269bba22000-03-08 14:50:47 +00001302#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001303#if ! defined BB_FEATURE_USE_PROCFS
1304#error Sorry, I depend on the /proc filesystem right now.
1305#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001306
Erik Andersen246cc6d2000-03-07 07:41:42 +00001307/* findPidByName()
1308 *
1309 * This finds the pid of the specified process.
1310 * Currently, it's implemented by rummaging through
1311 * the proc filesystem.
1312 *
Erik Andersen825aead2000-04-07 06:00:07 +00001313 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001314 */
Erik Andersen825aead2000-04-07 06:00:07 +00001315extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001316{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001317 DIR *dir;
1318 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001319 pid_t* pidList=NULL;
1320 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001321
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001322 dir = opendir("/proc");
1323 if (!dir)
1324 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1325
1326 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001327 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001328 char filename[256];
1329 char buffer[256];
Erik Andersenb9167cb2000-04-18 22:41:30 +00001330 char* p;
John Beppuf95ca971999-12-09 22:10:18 +00001331
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001332 /* If it isn't a number, we don't want it */
1333 if (!isdigit(*next->d_name))
1334 continue;
1335
Erik Andersen825aead2000-04-07 06:00:07 +00001336 /* Now open the status file */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001337 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001338 status = fopen(filename, "r");
1339 if (!status) {
1340 continue;
1341 }
1342 fgets(buffer, 256, status);
1343 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001344
Erik Andersen0a64de92000-04-18 20:00:03 +00001345 /* Make sure we only match on the process name */
1346 p=buffer+5; /* Skip the name */
1347 while ((p)++) {
1348 if (*p==0 || *p=='\n') {
1349 *p='\0';
1350 break;
1351 }
1352 }
1353 p=buffer+6; /* Skip the "Name:\t" */
1354
Erik Andersenb9167cb2000-04-18 22:41:30 +00001355 if ((strstr(p, pidName) != NULL)
1356 && (strlen(pidName) == strlen(p))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001357 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1358 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001359 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001360 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001361 }
John Beppuf95ca971999-12-09 22:10:18 +00001362 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001363 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001364 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001365 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001366}
Erik Andersen269bba22000-03-08 14:50:47 +00001367#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001368
Erik Andersen0d068a22000-03-21 22:32:57 +00001369/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001370extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001371{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001372 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001373
Erik Andersen0d068a22000-03-21 22:32:57 +00001374 if (cp == NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001375 fatalError(memory_exhausted, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001376 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001377}
1378
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001379#if (__GLIBC__ < 2)
Erik Andersen7dc16072000-01-04 01:10:25 +00001380extern int vdprintf(int d, const char *format, va_list ap)
1381{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001382 char buf[BUF_SIZE];
1383 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001384
Erik Andersene49d5ec2000-02-08 19:58:47 +00001385 len = vsprintf(buf, format, ap);
1386 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001387}
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001388#endif
Erik Andersen7dc16072000-01-04 01:10:25 +00001389
Erik Andersen5cbdd712000-01-26 20:06:48 +00001390extern int del_loop(const char *device)
1391{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001392 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001393
Erik Andersene49d5ec2000-02-08 19:58:47 +00001394 if ((fd = open(device, O_RDONLY)) < 0) {
1395 perror(device);
1396 return (FALSE);
1397 }
1398 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1399 perror("ioctl: LOOP_CLR_FD");
1400 return (FALSE);
1401 }
1402 close(fd);
1403 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001404}
Erik Andersende7965c2000-01-26 23:49:21 +00001405
Erik Andersene49d5ec2000-02-08 19:58:47 +00001406extern int set_loop(const char *device, const char *file, int offset,
1407 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001408{
1409 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001410 int fd, ffd, mode;
1411
Erik Andersende7965c2000-01-26 23:49:21 +00001412 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001413 if ((ffd = open(file, mode)) < 0 && !*loopro
1414 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1415 perror(file);
1416 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001417 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001418 if ((fd = open(device, mode)) < 0) {
1419 close(ffd);
1420 perror(device);
1421 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001422 }
1423 *loopro = (mode == O_RDONLY);
1424
1425 memset(&loopinfo, 0, sizeof(loopinfo));
1426 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001427 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001428
1429 loopinfo.lo_offset = offset;
1430
1431 loopinfo.lo_encrypt_key_size = 0;
1432 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1433 perror("ioctl: LOOP_SET_FD");
1434 close(fd);
1435 close(ffd);
1436 return 1;
1437 }
1438 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1439 (void) ioctl(fd, LOOP_CLR_FD, 0);
1440 perror("ioctl: LOOP_SET_STATUS");
1441 close(fd);
1442 close(ffd);
1443 return 1;
1444 }
1445 close(fd);
1446 close(ffd);
1447 return 0;
1448}
1449
Erik Andersene49d5ec2000-02-08 19:58:47 +00001450extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001451{
1452 char dev[20];
1453 int i, fd;
1454 struct stat statbuf;
1455 struct loop_info loopinfo;
1456
Erik Andersene49d5ec2000-02-08 19:58:47 +00001457 for (i = 0; i <= 7; i++) {
1458 sprintf(dev, "/dev/loop%d", i);
1459 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1460 if ((fd = open(dev, O_RDONLY)) >= 0) {
1461 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1462 if (errno == ENXIO) { /* probably free */
1463 close(fd);
1464 return strdup(dev);
1465 }
1466 }
1467 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001468 }
Erik Andersende7965c2000-01-26 23:49:21 +00001469 }
Erik Andersende7965c2000-01-26 23:49:21 +00001470 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001471 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001472}
Erik Andersende7965c2000-01-26 23:49:21 +00001473
Erik Andersenec5bd902000-03-22 07:12:05 +00001474extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001475{
Erik Andersen016ffe92000-03-22 00:58:54 +00001476 DIR *dir;
1477 struct dirent *entry;
1478 struct stat statBuf, rootStat;
1479 char fileName[BUFSIZ];
1480
Erik Andersenec5bd902000-03-22 07:12:05 +00001481 if (stat("/", &rootStat) != 0) {
1482 errorMsg("could not stat '/'\n");
1483 return( FALSE);
1484 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001485
Erik Andersenec5bd902000-03-22 07:12:05 +00001486 dir = opendir("/dev");
1487 if (!dir) {
1488 errorMsg("could not open '/dev'\n");
1489 return( FALSE);
1490 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001491
1492 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001493
Erik Andersen016ffe92000-03-22 00:58:54 +00001494 /* Must skip ".." since that is "/", and so we
1495 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001496 if (strcmp(entry->d_name, "..") == 0)
1497 continue;
1498
1499 sprintf( fileName, "/dev/%s", entry->d_name);
1500
1501 if (stat(fileName, &statBuf) != 0)
1502 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001503 /* Some char devices have the same dev_t as block
1504 * devices, so make sure this is a block device */
1505 if (! S_ISBLK(statBuf.st_mode))
1506 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001507 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001508 strcpy(name, fileName);
1509 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001510 }
1511 }
1512
Erik Andersenec5bd902000-03-22 07:12:05 +00001513 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001514}
Erik Andersen016ffe92000-03-22 00:58:54 +00001515
John Beppu5a728cf2000-04-17 04:22:09 +00001516const unsigned int CSTRING_BUFFER_LENGTH = 128;
1517/* recursive parser that returns cstrings of arbitrary length
1518 * from a FILE*
1519 */
1520static char *
1521cstring_alloc(FILE* f, int depth)
1522{
1523 char *cstring;
1524 char buffer[CSTRING_BUFFER_LENGTH];
1525 int target = CSTRING_BUFFER_LENGTH * depth;
1526 int i, len;
1527 int size;
1528
1529 /* fill buffer */
1530 i = 0;
1531 while ((buffer[i] = fgetc(f)) != EOF) {
1532 if (buffer[i++] == 0x0a) { break; }
1533 if (i == CSTRING_BUFFER_LENGTH) { break; }
1534 }
1535 len = i;
1536
1537 /* recurse or malloc? */
1538 if (len == CSTRING_BUFFER_LENGTH) {
1539 cstring = cstring_alloc(f, (depth + 1));
1540 } else {
1541 /* [special case] EOF */
1542 if ((depth | len) == 0) { return NULL; }
1543
1544 /* malloc */
1545 size = target + len + 1;
1546 cstring = malloc(size);
1547 if (!cstring) { return NULL; }
1548 cstring[size - 1] = 0;
1549 }
1550
1551 /* copy buffer */
1552 if (cstring) {
1553 memcpy(&cstring[target], buffer, len);
1554 }
1555 return cstring;
1556}
1557
1558/*
1559 * wrapper around recursive cstring_alloc
1560 * it's the caller's responsibility to free the cstring
1561 */
1562char *
1563cstring_lineFromFile(FILE *f)
1564{
1565 return cstring_alloc(f, 0);
1566}
Erik Andersen016ffe92000-03-22 00:58:54 +00001567
Eric Andersencc8ed391999-10-05 16:24:54 +00001568/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001569/*
1570Local Variables:
1571c-file-style: "linux"
1572c-basic-offset: 4
1573tab-width: 4
1574End:
1575*/