1 /* vi: set sw=4 ts=4: */
5 * 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.
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.
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.
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
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.
29 #if defined (BB_CHMOD_CHOWN_CHGRP) \
30 || defined (BB_CP_MV) \
31 || defined (BB_FIND) \
33 || defined (BB_INSMOD)
34 /* same conditions as recursiveAction */
35 #define bb_need_name_too_long
37 #define BB_DECLARE_EXTERN
50 #include <sys/param.h> /* for PATH_MAX */
51 #include <sys/utsname.h> /* for uname(2) */
53 #if defined BB_FEATURE_MOUNT_LOOP
55 #include <sys/ioctl.h>
56 #include <linux/loop.h>
59 /* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
60 * list of available filesystems used for the -t auto option */
61 #if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
62 //#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."
67 #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
68 # if defined BB_FEATURE_USE_PROCFS
69 const char mtab_file[] = "/proc/mounts";
72 const char mtab_file[] = "/etc/mtab";
74 # if defined BB_FEATURE_USE_DEVPS_PATCH
75 const char mtab_file[] = "/dev/mtab";
77 # 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)
84 extern void usage(const char *usage)
86 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
88 fprintf(stderr, "Usage: %s\n", usage);
92 extern void errorMsg(char *s, ...)
98 vfprintf(stderr, s, p);
99 fprintf(stderr, "\n");
103 extern void fatalError(char *s, ...)
109 vfprintf(stderr, s, p);
110 fprintf(stderr, "\n");
115 #if defined (BB_INIT) || defined (BB_PS)
116 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
117 * so, for example, to check if the kernel is greater than 2.2.11:
118 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
120 int get_kernel_revision()
123 int major = 0, minor = 0, patch = 0;
125 if (uname(&name) == -1) {
126 perror("cannot get system information");
129 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
130 return major * 65536 + minor * 256 + patch;
132 #endif /* BB_INIT || BB_PS */
134 #if defined (BB_CP_MV) || defined (BB_DU)
136 #define HASH_SIZE 311 /* Should be prime */
137 #define hash_inode(i) ((i) % HASH_SIZE)
139 static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
142 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
143 * `ino_dev_hashtable', else return 0
145 * If NAME is a non-NULL pointer to a character pointer, and there is
146 * a match, then set *NAME to the value of the name slot in that
149 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
151 ino_dev_hashtable_bucket_t *bucket;
153 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
154 while (bucket != NULL) {
155 if ((bucket->ino == statbuf->st_ino) &&
156 (bucket->dev == statbuf->st_dev))
158 if (name) *name = bucket->name;
161 bucket = bucket->next;
166 /* Add statbuf to statbuf hash table */
167 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
171 ino_dev_hashtable_bucket_t *bucket;
173 i = hash_inode(statbuf->st_ino);
174 s = name ? strlen(name) : 0;
175 bucket = malloc(sizeof(ino_dev_hashtable_bucket_t) + s);
177 fatalError("Not enough memory.");
178 bucket->ino = statbuf->st_ino;
179 bucket->dev = statbuf->st_dev;
181 strcpy(bucket->name, name);
183 bucket->name[0] = '\0';
184 bucket->next = ino_dev_hashtable[i];
185 ino_dev_hashtable[i] = bucket;
188 /* Clear statbuf hash table */
189 void reset_ino_dev_hashtable(void)
192 ino_dev_hashtable_bucket_t *bucket;
194 for (i = 0; i < HASH_SIZE; i++) {
195 while (ino_dev_hashtable[i] != NULL) {
196 bucket = ino_dev_hashtable[i]->next;
197 free(ino_dev_hashtable[i]);
198 ino_dev_hashtable[i] = bucket;
203 #endif /* BB_CP_MV || BB_DU */
205 #if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
207 * Return TRUE if a fileName is a directory.
208 * Nonexistant files return FALSE.
210 int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
215 if (statBuf == NULL) {
216 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
220 if (followLinks == TRUE)
221 status = stat(fileName, statBuf);
223 status = lstat(fileName, statBuf);
225 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
238 #if defined (BB_CP_MV)
240 * Copy one file to another, while possibly preserving its modes, times,
241 * and modes. Returns TRUE if successful, or FALSE on a failure with an
242 * error message output. (Failure is not indicated if the attributes cannot
247 copyFile(const char *srcName, const char *destName,
248 int setModes, int followLinks)
255 struct stat srcStatBuf;
256 struct stat dstStatBuf;
257 struct utimbuf times;
259 if (followLinks == TRUE)
260 status = stat(srcName, &srcStatBuf);
262 status = lstat(srcName, &srcStatBuf);
269 if (followLinks == TRUE)
270 status = stat(destName, &dstStatBuf);
272 status = lstat(destName, &dstStatBuf);
275 dstStatBuf.st_ino = -1;
276 dstStatBuf.st_dev = -1;
279 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
280 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
281 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
285 if (S_ISDIR(srcStatBuf.st_mode)) {
286 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
287 /* Make sure the directory is writable */
288 status = mkdir(destName, 0777777 ^ umask(0));
289 if (status < 0 && errno != EEXIST) {
293 } else if (S_ISLNK(srcStatBuf.st_mode)) {
294 char link_val[PATH_MAX + 1];
297 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
298 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
299 link_size = readlink(srcName, &link_val[0], PATH_MAX);
304 link_val[link_size] = '\0';
305 status = symlink(link_val, destName);
310 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
311 if (setModes == TRUE) {
312 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
319 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
320 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
321 if (mkfifo(destName, 0644) < 0) {
325 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
326 || S_ISSOCK(srcStatBuf.st_mode)) {
327 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
328 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
332 } else if (S_ISREG(srcStatBuf.st_mode)) {
333 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
334 rfd = open(srcName, O_RDONLY);
341 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
349 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
350 if (fullWrite(wfd, buf, rcc) < 0)
358 if (close(wfd) < 0) {
363 if (setModes == TRUE) {
364 /* This is fine, since symlinks never get here */
365 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
369 if (chmod(destName, srcStatBuf.st_mode) < 0) {
373 times.actime = srcStatBuf.st_atime;
374 times.modtime = srcStatBuf.st_mtime;
375 if (utime(destName, ×) < 0) {
390 #endif /* BB_CP_MV */
394 #if defined BB_TAR || defined BB_LS
396 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
397 #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
399 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
400 static const mode_t SBIT[] = {
406 /* The 9 mode bits to test */
407 static const mode_t MBIT[] = {
408 S_IRUSR, S_IWUSR, S_IXUSR,
409 S_IRGRP, S_IWGRP, S_IXGRP,
410 S_IROTH, S_IWOTH, S_IXOTH
413 #define MODE1 "rwxrwxrwx"
414 #define MODE0 "---------"
415 #define SMODE1 "..s..s..t"
416 #define SMODE0 "..S..S..T"
419 * Return the standard ls-like mode string from a file mode.
420 * This is static and so is overwritten on each call.
422 const char *modeString(int mode)
428 buf[0] = TYPECHAR(mode);
429 for (i = 0; i < 9; i++) {
431 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
433 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
437 #endif /* BB_TAR || BB_LS */
442 * Return the standard ls-like time string from a time_t
443 * This is static and so is overwritten on each call.
445 const char *timeString(time_t timeVal)
453 str = ctime(&timeVal);
455 strcpy(buf, &str[4]);
458 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
459 strcpy(&buf[7], &str[20]);
467 #if defined BB_TAR || defined BB_CP_MV
469 * Write all of the supplied buffer out to a file.
470 * This does multiple writes as necessary.
471 * Returns the amount written, or -1 on an error.
473 int fullWrite(int fd, const char *buf, int len)
481 cc = write(fd, buf, len);
493 #endif /* BB_TAR || BB_CP_MV */
496 #if defined BB_TAR || defined BB_TAIL
498 * Read all of the supplied buffer from a file.
499 * This does multiple reads as necessary.
500 * Returns the amount read, or -1 on an error.
501 * A short read is returned on an end of file.
503 int fullRead(int fd, char *buf, int len)
511 cc = read(fd, buf, len);
526 #endif /* BB_TAR || BB_TAIL */
529 #if defined (BB_CHMOD_CHOWN_CHGRP) \
530 || defined (BB_CP_MV) \
531 || defined (BB_FIND) \
533 || defined (BB_INSMOD)
535 * Walk down all the directories under the specified
536 * location, and do something (something specified
537 * by the fileAction and dirAction function pointers).
539 * Unfortunatly, while nftw(3) could replace this and reduce
540 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
541 * and so isn't sufficiently portable to take over since glibc2.1
542 * is so stinking huge.
544 int recursiveAction(const char *fileName,
545 int recurse, int followLinks, int depthFirst,
546 int (*fileAction) (const char *fileName,
547 struct stat * statbuf),
548 int (*dirAction) (const char *fileName,
549 struct stat * statbuf))
555 if (followLinks == TRUE)
556 status = stat(fileName, &statbuf);
558 status = lstat(fileName, &statbuf);
561 #ifdef BB_DEBUG_PRINT_SCAFFOLD
563 "status=%d followLinks=%d TRUE=%d\n",
564 status, followLinks, TRUE);
570 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
571 if (fileAction == NULL)
574 return fileAction(fileName, &statbuf);
577 if (recurse == FALSE) {
578 if (S_ISDIR(statbuf.st_mode)) {
579 if (dirAction != NULL)
580 return (dirAction(fileName, &statbuf));
586 if (S_ISDIR(statbuf.st_mode)) {
589 dir = opendir(fileName);
594 if (dirAction != NULL && depthFirst == FALSE) {
595 status = dirAction(fileName, &statbuf);
596 if (status == FALSE) {
601 while ((next = readdir(dir)) != NULL) {
602 char nextFile[PATH_MAX + 1];
604 if ((strcmp(next->d_name, "..") == 0)
605 || (strcmp(next->d_name, ".") == 0)) {
608 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
609 fprintf(stderr, name_too_long, "ftw");
612 sprintf(nextFile, "%s/%s", fileName, next->d_name);
614 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
615 fileAction, dirAction);
621 status = closedir(dir);
626 if (dirAction != NULL && depthFirst == TRUE) {
627 status = dirAction(fileName, &statbuf);
628 if (status == FALSE) {
634 if (fileAction == NULL)
637 return fileAction(fileName, &statbuf);
642 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
646 #if defined (BB_TAR) || defined (BB_MKDIR)
648 * Attempt to create the directories along the specified path, except for
649 * the final component. The mode is given for the final directory only,
650 * while all previous ones get default protections. Errors are not reported
651 * here, as failures to restore files can be reported later.
653 extern int createPath(const char *name, int mode)
657 char buf[PATH_MAX + 1];
661 for (cp = buf; *cp == '/'; cp++);
662 cp = strchr(cp, '/');
665 cp = strchr(cp + 1, '/');
667 retVal = mkdir(buf, cp ? 0777 : mode);
668 if (retVal != 0 && errno != EEXIST) {
676 #endif /* BB_TAR || BB_MKDIR */
680 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
681 /* [ugoa]{+|-|=}[rwxst] */
685 extern int parse_mode(const char *s, mode_t * theMode)
689 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
702 groups |= S_ISUID | S_IRWXU;
705 groups |= S_ISGID | S_IRWXG;
711 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
717 if (groups == 0) /* The default is "all" */
719 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
722 if (isdigit(c) && c >= '0' && c <= '7' &&
723 mode == 0 && groups == 0) {
724 *theMode = strtol(--s, NULL, 8);
732 while ((c = *s++) != '\0') {
737 mode |= S_IRUSR | S_IRGRP | S_IROTH;
740 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
743 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
746 mode |= S_IXGRP | S_ISUID | S_ISGID;
760 andMode &= ~(groups);
763 orMode |= mode & groups;
766 andMode &= ~(mode & groups);
777 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
785 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
787 /* Use this to avoid needing the glibc NSS stuff
788 * This uses storage buf to hold things.
790 uid_t my_getid(const char *filename, char *name, uid_t id)
793 char *rname, *start, *end, buf[128];
796 file = fopen(filename, "r");
802 while (fgets(buf, 128, file) != NULL) {
807 end = strchr(start, ':');
814 end = strchr(start, ':');
819 rid = (uid_t) strtol(start, &end, 10);
824 if (0 == strcmp(rname, name)) {
829 if (id != -1 && id == rid) {
830 strncpy(name, rname, 8);
839 uid_t my_getpwnam(char *name)
841 return my_getid("/etc/passwd", name, -1);
844 gid_t my_getgrnam(char *name)
846 return my_getid("/etc/group", name, -1);
849 void my_getpwuid(char *name, uid_t uid)
851 my_getid("/etc/passwd", name, uid);
854 void my_getgrgid(char *group, gid_t gid)
856 my_getid("/etc/group", group, gid);
860 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
865 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
868 #include <linux/kd.h>
869 #include <sys/ioctl.h>
871 int is_a_console(int fd)
876 return (ioctl(fd, KDGKBTYPE, &arg) == 0
877 && ((arg == KB_101) || (arg == KB_84)));
880 static int open_a_console(char *fnam)
885 fd = open(fnam, O_RDWR);
887 /* if failed, try read-only */
888 if (fd < 0 && errno == EACCES)
889 fd = open(fnam, O_RDONLY);
891 /* if failed, try write-only */
892 if (fd < 0 && errno == EACCES)
893 fd = open(fnam, O_WRONLY);
895 /* if failed, fail */
899 /* if not a console, fail */
900 if (!is_a_console(fd)) {
910 * Get an fd for use with kbd/console ioctls.
911 * We try several things because opening /dev/console will fail
912 * if someone else used X (which does a chown on /dev/console).
914 * if tty_name is non-NULL, try this one instead.
917 int get_console_fd(char *tty_name)
922 if (-1 == (fd = open_a_console(tty_name)))
928 fd = open_a_console("/dev/tty");
932 fd = open_a_console("/dev/tty0");
936 fd = open_a_console("/dev/console");
940 for (fd = 0; fd < 3; fd++)
941 if (is_a_console(fd))
945 "Couldnt get a file descriptor referring to the console\n");
946 return -1; /* total failure */
950 #endif /* BB_CHVT || BB_DEALLOCVT */
953 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
955 /* Do a case insensitive strstr() */
956 char *stristr(char *haystack, const char *needle)
958 int len = strlen(needle);
961 if (!strncasecmp(haystack, needle, len))
972 /* This tries to find a needle in a haystack, but does so by
973 * only trying to match literal strings (look 'ma, no regexps!)
974 * This is short, sweet, and carries _very_ little baggage,
975 * unlike its beefier cousin in regexp.c
978 extern int find_match(char *haystack, char *needle, int ignoreCase)
981 if (ignoreCase == FALSE)
982 haystack = strstr(haystack, needle);
984 haystack = stristr(haystack, needle);
985 if (haystack == NULL)
991 /* This performs substitutions after a string match has been found. */
992 extern int replace_match(char *haystack, char *needle, char *newNeedle,
996 char *where, *slider, *slider1, *oldhayStack;
998 if (ignoreCase == FALSE)
999 where = strstr(haystack, needle);
1001 where = stristr(haystack, needle);
1003 if (strcmp(needle, newNeedle) == 0)
1006 oldhayStack = (char *) malloc((unsigned) (strlen(haystack)));
1007 while (where != NULL) {
1009 strcpy(oldhayStack, haystack);
1011 if (strlen(newNeedle) > strlen(needle)) {
1013 (char *) realloc(haystack,
1014 (unsigned) (strlen(haystack) -
1016 strlen(newNeedle)));
1019 for (slider = haystack, slider1 = oldhayStack; slider != where;
1020 slider++, slider1++);
1022 haystack = strcat(haystack, newNeedle);
1023 slider1 += strlen(needle);
1024 haystack = strcat(haystack, slider1);
1025 where = strstr(slider, needle);
1035 #endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
1040 * Routine to see if a text string is matched by a wildcard pattern.
1041 * Returns TRUE if the text is matched, or FALSE if it is not matched
1042 * or if the pattern is invalid.
1043 * * matches zero or more characters
1044 * ? matches a single character
1045 * [abc] matches 'a', 'b' or 'c'
1046 * \c quotes character c
1047 * Adapted from code written by Ingo Wilken, and
1048 * then taken from sash, Copyright (c) 1999 by David I. Bell
1049 * Permission is granted to use, distribute, or modify this source,
1050 * provided that this copyright notice remains intact.
1051 * Permission to distribute this code under the GPL has been granted.
1053 extern int check_wildcard_match(const char *text, const char *pattern)
1055 const char *retryPat;
1056 const char *retryText;
1063 while (*text || *pattern) {
1075 while ((ch = *pattern++) != ']') {
1087 if (found == TRUE) {
1092 /* fall into next case */
1095 if (*text++ == '\0')
1106 /* fall into next case */
1124 if (pattern == NULL)
1130 #endif /* BB_FIND */
1135 #if defined BB_DF || defined BB_MTAB
1137 * Given a block device, find the mount table entry if that block device
1140 * Given any other file (or directory), find the mount table entry for its
1143 extern struct mntent *findMountPoint(const char *name, const char *table)
1148 struct mntent *mountEntry;
1150 if (stat(name, &s) != 0)
1153 if ((s.st_mode & S_IFMT) == S_IFBLK)
1154 mountDevice = s.st_rdev;
1156 mountDevice = s.st_dev;
1159 if ((mountTable = setmntent(table, "r")) == 0)
1162 while ((mountEntry = getmntent(mountTable)) != 0) {
1163 if (strcmp(name, mountEntry->mnt_dir) == 0
1164 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1166 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1168 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1171 endmntent(mountTable);
1174 #endif /* BB_DF || BB_MTAB */
1178 #if defined BB_DD || defined BB_TAIL
1180 * Read a number with a possible multiplier.
1181 * Returns -1 if the number format is illegal.
1183 extern long getNum(const char *cp)
1187 if (!isDecimal(*cp))
1192 while (isDecimal(*cp))
1193 value = value * 10 + *cp++ - '0';
1197 case 'm': /* `tail' uses it traditionally */
1225 #endif /* BB_DD || BB_TAIL */
1228 #if defined BB_INIT || defined BB_SYSLOGD
1229 /* try to open up the specified device */
1230 extern int device_open(char *device, int mode)
1234 m = mode | O_NONBLOCK;
1236 /* Retry up to 5 times */
1237 for (f = 0; f < 5; f++)
1238 if ((fd = open(device, m, 0600)) >= 0)
1242 /* Reset original flags. */
1244 fcntl(fd, F_SETFL, mode);
1247 #endif /* BB_INIT BB_SYSLOGD */
1250 #if defined BB_KILLALL || defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF )
1252 #ifdef BB_FEATURE_USE_DEVPS_PATCH
1253 #include <linux/devps.h>
1256 #if defined BB_FEATURE_USE_DEVPS_PATCH
1259 * This finds the pid of the specified process,
1260 * by using the /dev/ps device driver.
1264 * pid when the pid is found.
1266 extern pid_t findPidByName( char* pidName)
1269 char device[] = "/dev/ps";
1272 pid_t* pid_array = NULL;
1275 fd = open(device, O_RDONLY);
1277 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1279 /* Find out how many processes there are */
1280 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1281 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1283 /* Allocate some memory -- grab a few extras just in case
1284 * some new processes start up while we wait. The kernel will
1285 * just ignore any extras if we give it too many, and will trunc.
1286 * the list if we give it too few. */
1287 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1288 pid_array[0] = num_pids+10;
1290 /* Now grab the pid list */
1291 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1292 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1294 /* Now search for a match */
1295 for (i=1; i<pid_array[0] ; i++) {
1296 struct pid_info info;
1298 info.pid = pid_array[i];
1299 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1300 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1302 if ((strstr(info.command_line, pidName) != NULL)) {
1312 if (close (fd) != 0)
1313 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1317 #else /* BB_FEATURE_USE_DEVPS_PATCH */
1318 #if ! defined BB_FEATURE_USE_PROCFS
1319 #error Sorry, I depend on the /proc filesystem right now.
1324 * This finds the pid of the specified process.
1325 * Currently, it's implemented by rummaging through
1326 * the proc filesystem.
1330 * pid when the pid is found.
1332 extern pid_t findPidByName( char* pidName)
1335 struct dirent *next;
1337 dir = opendir("/proc");
1339 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1341 while ((next = readdir(dir)) != NULL) {
1346 /* If it isn't a number, we don't want it */
1347 if (!isdigit(*next->d_name))
1350 /* Now open the command line file */
1351 sprintf(filename, "/proc/%s/status", next->d_name);
1352 status = fopen(filename, "r");
1356 fgets(buffer, 256, status);
1359 if ((strstr(buffer, pidName) != NULL)) {
1360 return strtol(next->d_name, NULL, 0);
1365 #endif /* BB_FEATURE_USE_DEVPS_PATCH */
1366 #endif /* BB_INIT || BB_HALT || BB_REBOOT || BB_KILLALL || BB_POWEROFF */
1368 #if defined BB_GUNZIP \
1369 || defined BB_GZIP \
1370 || defined BB_PRINTF \
1372 extern void *xmalloc(size_t size)
1374 void *cp = malloc(size);
1377 errorMsg("out of memory");
1382 #endif /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
1384 #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1385 extern int vdprintf(int d, const char *format, va_list ap)
1390 len = vsprintf(buf, format, ap);
1391 return write(d, buf, len);
1393 #endif /* BB_SYSLOGD */
1395 #if defined BB_FEATURE_MOUNT_LOOP
1396 extern int del_loop(const char *device)
1400 if ((fd = open(device, O_RDONLY)) < 0) {
1404 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1405 perror("ioctl: LOOP_CLR_FD");
1412 extern int set_loop(const char *device, const char *file, int offset,
1415 struct loop_info loopinfo;
1418 mode = *loopro ? O_RDONLY : O_RDWR;
1419 if ((ffd = open(file, mode)) < 0 && !*loopro
1420 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1424 if ((fd = open(device, mode)) < 0) {
1429 *loopro = (mode == O_RDONLY);
1431 memset(&loopinfo, 0, sizeof(loopinfo));
1432 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1433 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
1435 loopinfo.lo_offset = offset;
1437 loopinfo.lo_encrypt_key_size = 0;
1438 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1439 perror("ioctl: LOOP_SET_FD");
1444 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1445 (void) ioctl(fd, LOOP_CLR_FD, 0);
1446 perror("ioctl: LOOP_SET_STATUS");
1456 extern char *find_unused_loop_device(void)
1460 struct stat statbuf;
1461 struct loop_info loopinfo;
1463 for (i = 0; i <= 7; i++) {
1464 sprintf(dev, "/dev/loop%d", i);
1465 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1466 if ((fd = open(dev, O_RDONLY)) >= 0) {
1467 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1468 if (errno == ENXIO) { /* probably free */
1479 #endif /* BB_FEATURE_MOUNT_LOOP */
1482 #define whine_if_fstab_is_missing() {}
1484 extern void whine_if_fstab_is_missing()
1486 struct stat statBuf;
1488 if (stat("/etc/fstab", &statBuf) < 0)
1490 "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1497 c-file-style: "linux"