blob: 719cd98ce3141d716d2879bf30123291653efaed [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) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000032 || defined (BB_INSMOD) \
Erik Andersenfac10d72000-02-07 05:29:42 +000033 || defined (BB_LS) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000034 || defined (BB_RM) \
35 || defined (BB_TAR)
Erik Andersenfac10d72000-02-07 05:29:42 +000036/* same conditions as recursiveAction */
37#define bb_need_name_too_long
38#endif
Eric Andersen9f16d612000-06-12 23:11:16 +000039#define bb_need_memory_exhausted
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000040#define bb_need_full_version
Erik Andersenfac10d72000-02-07 05:29:42 +000041#define BB_DECLARE_EXTERN
42#include "messages.c"
43
Eric Andersen2b69c401999-10-05 22:58:32 +000044#include <stdio.h>
45#include <string.h>
46#include <errno.h>
47#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000048#include <dirent.h>
49#include <time.h>
50#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000051#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000052#include <ctype.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000053#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000054#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Erik Andersen246cc6d2000-03-07 07:41:42 +000056/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
57 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000058#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000059//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
60#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
61#endif
62
Erik Andersen5cbdd712000-01-26 20:06:48 +000063
Eric Andersend0246fb1999-11-04 21:18:07 +000064#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000065# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000066const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000067# else
68# if defined BB_FEATURE_USE_PROCFS
69const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000070# else
Erik Andersen269bba22000-03-08 14:50:47 +000071# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000072const char mtab_file[] = "/dev/mtab";
73# else
Erik Andersen269bba22000-03-08 14:50:47 +000074# 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 +000075# endif
76# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000077# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000078#endif
79
Eric Andersend73dc5b1999-11-10 23:13:02 +000080extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000081{
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000082 fprintf(stderr, "%s\n\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 fprintf(stderr, "Usage: %s\n", usage);
84 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000085}
86
Erik Andersen1ad302a2000-03-24 00:54:46 +000087extern void errorMsg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000088{
89 va_list p;
90
91 va_start(p, s);
92 fflush(stdout);
Matt Kraaibe84cd42000-07-12 17:02:35 +000093 fprintf(stderr, "%s: ", applet_name);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000094 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000095 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +000096 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000097}
98
Erik Andersen1ad302a2000-03-24 00:54:46 +000099extern void fatalError(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000100{
101 va_list p;
102
103 va_start(p, s);
104 fflush(stdout);
Matt Kraaibe84cd42000-07-12 17:02:35 +0000105 fprintf(stderr, "%s: ", applet_name);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000106 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000107 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000108 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000109 exit( FALSE);
110}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000111
Erik Andersen23dea9b2000-05-13 00:28:25 +0000112#if defined BB_INIT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000113/* Returns kernel version encoded as major*65536 + minor*256 + patch,
114 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000115 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000116 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000117extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000118{
Erik Andersend07ee462000-02-21 21:26:32 +0000119 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000121
Erik Andersend07ee462000-02-21 21:26:32 +0000122 if (uname(&name) == -1) {
123 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 return (0);
125 }
Erik Andersend07ee462000-02-21 21:26:32 +0000126 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000128}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000129#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000130
Eric Andersen10dc9d42000-06-26 10:45:52 +0000131
132
133#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000134_syscall1(int, sysinfo, struct sysinfo *, info);
135#endif /* BB_INIT */
136
Eric Andersena57ba4d2000-07-08 19:20:49 +0000137#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000138
139#ifndef __NR_umount2
140#define __NR_umount2 52
141#endif
142
143/* Include our own version of <sys/mount.h>, since libc5 doesn't
144 * know about umount2 */
145extern _syscall1(int, umount, const char *, special_file);
146extern _syscall2(int, umount2, const char *, special_file, int, flags);
147extern _syscall5(int, mount, const char *, special_file, const char *, dir,
148 const char *, fstype, unsigned long int, rwflag, const void *, data);
149#endif
150
Eric Andersenc86e5e62000-08-21 19:38:49 +0000151#if defined BB_INSMOD || defined BB_LSMOD
152#ifndef __NR_query_module
153#define __NR_query_module 167
154#endif
155_syscall5(int, query_module, const char *, name, int, which,
156 void *, buf, size_t, bufsize, size_t*, ret);
157#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000158
159
Erik Andersen23dea9b2000-05-13 00:28:25 +0000160#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000161
Erik Andersen029011b2000-03-04 21:19:32 +0000162#define HASH_SIZE 311 /* Should be prime */
163#define hash_inode(i) ((i) % HASH_SIZE)
164
165static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
166
167/*
168 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
169 * `ino_dev_hashtable', else return 0
170 *
171 * If NAME is a non-NULL pointer to a character pointer, and there is
172 * a match, then set *NAME to the value of the name slot in that
173 * bucket.
174 */
175int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
176{
177 ino_dev_hashtable_bucket_t *bucket;
178
179 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
180 while (bucket != NULL) {
181 if ((bucket->ino == statbuf->st_ino) &&
182 (bucket->dev == statbuf->st_dev))
183 {
184 if (name) *name = bucket->name;
185 return 1;
186 }
187 bucket = bucket->next;
188 }
189 return 0;
190}
191
192/* Add statbuf to statbuf hash table */
193void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
194{
195 int i;
196 size_t s;
197 ino_dev_hashtable_bucket_t *bucket;
198
199 i = hash_inode(statbuf->st_ino);
200 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000201 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000202 bucket->ino = statbuf->st_ino;
203 bucket->dev = statbuf->st_dev;
204 if (name)
205 strcpy(bucket->name, name);
206 else
207 bucket->name[0] = '\0';
208 bucket->next = ino_dev_hashtable[i];
209 ino_dev_hashtable[i] = bucket;
210}
211
212/* Clear statbuf hash table */
213void reset_ino_dev_hashtable(void)
214{
215 int i;
216 ino_dev_hashtable_bucket_t *bucket;
217
218 for (i = 0; i < HASH_SIZE; i++) {
219 while (ino_dev_hashtable[i] != NULL) {
220 bucket = ino_dev_hashtable[i]->next;
221 free(ino_dev_hashtable[i]);
222 ino_dev_hashtable[i] = bucket;
223 }
224 }
225}
226
Erik Andersen23dea9b2000-05-13 00:28:25 +0000227#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000228
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000229#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_AR)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000230/*
231 * Return TRUE if a fileName is a directory.
232 * Nonexistant files return FALSE.
233 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000234int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000235{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000236 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000237 int didMalloc = 0;
238
239 if (statBuf == NULL) {
240 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
241 ++didMalloc;
242 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000243
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000245 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000247 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000248
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000249 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
250 status = FALSE;
251 }
252 else status = TRUE;
253
254 if (didMalloc) {
255 free(statBuf);
256 statBuf = NULL;
257 }
258 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000259}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000260#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000261
Eric Andersenebee3012000-09-05 22:17:22 +0000262#if defined (BB_AR) || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000263/*
264 * Copy readSize bytes between two file descriptors
265 */
266int copySubFile(int srcFd, int dstFd, size_t remaining)
267{
268 size_t size;
269 char buffer[BUFSIZ];
270
271 while (remaining > 0) {
272 if (remaining > BUFSIZ)
273 size = BUFSIZ;
274 else
275 size = remaining;
276 if (fullWrite(dstFd, buffer, fullRead(srcFd, buffer, size)) < size)
277 return(FALSE);
278 remaining -= size;
279 }
280 return (TRUE);
281}
282#endif
283
284
Erik Andersen23dea9b2000-05-13 00:28:25 +0000285#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000286/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000287 * Copy one file to another, while possibly preserving its modes, times, and
288 * modes. Returns TRUE if successful, or FALSE on a failure with an error
289 * message output. (Failure is not indicated if attributes cannot be set.)
290 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000291 */
292int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000294 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000295{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 int rfd;
297 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 struct stat srcStatBuf;
300 struct stat dstStatBuf;
301 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000302
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 if (followLinks == TRUE)
304 status = stat(srcName, &srcStatBuf);
305 else
306 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000307
Erik Andersenfac10d72000-02-07 05:29:42 +0000308 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 perror(srcName);
310 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000311 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312
313 if (followLinks == TRUE)
314 status = stat(destName, &dstStatBuf);
315 else
316 status = lstat(destName, &dstStatBuf);
317
Erik Andersen59b9e872000-05-10 05:05:45 +0000318 if (status < 0 || forceFlag==TRUE) {
319 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 dstStatBuf.st_ino = -1;
321 dstStatBuf.st_dev = -1;
322 }
323
324 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
325 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaid537a952000-07-14 01:51:25 +0000326 errorMsg("Copying file \"%s\" to itself\n", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327 return FALSE;
328 }
329
330 if (S_ISDIR(srcStatBuf.st_mode)) {
331 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
332 /* Make sure the directory is writable */
333 status = mkdir(destName, 0777777 ^ umask(0));
334 if (status < 0 && errno != EEXIST) {
335 perror(destName);
336 return FALSE;
337 }
338 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000339 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 int link_size;
341
342 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000343 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
344 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 if (link_size < 0) {
346 perror(srcName);
347 return FALSE;
348 }
349 link_val[link_size] = '\0';
350 status = symlink(link_val, destName);
351 if (status < 0) {
352 perror(destName);
353 return FALSE;
354 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000355#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000357 /* Try to set owner, but fail silently like GNU cp */
358 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000360#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 return TRUE;
362 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
363 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
364 if (mkfifo(destName, 0644) < 0) {
365 perror(destName);
366 return FALSE;
367 }
368 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
369 || S_ISSOCK(srcStatBuf.st_mode)) {
370 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
371 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
372 perror(destName);
373 return FALSE;
374 }
375 } else if (S_ISREG(srcStatBuf.st_mode)) {
376 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
377 rfd = open(srcName, O_RDONLY);
378 if (rfd < 0) {
379 perror(srcName);
380 return FALSE;
381 }
382
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000383 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 srcStatBuf.st_mode);
385 if (wfd < 0) {
386 perror(destName);
387 close(rfd);
388 return FALSE;
389 }
390
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000391 if (copySubFile(rfd, wfd, srcStatBuf.st_size)==FALSE)
392 goto error_exit;
393
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 close(rfd);
395 if (close(wfd) < 0) {
396 return FALSE;
397 }
398 }
399
400 if (setModes == TRUE) {
401 /* This is fine, since symlinks never get here */
402 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
403 perror(destName);
404 exit FALSE;
405 }
406 if (chmod(destName, srcStatBuf.st_mode) < 0) {
407 perror(destName);
408 exit FALSE;
409 }
410 times.actime = srcStatBuf.st_atime;
411 times.modtime = srcStatBuf.st_mtime;
412 if (utime(destName, &times) < 0) {
413 perror(destName);
414 exit FALSE;
415 }
416 }
417
Erik Andersenfac10d72000-02-07 05:29:42 +0000418 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000419
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 error_exit:
421 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000422 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000424
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000426}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000427#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000428
429
430
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000431#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000432
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000433#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
434#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
435
436/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
437static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438 0, 0, S_ISUID,
439 0, 0, S_ISGID,
440 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000441};
442
443/* The 9 mode bits to test */
444static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 S_IRUSR, S_IWUSR, S_IXUSR,
446 S_IRGRP, S_IWGRP, S_IXGRP,
447 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000448};
449
450#define MODE1 "rwxrwxrwx"
451#define MODE0 "---------"
452#define SMODE1 "..s..s..t"
453#define SMODE0 "..S..S..T"
454
Eric Andersencc8ed391999-10-05 16:24:54 +0000455/*
456 * Return the standard ls-like mode string from a file mode.
457 * This is static and so is overwritten on each call.
458 */
Eric Andersenf811e071999-10-09 00:25:00 +0000459const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000460{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000462
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 int i;
464
465 buf[0] = TYPECHAR(mode);
466 for (i = 0; i < 9; i++) {
467 if (mode & SBIT[i])
468 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
469 else
470 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
471 }
472 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000473}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000474#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000475
476
Pavel Roskin21ddb382000-07-17 19:31:54 +0000477#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000478/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000479 * Return the standard ls-like time string from a time_t
480 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000481 */
Eric Andersenf811e071999-10-09 00:25:00 +0000482const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000483{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 time_t now;
485 char *str;
486 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000487
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000489
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000491
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 strcpy(buf, &str[4]);
493 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000494
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
496 strcpy(&buf[7], &str[20]);
497 buf[11] = '\0';
498 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000499
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000501}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000502#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000503
Pavel Roskin21ddb382000-07-17 19:31:54 +0000504#if defined BB_TAR || defined BB_CP_MV || defined BB_AR
Eric Andersen17d49ef1999-10-06 20:25:32 +0000505/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000506 * Write all of the supplied buffer out to a file.
507 * This does multiple writes as necessary.
508 * Returns the amount written, or -1 on an error.
509 */
Eric Andersenf811e071999-10-09 00:25:00 +0000510int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000511{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 int cc;
513 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000514
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000516
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 while (len > 0) {
518 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000519
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 if (cc < 0)
521 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000522
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 buf += cc;
524 total += cc;
525 len -= cc;
526 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000527
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000529}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000530#endif /* BB_TAR || BB_CP_MV || BB_AR */
Eric Andersencc8ed391999-10-05 16:24:54 +0000531
532
Eric Andersenebee3012000-09-05 22:17:22 +0000533#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV
Eric Andersencc8ed391999-10-05 16:24:54 +0000534/*
535 * Read all of the supplied buffer from a file.
536 * This does multiple reads as necessary.
537 * Returns the amount read, or -1 on an error.
538 * A short read is returned on an end of file.
539 */
Eric Andersenf811e071999-10-09 00:25:00 +0000540int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000541{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 int cc;
543 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000544
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000546
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 while (len > 0) {
548 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 if (cc < 0)
551 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000552
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 if (cc == 0)
554 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000555
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 buf += cc;
557 total += cc;
558 len -= cc;
559 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000560
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000562}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000563#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000564
565
Erik Andersen23dea9b2000-05-13 00:28:25 +0000566#if defined (BB_CHMOD_CHOWN_CHGRP) \
567 || defined (BB_CP_MV) \
568 || defined (BB_FIND) \
569 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000570 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000571 || defined (BB_RM) \
572 || defined (BB_TAR)
573
Eric Andersencc8ed391999-10-05 16:24:54 +0000574/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000575 * Walk down all the directories under the specified
576 * location, and do something (something specified
577 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000578 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000579 * Unfortunatly, while nftw(3) could replace this and reduce
580 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000581 * and so isn't sufficiently portable to take over since glibc2.1
582 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000583 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000584int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 int recurse, int followLinks, int depthFirst,
586 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000587 struct stat * statbuf,
588 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000590 struct stat * statbuf,
591 void* userData),
592 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000593{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 int status;
595 struct stat statbuf;
596 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000597
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 if (followLinks == TRUE)
599 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000600 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000602
Eric Andersen2b69c401999-10-05 22:58:32 +0000603 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604#ifdef BB_DEBUG_PRINT_SCAFFOLD
605 fprintf(stderr,
606 "status=%d followLinks=%d TRUE=%d\n",
607 status, followLinks, TRUE);
608#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000609 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000610 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000611 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000612
613 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
614 if (fileAction == NULL)
615 return TRUE;
616 else
Erik Andersen3364d782000-03-28 00:58:14 +0000617 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 }
619
620 if (recurse == FALSE) {
621 if (S_ISDIR(statbuf.st_mode)) {
622 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000623 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 else
625 return TRUE;
626 }
627 }
628
629 if (S_ISDIR(statbuf.st_mode)) {
630 DIR *dir;
631
632 dir = opendir(fileName);
633 if (!dir) {
634 perror(fileName);
635 return FALSE;
636 }
637 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000638 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639 if (status == FALSE) {
640 perror(fileName);
641 return FALSE;
642 }
643 }
644 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000645 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646
647 if ((strcmp(next->d_name, "..") == 0)
648 || (strcmp(next->d_name, ".") == 0)) {
649 continue;
650 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000651 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Matt Kraaid537a952000-07-14 01:51:25 +0000652 errorMsg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 return FALSE;
654 }
Erik Andersenc3660502000-04-18 21:18:57 +0000655 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000656 sprintf(nextFile, "%s/%s", fileName, next->d_name);
657 status =
658 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000659 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000660 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000661 closedir(dir);
662 return FALSE;
663 }
664 }
665 status = closedir(dir);
666 if (status < 0) {
667 perror(fileName);
668 return FALSE;
669 }
670 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000671 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 if (status == FALSE) {
673 perror(fileName);
674 return FALSE;
675 }
676 }
677 } else {
678 if (fileAction == NULL)
679 return TRUE;
680 else
Erik Andersen3364d782000-03-28 00:58:14 +0000681 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 }
683 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000684}
685
Erik Andersen23dea9b2000-05-13 00:28:25 +0000686#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000687
Eric Andersenf6be9441999-10-13 21:12:06 +0000688
689
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000690#if defined (BB_TAR) || defined (BB_MKDIR) || defined (BB_AR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000691/*
692 * Attempt to create the directories along the specified path, except for
693 * the final component. The mode is given for the final directory only,
694 * while all previous ones get default protections. Errors are not reported
695 * here, as failures to restore files can be reported later.
696 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000698{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 char *cp;
700 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000701 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000702 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000703
Erik Andersene49d5ec2000-02-08 19:58:47 +0000704 strcpy(buf, name);
705 for (cp = buf; *cp == '/'; cp++);
706 cp = strchr(cp, '/');
707 while (cp) {
708 cpOld = cp;
709 cp = strchr(cp + 1, '/');
710 *cpOld = '\0';
711 retVal = mkdir(buf, cp ? 0777 : mode);
712 if (retVal != 0 && errno != EEXIST) {
713 perror(buf);
714 return FALSE;
715 }
716 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000717 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000719}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000720#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000721
722
723
Pavel Roskin21ddb382000-07-17 19:31:54 +0000724#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000725 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000726/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000727
728
729
Erik Andersene49d5ec2000-02-08 19:58:47 +0000730extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000731{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 mode_t andMode =
733
734 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000735 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736 mode_t mode = 0;
737 mode_t groups = 0;
738 char type;
739 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000740
Eric Andersen4e61db22000-09-01 00:06:17 +0000741 if (s==NULL)
742 return (FALSE);
743
Eric Andersenf6be9441999-10-13 21:12:06 +0000744 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 for (;;) {
746 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000747 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000748 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000749 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000750 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000751 continue;
752 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000753 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000754 continue;
755 case 'o':
756 groups |= S_IRWXO;
757 continue;
758 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000760 continue;
761 case '+':
762 case '=':
763 case '-':
764 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000765 if (groups == 0) /* The default is "all" */
766 groups |=
767 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000768 break;
769 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 if (isdigit(c) && c >= '0' && c <= '7' &&
771 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000772 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000773 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000775 return (FALSE);
776 }
777 break;
778 }
779
Erik Andersene49d5ec2000-02-08 19:58:47 +0000780 while ((c = *s++) != '\0') {
781 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000782 case ',':
783 break;
784 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000785 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000786 continue;
787 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000789 continue;
790 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000791 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000792 continue;
793 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000795 continue;
796 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000797 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000798 continue;
799 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000800 *theMode &= andMode;
801 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000802 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000803 }
804 break;
805 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000806 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000807 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000808 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000809 /* fall through */
810 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000811 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000812 break;
813 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000814 andMode &= ~(mode & groups);
815 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000816 break;
817 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000819 *theMode &= andMode;
820 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000821 return (TRUE);
822}
Eric Andersence8f3b91999-10-20 07:03:36 +0000823
824
Pavel Roskin21ddb382000-07-17 19:31:54 +0000825#endif
826/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000827
Eric Andersene674eb71999-10-19 20:52:57 +0000828
829
Erik Andersen23dea9b2000-05-13 00:28:25 +0000830
831
Pavel Roskin21ddb382000-07-17 19:31:54 +0000832#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
833 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
834 || defined BB_LOGNAME || defined BB_WHOAMI
Erik Andersen23dea9b2000-05-13 00:28:25 +0000835
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000836/* This parses entries in /etc/passwd and /etc/group. This is desirable
837 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
838 * increases target size and is often not needed or wanted for embedded
839 * systems.
840 *
841 * /etc/passwd entries look like this:
842 * root:x:0:0:root:/root:/bin/bash
843 * and /etc/group entries look like this:
844 * root:x:0:
845 *
846 * This uses buf as storage to hold things.
847 *
848 */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000849unsigned long my_getid(const char *filename, char *name, long id, long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000850{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000851 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000852 char *rname, *start, *end, buf[128];
Eric Andersenfad04fd2000-07-14 06:49:52 +0000853 long rid;
854 long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000855
Erik Andersene49d5ec2000-02-08 19:58:47 +0000856 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000857 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000858 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000859 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000861 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000862
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000864 if (buf[0] == '#')
865 continue;
866
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000867 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000868 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000870 if (end == NULL)
871 continue;
872 *end = '\0';
873 rname = start;
874
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000875 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000876 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000878 if (end == NULL)
879 continue;
880
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000881 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000882 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000883 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000884 if (end == start)
885 continue;
886
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000887 /* gid in passwd */
888 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000889 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000890
Eric Andersend23f9ba1999-10-20 19:18:15 +0000891 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000892 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000893 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 fclose(file);
895 return (rid);
896 }
897 }
898 if (id != -1 && id == rid) {
899 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000900 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000901 fclose(file);
902 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000903 }
904 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000905 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000906 return (-1);
907}
908
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000909/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000910long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000911{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000912 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000913}
914
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000915/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000916long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000917{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000918 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000919}
920
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000921/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000922void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000923{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000924 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000925}
926
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000927/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000928void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000929{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000930 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000931}
932
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000933/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000934long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000935{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000936 long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000937 my_getid("/etc/passwd", name, -1, &gid);
938 return gid;
939}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000940
Pavel Roskin21ddb382000-07-17 19:31:54 +0000941#endif
942 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
943 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000944
945
Eric Andersenf86aee72000-07-06 17:20:46 +0000946#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000947
Eric Andersenbd22ed82000-07-08 18:55:24 +0000948/* From <linux/kd.h> */
949#define KDGKBTYPE 0x4B33 /* get keyboard type */
950#define KB_84 0x01
951#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000952
Erik Andersene49d5ec2000-02-08 19:58:47 +0000953int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000954{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000955 char arg;
956
957 arg = 0;
958 return (ioctl(fd, KDGKBTYPE, &arg) == 0
959 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000960}
961
Erik Andersene49d5ec2000-02-08 19:58:47 +0000962static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000963{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000964 int fd;
965
966 /* try read-only */
967 fd = open(fnam, O_RDWR);
968
969 /* if failed, try read-only */
970 if (fd < 0 && errno == EACCES)
971 fd = open(fnam, O_RDONLY);
972
973 /* if failed, try write-only */
974 if (fd < 0 && errno == EACCES)
975 fd = open(fnam, O_WRONLY);
976
977 /* if failed, fail */
978 if (fd < 0)
979 return -1;
980
981 /* if not a console, fail */
982 if (!is_a_console(fd)) {
983 close(fd);
984 return -1;
985 }
986
987 /* success */
988 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000989}
990
991/*
992 * Get an fd for use with kbd/console ioctls.
993 * We try several things because opening /dev/console will fail
994 * if someone else used X (which does a chown on /dev/console).
995 *
996 * if tty_name is non-NULL, try this one instead.
997 */
998
Erik Andersene49d5ec2000-02-08 19:58:47 +0000999int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001000{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001001 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001002
Erik Andersene49d5ec2000-02-08 19:58:47 +00001003 if (tty_name) {
1004 if (-1 == (fd = open_a_console(tty_name)))
1005 return -1;
1006 else
1007 return fd;
1008 }
1009
1010 fd = open_a_console("/dev/tty");
1011 if (fd >= 0)
1012 return fd;
1013
1014 fd = open_a_console("/dev/tty0");
1015 if (fd >= 0)
1016 return fd;
1017
1018 fd = open_a_console("/dev/console");
1019 if (fd >= 0)
1020 return fd;
1021
1022 for (fd = 0; fd < 3; fd++)
1023 if (is_a_console(fd))
1024 return fd;
1025
Matt Kraaid537a952000-07-14 01:51:25 +00001026 errorMsg("Couldnt get a file descriptor referring to the console\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001027 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001028}
1029
1030
Eric Andersenf86aee72000-07-06 17:20:46 +00001031#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001032
1033
Eric Andersen9f16d612000-06-12 23:11:16 +00001034#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001035/*
1036 * Routine to see if a text string is matched by a wildcard pattern.
1037 * Returns TRUE if the text is matched, or FALSE if it is not matched
1038 * or if the pattern is invalid.
1039 * * matches zero or more characters
1040 * ? matches a single character
1041 * [abc] matches 'a', 'b' or 'c'
1042 * \c quotes character c
1043 * Adapted from code written by Ingo Wilken, and
1044 * then taken from sash, Copyright (c) 1999 by David I. Bell
1045 * Permission is granted to use, distribute, or modify this source,
1046 * provided that this copyright notice remains intact.
1047 * Permission to distribute this code under the GPL has been granted.
1048 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001049extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001050{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001051 const char *retryPat;
1052 const char *retryText;
1053 int ch;
1054 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001055 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001056
Erik Andersene49d5ec2000-02-08 19:58:47 +00001057 retryPat = NULL;
1058 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001059
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001061 ch = *pattern++;
1062
Erik Andersene49d5ec2000-02-08 19:58:47 +00001063 switch (ch) {
1064 case '*':
1065 retryPat = pattern;
1066 retryText = text;
1067 break;
1068
1069 case '[':
1070 found = FALSE;
1071
1072 while ((ch = *pattern++) != ']') {
1073 if (ch == '\\')
1074 ch = *pattern++;
1075
1076 if (ch == '\0')
1077 return FALSE;
1078
1079 if (*text == ch)
1080 found = TRUE;
1081 }
Erik Andersen632bb572000-04-18 22:09:06 +00001082 len=strlen(text);
1083 if (found == FALSE && len!=0) {
1084 return FALSE;
1085 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001087 if (strlen(pattern)==0 && len==1) {
1088 return TRUE;
1089 }
1090 if (len!=0) {
1091 text++;
1092 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001093 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 }
1095
1096 /* fall into next case */
1097
1098 case '?':
1099 if (*text++ == '\0')
1100 return FALSE;
1101
1102 break;
1103
1104 case '\\':
1105 ch = *pattern++;
1106
1107 if (ch == '\0')
1108 return FALSE;
1109
1110 /* fall into next case */
1111
1112 default:
1113 if (*text == ch) {
1114 if (*text)
1115 text++;
1116 break;
1117 }
1118
1119 if (*text) {
1120 pattern = retryPat;
1121 text = ++retryText;
1122 break;
1123 }
1124
Eric Andersenb186d981999-12-03 09:19:54 +00001125 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001126 }
1127
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128 if (pattern == NULL)
1129 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001130 }
1131
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001133}
Eric Andersen9f16d612000-06-12 23:11:16 +00001134#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001135
1136
Eric Andersen29d2e361999-11-06 06:07:27 +00001137
1138
Erik Andersen23dea9b2000-05-13 00:28:25 +00001139#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001140/*
1141 * Given a block device, find the mount table entry if that block device
1142 * is mounted.
1143 *
1144 * Given any other file (or directory), find the mount table entry for its
1145 * filesystem.
1146 */
1147extern struct mntent *findMountPoint(const char *name, const char *table)
1148{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001149 struct stat s;
1150 dev_t mountDevice;
1151 FILE *mountTable;
1152 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001153
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 if (stat(name, &s) != 0)
1155 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001156
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157 if ((s.st_mode & S_IFMT) == S_IFBLK)
1158 mountDevice = s.st_rdev;
1159 else
1160 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001161
1162
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 if ((mountTable = setmntent(table, "r")) == 0)
1164 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001165
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 while ((mountEntry = getmntent(mountTable)) != 0) {
1167 if (strcmp(name, mountEntry->mnt_dir) == 0
1168 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1169 break;
1170 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1171 break;
1172 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1173 break;
1174 }
1175 endmntent(mountTable);
1176 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001177}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001178#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001179
1180
1181
Erik Andersen23dea9b2000-05-13 00:28:25 +00001182#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001183/*
1184 * Read a number with a possible multiplier.
1185 * Returns -1 if the number format is illegal.
1186 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001187extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001188{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001190
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 if (!isDecimal(*cp))
1192 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001193
Erik Andersene49d5ec2000-02-08 19:58:47 +00001194 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001195
Erik Andersene49d5ec2000-02-08 19:58:47 +00001196 while (isDecimal(*cp))
1197 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001198
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 switch (*cp++) {
1200 case 'M':
1201 case 'm': /* `tail' uses it traditionally */
1202 value *= 1048576;
1203 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001204
Erik Andersene49d5ec2000-02-08 19:58:47 +00001205 case 'k':
1206 value *= 1024;
1207 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001208
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209 case 'b':
1210 value *= 512;
1211 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001212
Erik Andersene49d5ec2000-02-08 19:58:47 +00001213 case 'w':
1214 value *= 2;
1215 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001216
Erik Andersene49d5ec2000-02-08 19:58:47 +00001217 case '\0':
1218 return value;
1219
1220 default:
1221 return -1;
1222 }
1223
1224 if (*cp)
1225 return -1;
1226
Eric Andersen1792f8c1999-12-09 06:11:36 +00001227 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001228}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001229#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001230
Eric Andersen6805d5d1999-12-09 22:39:55 +00001231
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001232#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001233/* try to open up the specified device */
1234extern int device_open(char *device, int mode)
1235{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001236 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001237
Erik Andersene49d5ec2000-02-08 19:58:47 +00001238 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001239
Erik Andersene49d5ec2000-02-08 19:58:47 +00001240 /* Retry up to 5 times */
1241 for (f = 0; f < 5; f++)
1242 if ((fd = open(device, m, 0600)) >= 0)
1243 break;
1244 if (fd < 0)
1245 return fd;
1246 /* Reset original flags. */
1247 if (m != mode)
1248 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001249 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001250}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001251#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001252
1253
Erik Andersen23dea9b2000-05-13 00:28:25 +00001254#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001255#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001256#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001257#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001258
Erik Andersen23dea9b2000-05-13 00:28:25 +00001259#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001260/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001261 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001262 * This finds the pid of the specified process,
1263 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001264 *
Erik Andersen825aead2000-04-07 06:00:07 +00001265 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001266 */
Erik Andersen825aead2000-04-07 06:00:07 +00001267extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001268{
Erik Andersen825aead2000-04-07 06:00:07 +00001269 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001270 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001271 pid_t num_pids;
1272 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001273 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001274
1275 /* open device */
1276 fd = open(device, O_RDONLY);
1277 if (fd < 0)
1278 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1279
1280 /* Find out how many processes there are */
1281 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1282 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1283
1284 /* Allocate some memory -- grab a few extras just in case
1285 * some new processes start up while we wait. The kernel will
1286 * just ignore any extras if we give it too many, and will trunc.
1287 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001288 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001289 pid_array[0] = num_pids+10;
1290
1291 /* Now grab the pid list */
1292 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1293 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1294
1295 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001296 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001297 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001298 struct pid_info info;
1299
1300 info.pid = pid_array[i];
1301 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1302 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1303
Erik Andersen0a64de92000-04-18 20:00:03 +00001304 /* Make sure we only match on the process name */
1305 p=info.command_line+1;
1306 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1307 (p)++;
1308 }
1309 if (isspace(*(p)))
1310 *p='\0';
1311
Erik Andersenb9167cb2000-04-18 22:41:30 +00001312 if ((strstr(info.command_line, pidName) != NULL)
1313 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001314 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001315 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001316 }
1317 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001318 if (pidList)
1319 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001320
1321 /* Free memory */
1322 free( pid_array);
1323
1324 /* close device */
1325 if (close (fd) != 0)
1326 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1327
Erik Andersen825aead2000-04-07 06:00:07 +00001328 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001329}
Erik Andersen269bba22000-03-08 14:50:47 +00001330#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001331#if ! defined BB_FEATURE_USE_PROCFS
1332#error Sorry, I depend on the /proc filesystem right now.
1333#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001334
Erik Andersen246cc6d2000-03-07 07:41:42 +00001335/* findPidByName()
1336 *
1337 * This finds the pid of the specified process.
1338 * Currently, it's implemented by rummaging through
1339 * the proc filesystem.
1340 *
Erik Andersen825aead2000-04-07 06:00:07 +00001341 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001342 */
Erik Andersen825aead2000-04-07 06:00:07 +00001343extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001344{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001345 DIR *dir;
1346 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001347 pid_t* pidList=NULL;
1348 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001349
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001350 dir = opendir("/proc");
1351 if (!dir)
1352 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1353
1354 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001355 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001356 char filename[256];
1357 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001358
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001359 /* If it isn't a number, we don't want it */
1360 if (!isdigit(*next->d_name))
1361 continue;
1362
Eric Andersen31e68292000-09-14 21:12:20 +00001363 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001364 status = fopen(filename, "r");
1365 if (!status) {
1366 continue;
1367 }
1368 fgets(buffer, 256, status);
1369 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001370
Eric Andersen31e68292000-09-14 21:12:20 +00001371 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001372 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001373 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001374 }
John Beppuf95ca971999-12-09 22:10:18 +00001375 }
Eric Andersen31e68292000-09-14 21:12:20 +00001376
Erik Andersen0a64de92000-04-18 20:00:03 +00001377 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001378 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001379 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001380}
Erik Andersen269bba22000-03-08 14:50:47 +00001381#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001382#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001383
Eric Andersenae6eae02000-07-19 17:35:54 +00001384#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001385/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001386extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001387{
Eric Andersenae6eae02000-07-19 17:35:54 +00001388 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001389
Eric Andersenae6eae02000-07-19 17:35:54 +00001390 if (!ptr)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001391 fatalError(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001392 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001393}
1394
Eric Andersen501c88b2000-07-28 15:14:45 +00001395extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001396{
1397 void *ptr = realloc(old, size);
1398 if (!ptr)
1399 fatalError(memory_exhausted);
1400 return ptr;
1401}
Eric Andersen501c88b2000-07-28 15:14:45 +00001402
1403extern void *xcalloc(size_t nmemb, size_t size)
1404{
1405 void *ptr = calloc(nmemb, size);
1406 if (!ptr)
1407 fatalError(memory_exhausted);
1408 return ptr;
1409}
Eric Andersenae6eae02000-07-19 17:35:54 +00001410#endif
1411
Eric Andersen11c65522000-09-07 17:24:47 +00001412#if defined BB_FEATURE_NFSMOUNT || defined BB_SH || defined BB_LS
Eric Andersenae6eae02000-07-19 17:35:54 +00001413# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001414extern char * xstrdup (const char *s) {
1415 char *t;
1416
1417 if (s == NULL)
1418 return NULL;
1419
1420 t = strdup (s);
1421
1422 if (t == NULL)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001423 fatalError(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001424
1425 return t;
1426}
Eric Andersenae6eae02000-07-19 17:35:54 +00001427# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001428#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001429
Matt Kraai88cc3052000-08-26 16:52:28 +00001430#if defined BB_FEATURE_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001431extern char * xstrndup (const char *s, int n) {
1432 char *t;
1433
1434 if (s == NULL)
1435 fatalError("xstrndup bug");
1436
1437 t = xmalloc(n+1);
1438 strncpy(t,s,n);
1439 t[n] = 0;
1440
1441 return t;
1442}
1443#endif
1444
1445
Erik Andersen23dea9b2000-05-13 00:28:25 +00001446#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001447extern int vdprintf(int d, const char *format, va_list ap)
1448{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001449 char buf[BUF_SIZE];
1450 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001451
Erik Andersene49d5ec2000-02-08 19:58:47 +00001452 len = vsprintf(buf, format, ap);
1453 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001454}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001455#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001456
Eric Andersen624cc772000-09-21 02:04:51 +00001457
Erik Andersen23dea9b2000-05-13 00:28:25 +00001458#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001459#include <fcntl.h>
1460#include "loop.h" /* Pull in loop device support */
1461
Erik Andersen5cbdd712000-01-26 20:06:48 +00001462extern int del_loop(const char *device)
1463{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001464 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001465
Erik Andersene49d5ec2000-02-08 19:58:47 +00001466 if ((fd = open(device, O_RDONLY)) < 0) {
1467 perror(device);
1468 return (FALSE);
1469 }
1470 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1471 perror("ioctl: LOOP_CLR_FD");
1472 return (FALSE);
1473 }
1474 close(fd);
1475 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001476}
Erik Andersende7965c2000-01-26 23:49:21 +00001477
Erik Andersene49d5ec2000-02-08 19:58:47 +00001478extern int set_loop(const char *device, const char *file, int offset,
1479 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001480{
1481 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001482 int fd, ffd, mode;
1483
Erik Andersende7965c2000-01-26 23:49:21 +00001484 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001485 if ((ffd = open(file, mode)) < 0 && !*loopro
1486 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1487 perror(file);
1488 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001489 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001490 if ((fd = open(device, mode)) < 0) {
1491 close(ffd);
1492 perror(device);
1493 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001494 }
1495 *loopro = (mode == O_RDONLY);
1496
1497 memset(&loopinfo, 0, sizeof(loopinfo));
1498 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001499 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001500
1501 loopinfo.lo_offset = offset;
1502
1503 loopinfo.lo_encrypt_key_size = 0;
1504 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1505 perror("ioctl: LOOP_SET_FD");
1506 close(fd);
1507 close(ffd);
1508 return 1;
1509 }
1510 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1511 (void) ioctl(fd, LOOP_CLR_FD, 0);
1512 perror("ioctl: LOOP_SET_STATUS");
1513 close(fd);
1514 close(ffd);
1515 return 1;
1516 }
1517 close(fd);
1518 close(ffd);
1519 return 0;
1520}
1521
Erik Andersene49d5ec2000-02-08 19:58:47 +00001522extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001523{
1524 char dev[20];
1525 int i, fd;
1526 struct stat statbuf;
1527 struct loop_info loopinfo;
1528
Erik Andersene49d5ec2000-02-08 19:58:47 +00001529 for (i = 0; i <= 7; i++) {
1530 sprintf(dev, "/dev/loop%d", i);
1531 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1532 if ((fd = open(dev, O_RDONLY)) >= 0) {
1533 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1534 if (errno == ENXIO) { /* probably free */
1535 close(fd);
1536 return strdup(dev);
1537 }
1538 }
1539 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001540 }
Erik Andersende7965c2000-01-26 23:49:21 +00001541 }
Erik Andersende7965c2000-01-26 23:49:21 +00001542 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001543 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001544}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001545#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001546
Erik Andersen23dea9b2000-05-13 00:28:25 +00001547#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001548extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001549{
Erik Andersen016ffe92000-03-22 00:58:54 +00001550 DIR *dir;
1551 struct dirent *entry;
1552 struct stat statBuf, rootStat;
1553 char fileName[BUFSIZ];
1554
Erik Andersenec5bd902000-03-22 07:12:05 +00001555 if (stat("/", &rootStat) != 0) {
1556 errorMsg("could not stat '/'\n");
1557 return( FALSE);
1558 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001559
Erik Andersenec5bd902000-03-22 07:12:05 +00001560 dir = opendir("/dev");
1561 if (!dir) {
1562 errorMsg("could not open '/dev'\n");
1563 return( FALSE);
1564 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001565
1566 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001567
Erik Andersen016ffe92000-03-22 00:58:54 +00001568 /* Must skip ".." since that is "/", and so we
1569 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001570 if (strcmp(entry->d_name, "..") == 0)
1571 continue;
1572
Eric Andersenf9ca6532000-08-11 17:45:02 +00001573 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001574
1575 if (stat(fileName, &statBuf) != 0)
1576 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001577 /* Some char devices have the same dev_t as block
1578 * devices, so make sure this is a block device */
1579 if (! S_ISBLK(statBuf.st_mode))
1580 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001581 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001582 strcpy(name, fileName);
1583 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001584 }
1585 }
1586
Erik Andersenec5bd902000-03-22 07:12:05 +00001587 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001588}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001589#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001590
Mark Whitley1ca41772000-06-28 22:15:26 +00001591
1592/* get_line_from_file() - This function reads an entire line from a text file
1593 * up to a newline. It returns a malloc'ed char * which must be stored and
1594 * free'ed by the caller. */
1595extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001596{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001597 static const int GROWBY = 80; /* how large we will grow strings by */
1598
Mark Whitley1ca41772000-06-28 22:15:26 +00001599 int ch;
1600 int idx = 0;
1601 char *linebuf = NULL;
1602 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001603
Mark Whitley1ca41772000-06-28 22:15:26 +00001604 while (1) {
1605 ch = fgetc(file);
1606 if (ch == EOF)
1607 break;
1608 /* grow the line buffer as necessary */
Eric Andersen161cf932000-07-09 02:38:01 +00001609 if (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001610 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001611 linebuf[idx++] = (char)ch;
1612 if ((char)ch == '\n')
1613 break;
1614 }
John Beppu5a728cf2000-04-17 04:22:09 +00001615
Mark Whitley1ca41772000-06-28 22:15:26 +00001616 if (idx == 0)
1617 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001618
Mark Whitley1ca41772000-06-28 22:15:26 +00001619 linebuf[idx] = 0;
1620 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001621}
Erik Andersen016ffe92000-03-22 00:58:54 +00001622
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001623#if defined BB_CAT || defined BB_LSMOD
1624extern void print_file(FILE *file)
1625{
1626 int c;
1627
1628 while ((c = getc(file)) != EOF)
1629 putc(c, stdout);
1630 fclose(file);
1631 fflush(stdout);
1632}
1633
1634extern int print_file_by_name(char *filename)
1635{
1636 FILE *file;
1637 file = fopen(filename, "r");
1638 if (file == NULL) {
1639 return FALSE;
1640 }
1641 print_file(file);
1642 return TRUE;
1643}
1644#endif /* BB_CAT || BB_LSMOD */
1645
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001646#if defined BB_ECHO || defined BB_TR
1647char process_escape_sequence(char **ptr)
1648{
1649 char c;
1650
1651 switch (c = *(*ptr)++) {
1652 case 'a':
1653 c = '\a';
1654 break;
1655 case 'b':
1656 c = '\b';
1657 break;
1658 case 'f':
1659 c = '\f';
1660 break;
1661 case 'n':
1662 c = '\n';
1663 break;
1664 case 't':
1665 c = '\t';
1666 break;
1667 case 'v':
1668 c = '\v';
1669 break;
1670 case '\\':
1671 c = '\\';
1672 break;
1673 case '0': case '1': case '2': case '3':
1674 case '4': case '5': case '6': case '7':
1675 c -= '0';
1676 if ('0' <= **ptr && **ptr <= '7') {
1677 c = c * 8 + (*(*ptr)++ - '0');
1678 if ('0' <= **ptr && **ptr <= '7')
1679 c = c * 8 + (*(*ptr)++ - '0');
1680 }
1681 break;
1682 default:
1683 (*ptr)--;
1684 c = '\\';
1685 break;
1686 }
1687 return c;
1688}
1689#endif
1690
Eric Andersend13ebff2000-09-05 21:50:52 +00001691#if defined BB_BASENAME || defined BB_LN || defined BB_SH
Eric Andersen0b4551f2000-07-10 16:44:03 +00001692char *get_last_path_component(char *path)
1693{
1694 char *s=path+strlen(path)-1;
1695
1696 /* strip trailing slashes */
1697 while (s && *s == '/') {
1698 *s-- = '\0';
1699 }
1700
1701 /* find last component */
1702 s = strrchr(path, '/');
1703 if (s==NULL) return path;
1704 else return s+1;
1705}
1706#endif
1707
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001708#if defined BB_GREP || defined BB_SED
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001709void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001710{
1711 int ret;
1712 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1713 int errmsgsz = regerror(ret, preg, NULL, 0);
1714 char *errmsg = xmalloc(errmsgsz);
1715 regerror(ret, preg, errmsg, errmsgsz);
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001716 fatalError("bb_regcomp: %s\n", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001717 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001718}
1719#endif
1720
Eric Andersencc8ed391999-10-05 16:24:54 +00001721/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001722/*
1723Local Variables:
1724c-file-style: "linux"
1725c-basic-offset: 4
1726tab-width: 4
1727End:
1728*/