Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 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. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | * |
| 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 Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 28 | #include "internal.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 29 | #if defined (BB_CHMOD_CHOWN_CHGRP) \ |
| 30 | || defined (BB_CP_MV) \ |
| 31 | || defined (BB_FIND) \ |
| 32 | || defined (BB_LS) \ |
| 33 | || defined (BB_INSMOD) |
| 34 | /* same conditions as recursiveAction */ |
| 35 | #define bb_need_name_too_long |
| 36 | #endif |
| 37 | #define BB_DECLARE_EXTERN |
| 38 | #include "messages.c" |
| 39 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | #include <errno.h> |
| 43 | #include <fcntl.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 44 | #include <dirent.h> |
| 45 | #include <time.h> |
| 46 | #include <utime.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 47 | #include <sys/stat.h> |
| 48 | #include <unistd.h> |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 49 | #include <ctype.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | #include <sys/param.h> /* for PATH_MAX */ |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 51 | #include <sys/utsname.h> /* for uname(2) */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 53 | #if defined BB_FEATURE_MOUNT_LOOP |
| 54 | #include <fcntl.h> |
| 55 | #include <sys/ioctl.h> |
| 56 | #include <linux/loop.h> |
| 57 | #endif |
| 58 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 59 | /* Busybox mount uses either /proc/filesystems or /dev/mtab to get the |
| 60 | * list of available filesystems used for the -t auto option */ |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 61 | #if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 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." |
| 64 | #endif |
| 65 | |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 66 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 67 | #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 68 | # if defined BB_FEATURE_USE_PROCFS |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 69 | const char mtab_file[] = "/proc/mounts"; |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 70 | # else |
| 71 | # if defined BB_MTAB |
| 72 | const char mtab_file[] = "/etc/mtab"; |
| 73 | # else |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 74 | # if defined BB_FEATURE_USE_DEVPS_PATCH |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 75 | const char mtab_file[] = "/dev/mtab"; |
| 76 | # else |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 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) |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 78 | # endif |
| 79 | # endif |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 80 | # endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 84 | extern void usage(const char *usage) |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 85 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", |
| 87 | BB_VER, BB_BT); |
| 88 | fprintf(stderr, "Usage: %s\n", usage); |
| 89 | exit FALSE; |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 92 | extern void errorMsg(const char *s, ...) |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 93 | { |
| 94 | va_list p; |
| 95 | |
| 96 | va_start(p, s); |
| 97 | fflush(stdout); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 98 | vfprintf(stderr, s, p); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 99 | va_end(p); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 100 | fflush(stderr); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 103 | extern void fatalError(const char *s, ...) |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 104 | { |
| 105 | va_list p; |
| 106 | |
| 107 | va_start(p, s); |
| 108 | fflush(stdout); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 109 | vfprintf(stderr, s, p); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 110 | va_end(p); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 111 | fflush(stderr); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 112 | exit( FALSE); |
| 113 | } |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 114 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 115 | #if defined BB_INIT |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 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: |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 118 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 119 | */ |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 120 | extern int get_kernel_revision(void) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 121 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 122 | struct utsname name; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | int major = 0, minor = 0, patch = 0; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 124 | |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 125 | if (uname(&name) == -1) { |
| 126 | perror("cannot get system information"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 127 | return (0); |
| 128 | } |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 129 | sscanf(name.version, "%d.%d.%d", &major, &minor, &patch); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 130 | return major * 65536 + minor * 256 + patch; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 131 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 132 | #endif /* BB_INIT */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 133 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 134 | #if defined (BB_CP_MV) || defined (BB_DU) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 135 | |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 136 | #define HASH_SIZE 311 /* Should be prime */ |
| 137 | #define hash_inode(i) ((i) % HASH_SIZE) |
| 138 | |
| 139 | static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE]; |
| 140 | |
| 141 | /* |
| 142 | * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in |
| 143 | * `ino_dev_hashtable', else return 0 |
| 144 | * |
| 145 | * If NAME is a non-NULL pointer to a character pointer, and there is |
| 146 | * a match, then set *NAME to the value of the name slot in that |
| 147 | * bucket. |
| 148 | */ |
| 149 | int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name) |
| 150 | { |
| 151 | ino_dev_hashtable_bucket_t *bucket; |
| 152 | |
| 153 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; |
| 154 | while (bucket != NULL) { |
| 155 | if ((bucket->ino == statbuf->st_ino) && |
| 156 | (bucket->dev == statbuf->st_dev)) |
| 157 | { |
| 158 | if (name) *name = bucket->name; |
| 159 | return 1; |
| 160 | } |
| 161 | bucket = bucket->next; |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* Add statbuf to statbuf hash table */ |
| 167 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) |
| 168 | { |
| 169 | int i; |
| 170 | size_t s; |
| 171 | ino_dev_hashtable_bucket_t *bucket; |
| 172 | |
| 173 | i = hash_inode(statbuf->st_ino); |
| 174 | s = name ? strlen(name) : 0; |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 175 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 176 | bucket->ino = statbuf->st_ino; |
| 177 | bucket->dev = statbuf->st_dev; |
| 178 | if (name) |
| 179 | strcpy(bucket->name, name); |
| 180 | else |
| 181 | bucket->name[0] = '\0'; |
| 182 | bucket->next = ino_dev_hashtable[i]; |
| 183 | ino_dev_hashtable[i] = bucket; |
| 184 | } |
| 185 | |
| 186 | /* Clear statbuf hash table */ |
| 187 | void reset_ino_dev_hashtable(void) |
| 188 | { |
| 189 | int i; |
| 190 | ino_dev_hashtable_bucket_t *bucket; |
| 191 | |
| 192 | for (i = 0; i < HASH_SIZE; i++) { |
| 193 | while (ino_dev_hashtable[i] != NULL) { |
| 194 | bucket = ino_dev_hashtable[i]->next; |
| 195 | free(ino_dev_hashtable[i]); |
| 196 | ino_dev_hashtable[i] = bucket; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | #endif /* BB_CP_MV || BB_DU */ |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 202 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 203 | #if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 204 | /* |
| 205 | * Return TRUE if a fileName is a directory. |
| 206 | * Nonexistant files return FALSE. |
| 207 | */ |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 208 | int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 209 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 210 | int status; |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 211 | int didMalloc = 0; |
| 212 | |
| 213 | if (statBuf == NULL) { |
| 214 | statBuf = (struct stat *)xmalloc(sizeof(struct stat)); |
| 215 | ++didMalloc; |
| 216 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 217 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 218 | if (followLinks == TRUE) |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 219 | status = stat(fileName, statBuf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | else |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 221 | status = lstat(fileName, statBuf); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 222 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 223 | if (status < 0 || !(S_ISDIR(statBuf->st_mode))) { |
| 224 | status = FALSE; |
| 225 | } |
| 226 | else status = TRUE; |
| 227 | |
| 228 | if (didMalloc) { |
| 229 | free(statBuf); |
| 230 | statBuf = NULL; |
| 231 | } |
| 232 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 233 | } |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 234 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 235 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 236 | #if defined (BB_CP_MV) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 237 | /* |
| 238 | * Copy one file to another, while possibly preserving its modes, times, |
| 239 | * and modes. Returns TRUE if successful, or FALSE on a failure with an |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 240 | * error message output. (Failure is not indicated if the attributes cannot |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 241 | * be set.) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 242 | * -Erik Andersen |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 243 | */ |
| 244 | int |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 245 | copyFile(const char *srcName, const char *destName, |
| 246 | int setModes, int followLinks) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 247 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 248 | int rfd; |
| 249 | int wfd; |
| 250 | int rcc; |
| 251 | int status; |
| 252 | char buf[BUF_SIZE]; |
| 253 | struct stat srcStatBuf; |
| 254 | struct stat dstStatBuf; |
| 255 | struct utimbuf times; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 256 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | if (followLinks == TRUE) |
| 258 | status = stat(srcName, &srcStatBuf); |
| 259 | else |
| 260 | status = lstat(srcName, &srcStatBuf); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 261 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 262 | if (status < 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | perror(srcName); |
| 264 | return FALSE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 265 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 266 | |
| 267 | if (followLinks == TRUE) |
| 268 | status = stat(destName, &dstStatBuf); |
| 269 | else |
| 270 | status = lstat(destName, &dstStatBuf); |
| 271 | |
| 272 | if (status < 0) { |
| 273 | dstStatBuf.st_ino = -1; |
| 274 | dstStatBuf.st_dev = -1; |
| 275 | } |
| 276 | |
| 277 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && |
| 278 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { |
| 279 | fprintf(stderr, "Copying file \"%s\" to itself\n", srcName); |
| 280 | return FALSE; |
| 281 | } |
| 282 | |
| 283 | if (S_ISDIR(srcStatBuf.st_mode)) { |
| 284 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); |
| 285 | /* Make sure the directory is writable */ |
| 286 | status = mkdir(destName, 0777777 ^ umask(0)); |
| 287 | if (status < 0 && errno != EEXIST) { |
| 288 | perror(destName); |
| 289 | return FALSE; |
| 290 | } |
| 291 | } else if (S_ISLNK(srcStatBuf.st_mode)) { |
| 292 | char link_val[PATH_MAX + 1]; |
| 293 | int link_size; |
| 294 | |
| 295 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); |
| 296 | /* Warning: This could possibly truncate silently, to PATH_MAX chars */ |
| 297 | link_size = readlink(srcName, &link_val[0], PATH_MAX); |
| 298 | if (link_size < 0) { |
| 299 | perror(srcName); |
| 300 | return FALSE; |
| 301 | } |
| 302 | link_val[link_size] = '\0'; |
| 303 | status = symlink(link_val, destName); |
| 304 | if (status < 0) { |
| 305 | perror(destName); |
| 306 | return FALSE; |
| 307 | } |
Erik Andersen | 8e759aa | 2000-01-29 05:52:40 +0000 | [diff] [blame] | 308 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 309 | if (setModes == TRUE) { |
| 310 | if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) { |
| 311 | perror(destName); |
| 312 | return FALSE; |
| 313 | } |
| 314 | } |
Erik Andersen | 8e759aa | 2000-01-29 05:52:40 +0000 | [diff] [blame] | 315 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 316 | return TRUE; |
| 317 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { |
| 318 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); |
| 319 | if (mkfifo(destName, 0644) < 0) { |
| 320 | perror(destName); |
| 321 | return FALSE; |
| 322 | } |
| 323 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) |
| 324 | || S_ISSOCK(srcStatBuf.st_mode)) { |
| 325 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); |
| 326 | if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) { |
| 327 | perror(destName); |
| 328 | return FALSE; |
| 329 | } |
| 330 | } else if (S_ISREG(srcStatBuf.st_mode)) { |
| 331 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); |
| 332 | rfd = open(srcName, O_RDONLY); |
| 333 | if (rfd < 0) { |
| 334 | perror(srcName); |
| 335 | return FALSE; |
| 336 | } |
| 337 | |
| 338 | wfd = |
| 339 | open(destName, O_WRONLY | O_CREAT | O_TRUNC, |
| 340 | srcStatBuf.st_mode); |
| 341 | if (wfd < 0) { |
| 342 | perror(destName); |
| 343 | close(rfd); |
| 344 | return FALSE; |
| 345 | } |
| 346 | |
| 347 | while ((rcc = read(rfd, buf, sizeof(buf))) > 0) { |
| 348 | if (fullWrite(wfd, buf, rcc) < 0) |
| 349 | goto error_exit; |
| 350 | } |
| 351 | if (rcc < 0) { |
| 352 | goto error_exit; |
| 353 | } |
| 354 | |
| 355 | close(rfd); |
| 356 | if (close(wfd) < 0) { |
| 357 | return FALSE; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (setModes == TRUE) { |
| 362 | /* This is fine, since symlinks never get here */ |
| 363 | if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) { |
| 364 | perror(destName); |
| 365 | exit FALSE; |
| 366 | } |
| 367 | if (chmod(destName, srcStatBuf.st_mode) < 0) { |
| 368 | perror(destName); |
| 369 | exit FALSE; |
| 370 | } |
| 371 | times.actime = srcStatBuf.st_atime; |
| 372 | times.modtime = srcStatBuf.st_mtime; |
| 373 | if (utime(destName, ×) < 0) { |
| 374 | perror(destName); |
| 375 | exit FALSE; |
| 376 | } |
| 377 | } |
| 378 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 379 | return TRUE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 380 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 381 | error_exit: |
| 382 | perror(destName); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 383 | close(rfd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 384 | close(wfd); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 385 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | return FALSE; |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 387 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | #endif /* BB_CP_MV */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | |
| 390 | |
| 391 | |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 392 | #if defined BB_TAR || defined BB_LS |
| 393 | |
| 394 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) |
| 395 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) |
| 396 | |
| 397 | /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ |
| 398 | static const mode_t SBIT[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 399 | 0, 0, S_ISUID, |
| 400 | 0, 0, S_ISGID, |
| 401 | 0, 0, S_ISVTX |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | /* The 9 mode bits to test */ |
| 405 | static const mode_t MBIT[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 406 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 407 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 408 | S_IROTH, S_IWOTH, S_IXOTH |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | #define MODE1 "rwxrwxrwx" |
| 412 | #define MODE0 "---------" |
| 413 | #define SMODE1 "..s..s..t" |
| 414 | #define SMODE0 "..S..S..T" |
| 415 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 416 | /* |
| 417 | * Return the standard ls-like mode string from a file mode. |
| 418 | * This is static and so is overwritten on each call. |
| 419 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 420 | const char *modeString(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 421 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 422 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 423 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 424 | int i; |
| 425 | |
| 426 | buf[0] = TYPECHAR(mode); |
| 427 | for (i = 0; i < 9; i++) { |
| 428 | if (mode & SBIT[i]) |
| 429 | buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i]; |
| 430 | else |
| 431 | buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i]; |
| 432 | } |
| 433 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 434 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 435 | #endif /* BB_TAR || BB_LS */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 436 | |
| 437 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 438 | #if defined BB_TAR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 439 | /* |
Eric Andersen | 3d8dbe1 | 1999-11-09 01:51:02 +0000 | [diff] [blame] | 440 | * Return the standard ls-like time string from a time_t |
| 441 | * This is static and so is overwritten on each call. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 442 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 443 | const char *timeString(time_t timeVal) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 444 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 445 | time_t now; |
| 446 | char *str; |
| 447 | static char buf[26]; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 448 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 449 | time(&now); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 450 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 451 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 452 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 453 | strcpy(buf, &str[4]); |
| 454 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 455 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 456 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 457 | strcpy(&buf[7], &str[20]); |
| 458 | buf[11] = '\0'; |
| 459 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 460 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 461 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 462 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 463 | #endif /* BB_TAR */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 464 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 465 | #if defined BB_TAR || defined BB_CP_MV |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 466 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 467 | * Write all of the supplied buffer out to a file. |
| 468 | * This does multiple writes as necessary. |
| 469 | * Returns the amount written, or -1 on an error. |
| 470 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 471 | int fullWrite(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 472 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 473 | int cc; |
| 474 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 475 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 476 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 477 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 478 | while (len > 0) { |
| 479 | cc = write(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 480 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 481 | if (cc < 0) |
| 482 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 483 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 484 | buf += cc; |
| 485 | total += cc; |
| 486 | len -= cc; |
| 487 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 488 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 489 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 490 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 491 | #endif /* BB_TAR || BB_CP_MV */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 492 | |
| 493 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 494 | #if defined BB_TAR || defined BB_TAIL |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 495 | /* |
| 496 | * Read all of the supplied buffer from a file. |
| 497 | * This does multiple reads as necessary. |
| 498 | * Returns the amount read, or -1 on an error. |
| 499 | * A short read is returned on an end of file. |
| 500 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 501 | int fullRead(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 502 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 503 | int cc; |
| 504 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 505 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 506 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 507 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 508 | while (len > 0) { |
| 509 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 510 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 511 | if (cc < 0) |
| 512 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 513 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 514 | if (cc == 0) |
| 515 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 516 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 517 | buf += cc; |
| 518 | total += cc; |
| 519 | len -= cc; |
| 520 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 521 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 522 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 523 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 524 | #endif /* BB_TAR || BB_TAIL */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 525 | |
| 526 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 527 | #if defined (BB_CHMOD_CHOWN_CHGRP) \ |
| 528 | || defined (BB_CP_MV) \ |
| 529 | || defined (BB_FIND) \ |
| 530 | || defined (BB_LS) \ |
| 531 | || defined (BB_INSMOD) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 532 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 533 | * Walk down all the directories under the specified |
| 534 | * location, and do something (something specified |
| 535 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 536 | * |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 537 | * Unfortunatly, while nftw(3) could replace this and reduce |
| 538 | * code size a bit, nftw() wasn't supported before GNU libc 2.1, |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 539 | * and so isn't sufficiently portable to take over since glibc2.1 |
| 540 | * is so stinking huge. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 541 | */ |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 542 | int recursiveAction(const char *fileName, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 543 | int recurse, int followLinks, int depthFirst, |
| 544 | int (*fileAction) (const char *fileName, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 545 | struct stat * statbuf, |
| 546 | void* userData), |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 547 | int (*dirAction) (const char *fileName, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 548 | struct stat * statbuf, |
| 549 | void* userData), |
| 550 | void* userData) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 551 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 552 | int status; |
| 553 | struct stat statbuf; |
| 554 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 555 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 556 | if (followLinks == TRUE) |
| 557 | status = stat(fileName, &statbuf); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 558 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 559 | status = lstat(fileName, &statbuf); |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 560 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 561 | if (status < 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 562 | #ifdef BB_DEBUG_PRINT_SCAFFOLD |
| 563 | fprintf(stderr, |
| 564 | "status=%d followLinks=%d TRUE=%d\n", |
| 565 | status, followLinks, TRUE); |
| 566 | #endif |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 567 | perror(fileName); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 568 | return FALSE; |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 569 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 570 | |
| 571 | if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) { |
| 572 | if (fileAction == NULL) |
| 573 | return TRUE; |
| 574 | else |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 575 | return fileAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | if (recurse == FALSE) { |
| 579 | if (S_ISDIR(statbuf.st_mode)) { |
| 580 | if (dirAction != NULL) |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 581 | return (dirAction(fileName, &statbuf, userData)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 582 | else |
| 583 | return TRUE; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (S_ISDIR(statbuf.st_mode)) { |
| 588 | DIR *dir; |
| 589 | |
| 590 | dir = opendir(fileName); |
| 591 | if (!dir) { |
| 592 | perror(fileName); |
| 593 | return FALSE; |
| 594 | } |
| 595 | if (dirAction != NULL && depthFirst == FALSE) { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 596 | status = dirAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 597 | if (status == FALSE) { |
| 598 | perror(fileName); |
| 599 | return FALSE; |
| 600 | } |
| 601 | } |
| 602 | while ((next = readdir(dir)) != NULL) { |
| 603 | char nextFile[PATH_MAX + 1]; |
| 604 | |
| 605 | if ((strcmp(next->d_name, "..") == 0) |
| 606 | || (strcmp(next->d_name, ".") == 0)) { |
| 607 | continue; |
| 608 | } |
| 609 | if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) { |
| 610 | fprintf(stderr, name_too_long, "ftw"); |
| 611 | return FALSE; |
| 612 | } |
| 613 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 614 | status = |
| 615 | recursiveAction(nextFile, TRUE, followLinks, depthFirst, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 616 | fileAction, dirAction, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 617 | if (status < 0) { |
| 618 | closedir(dir); |
| 619 | return FALSE; |
| 620 | } |
| 621 | } |
| 622 | status = closedir(dir); |
| 623 | if (status < 0) { |
| 624 | perror(fileName); |
| 625 | return FALSE; |
| 626 | } |
| 627 | if (dirAction != NULL && depthFirst == TRUE) { |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 628 | status = dirAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 629 | if (status == FALSE) { |
| 630 | perror(fileName); |
| 631 | return FALSE; |
| 632 | } |
| 633 | } |
| 634 | } else { |
| 635 | if (fileAction == NULL) |
| 636 | return TRUE; |
| 637 | else |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 638 | return fileAction(fileName, &statbuf, userData); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 639 | } |
| 640 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 643 | #endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 644 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 645 | |
| 646 | |
Erik Andersen | a899108 | 1999-12-29 03:34:00 +0000 | [diff] [blame] | 647 | #if defined (BB_TAR) || defined (BB_MKDIR) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 648 | /* |
| 649 | * Attempt to create the directories along the specified path, except for |
| 650 | * the final component. The mode is given for the final directory only, |
| 651 | * while all previous ones get default protections. Errors are not reported |
| 652 | * here, as failures to restore files can be reported later. |
| 653 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 654 | extern int createPath(const char *name, int mode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 655 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 656 | char *cp; |
| 657 | char *cpOld; |
| 658 | char buf[PATH_MAX + 1]; |
| 659 | int retVal = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 660 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 661 | strcpy(buf, name); |
| 662 | for (cp = buf; *cp == '/'; cp++); |
| 663 | cp = strchr(cp, '/'); |
| 664 | while (cp) { |
| 665 | cpOld = cp; |
| 666 | cp = strchr(cp + 1, '/'); |
| 667 | *cpOld = '\0'; |
| 668 | retVal = mkdir(buf, cp ? 0777 : mode); |
| 669 | if (retVal != 0 && errno != EEXIST) { |
| 670 | perror(buf); |
| 671 | return FALSE; |
| 672 | } |
| 673 | *cpOld = '/'; |
Erik Andersen | 06936df | 2000-01-23 02:14:20 +0000 | [diff] [blame] | 674 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 675 | return TRUE; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 676 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 677 | #endif /* BB_TAR || BB_MKDIR */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 678 | |
| 679 | |
| 680 | |
| 681 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 682 | /* [ugoa]{+|-|=}[rwxst] */ |
| 683 | |
| 684 | |
| 685 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 686 | extern int parse_mode(const char *s, mode_t * theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 687 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 688 | mode_t andMode = |
| 689 | |
| 690 | S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 691 | mode_t orMode = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 692 | mode_t mode = 0; |
| 693 | mode_t groups = 0; |
| 694 | char type; |
| 695 | char c; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 696 | |
| 697 | do { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 698 | for (;;) { |
| 699 | switch (c = *s++) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 700 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 701 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 702 | case 'u': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 703 | groups |= S_ISUID | S_IRWXU; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 704 | continue; |
| 705 | case 'g': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 706 | groups |= S_ISGID | S_IRWXG; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 707 | continue; |
| 708 | case 'o': |
| 709 | groups |= S_IRWXO; |
| 710 | continue; |
| 711 | case 'a': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 712 | groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 713 | continue; |
| 714 | case '+': |
| 715 | case '=': |
| 716 | case '-': |
| 717 | type = c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 718 | if (groups == 0) /* The default is "all" */ |
| 719 | groups |= |
| 720 | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 721 | break; |
| 722 | default: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 723 | if (isdigit(c) && c >= '0' && c <= '7' && |
| 724 | mode == 0 && groups == 0) { |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 725 | *theMode = strtol(--s, NULL, 8); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 726 | return (TRUE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 727 | } else |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 728 | return (FALSE); |
| 729 | } |
| 730 | break; |
| 731 | } |
| 732 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 733 | while ((c = *s++) != '\0') { |
| 734 | switch (c) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 735 | case ',': |
| 736 | break; |
| 737 | case 'r': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 738 | mode |= S_IRUSR | S_IRGRP | S_IROTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 739 | continue; |
| 740 | case 'w': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 741 | mode |= S_IWUSR | S_IWGRP | S_IWOTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 742 | continue; |
| 743 | case 'x': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 744 | mode |= S_IXUSR | S_IXGRP | S_IXOTH; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 745 | continue; |
| 746 | case 's': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 747 | mode |= S_IXGRP | S_ISUID | S_ISGID; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 748 | continue; |
| 749 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 750 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 751 | continue; |
| 752 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 753 | *theMode &= andMode; |
| 754 | *theMode |= orMode; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 755 | return (TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 756 | } |
| 757 | break; |
| 758 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 759 | switch (type) { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 760 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 761 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 762 | /* fall through */ |
| 763 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 764 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 765 | break; |
| 766 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 767 | andMode &= ~(mode & groups); |
| 768 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 769 | break; |
| 770 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 771 | } while (c == ','); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 772 | *theMode &= andMode; |
| 773 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 774 | return (TRUE); |
| 775 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 776 | |
| 777 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 778 | #endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 779 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 780 | |
| 781 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 782 | |
| 783 | |
| 784 | |
| 785 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 786 | #if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 787 | |
| 788 | /* Use this to avoid needing the glibc NSS stuff |
| 789 | * This uses storage buf to hold things. |
| 790 | * */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 791 | uid_t my_getid(const char *filename, char *name, uid_t id) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 792 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 793 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 794 | char *rname, *start, *end, buf[128]; |
| 795 | uid_t rid; |
| 796 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 797 | file = fopen(filename, "r"); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 798 | if (file == NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 799 | perror(filename); |
| 800 | return (-1); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 801 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 802 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 803 | while (fgets(buf, 128, file) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 804 | if (buf[0] == '#') |
| 805 | continue; |
| 806 | |
| 807 | start = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 808 | end = strchr(start, ':'); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 809 | if (end == NULL) |
| 810 | continue; |
| 811 | *end = '\0'; |
| 812 | rname = start; |
| 813 | |
| 814 | start = end + 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 815 | end = strchr(start, ':'); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 816 | if (end == NULL) |
| 817 | continue; |
| 818 | |
| 819 | start = end + 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 820 | rid = (uid_t) strtol(start, &end, 10); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 821 | if (end == start) |
| 822 | continue; |
| 823 | |
| 824 | if (name) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 825 | if (0 == strcmp(rname, name)) { |
| 826 | fclose(file); |
| 827 | return (rid); |
| 828 | } |
| 829 | } |
| 830 | if (id != -1 && id == rid) { |
| 831 | strncpy(name, rname, 8); |
| 832 | fclose(file); |
| 833 | return (TRUE); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 836 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 837 | return (-1); |
| 838 | } |
| 839 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 840 | uid_t my_getpwnam(char *name) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 841 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 842 | return my_getid("/etc/passwd", name, -1); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 845 | gid_t my_getgrnam(char *name) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 846 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 847 | return my_getid("/etc/group", name, -1); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 850 | void my_getpwuid(char *name, uid_t uid) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 851 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 852 | my_getid("/etc/passwd", name, uid); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 855 | void my_getgrgid(char *group, gid_t gid) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 856 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 857 | my_getid("/etc/group", group, gid); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 861 | #endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR */ |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 862 | |
| 863 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 864 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 865 | |
| 866 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) |
| 867 | |
| 868 | |
| 869 | #include <linux/kd.h> |
| 870 | #include <sys/ioctl.h> |
| 871 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 872 | int is_a_console(int fd) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 873 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 874 | char arg; |
| 875 | |
| 876 | arg = 0; |
| 877 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 878 | && ((arg == KB_101) || (arg == KB_84))); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 881 | static int open_a_console(char *fnam) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 882 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 883 | int fd; |
| 884 | |
| 885 | /* try read-only */ |
| 886 | fd = open(fnam, O_RDWR); |
| 887 | |
| 888 | /* if failed, try read-only */ |
| 889 | if (fd < 0 && errno == EACCES) |
| 890 | fd = open(fnam, O_RDONLY); |
| 891 | |
| 892 | /* if failed, try write-only */ |
| 893 | if (fd < 0 && errno == EACCES) |
| 894 | fd = open(fnam, O_WRONLY); |
| 895 | |
| 896 | /* if failed, fail */ |
| 897 | if (fd < 0) |
| 898 | return -1; |
| 899 | |
| 900 | /* if not a console, fail */ |
| 901 | if (!is_a_console(fd)) { |
| 902 | close(fd); |
| 903 | return -1; |
| 904 | } |
| 905 | |
| 906 | /* success */ |
| 907 | return fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Get an fd for use with kbd/console ioctls. |
| 912 | * We try several things because opening /dev/console will fail |
| 913 | * if someone else used X (which does a chown on /dev/console). |
| 914 | * |
| 915 | * if tty_name is non-NULL, try this one instead. |
| 916 | */ |
| 917 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 918 | int get_console_fd(char *tty_name) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 919 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 920 | int fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 921 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 922 | if (tty_name) { |
| 923 | if (-1 == (fd = open_a_console(tty_name))) |
| 924 | return -1; |
| 925 | else |
| 926 | return fd; |
| 927 | } |
| 928 | |
| 929 | fd = open_a_console("/dev/tty"); |
| 930 | if (fd >= 0) |
| 931 | return fd; |
| 932 | |
| 933 | fd = open_a_console("/dev/tty0"); |
| 934 | if (fd >= 0) |
| 935 | return fd; |
| 936 | |
| 937 | fd = open_a_console("/dev/console"); |
| 938 | if (fd >= 0) |
| 939 | return fd; |
| 940 | |
| 941 | for (fd = 0; fd < 3; fd++) |
| 942 | if (is_a_console(fd)) |
| 943 | return fd; |
| 944 | |
| 945 | fprintf(stderr, |
| 946 | "Couldnt get a file descriptor referring to the console\n"); |
| 947 | return -1; /* total failure */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 951 | #endif /* BB_CHVT || BB_DEALLOCVT */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 952 | |
| 953 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 954 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED) |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 955 | |
| 956 | /* Do a case insensitive strstr() */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 957 | char *stristr(char *haystack, const char *needle) |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 958 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 959 | int len = strlen(needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 960 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 961 | while (*haystack) { |
| 962 | if (!strncasecmp(haystack, needle, len)) |
| 963 | break; |
| 964 | haystack++; |
| 965 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 966 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 967 | if (!(*haystack)) |
| 968 | haystack = NULL; |
| 969 | |
| 970 | return haystack; |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 973 | /* This tries to find a needle in a haystack, but does so by |
| 974 | * only trying to match literal strings (look 'ma, no regexps!) |
| 975 | * This is short, sweet, and carries _very_ little baggage, |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 976 | * unlike its beefier cousin in regexp.c |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 977 | * -Erik Andersen |
| 978 | */ |
| 979 | extern int find_match(char *haystack, char *needle, int ignoreCase) |
| 980 | { |
| 981 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 982 | if (ignoreCase == FALSE) |
| 983 | haystack = strstr(haystack, needle); |
| 984 | else |
| 985 | haystack = stristr(haystack, needle); |
| 986 | if (haystack == NULL) |
| 987 | return FALSE; |
| 988 | return TRUE; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 992 | /* This performs substitutions after a string match has been found. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 993 | extern int replace_match(char *haystack, char *needle, char *newNeedle, |
| 994 | int ignoreCase) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 995 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 996 | int foundOne = 0; |
| 997 | char *where, *slider, *slider1, *oldhayStack; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 998 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 999 | if (ignoreCase == FALSE) |
| 1000 | where = strstr(haystack, needle); |
| 1001 | else |
| 1002 | where = stristr(haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 1003 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1004 | if (strcmp(needle, newNeedle) == 0) |
| 1005 | return FALSE; |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 1006 | |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 1007 | oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack))); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1008 | while (where != NULL) { |
| 1009 | foundOne++; |
| 1010 | strcpy(oldhayStack, haystack); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 1011 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1012 | if (strlen(newNeedle) > strlen(needle)) { |
| 1013 | haystack = |
| 1014 | (char *) realloc(haystack, |
| 1015 | (unsigned) (strlen(haystack) - |
| 1016 | strlen(needle) + |
| 1017 | strlen(newNeedle))); |
| 1018 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 1019 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1020 | for (slider = haystack, slider1 = oldhayStack; slider != where; |
| 1021 | slider++, slider1++); |
| 1022 | *slider = 0; |
| 1023 | haystack = strcat(haystack, newNeedle); |
| 1024 | slider1 += strlen(needle); |
| 1025 | haystack = strcat(haystack, slider1); |
| 1026 | where = strstr(slider, needle); |
| 1027 | } |
| 1028 | free(oldhayStack); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 1029 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1030 | if (foundOne > 0) |
| 1031 | return TRUE; |
| 1032 | else |
| 1033 | return FALSE; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1036 | #endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */ |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1037 | |
| 1038 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1039 | #if defined BB_FIND |
| 1040 | /* |
| 1041 | * Routine to see if a text string is matched by a wildcard pattern. |
| 1042 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
| 1043 | * or if the pattern is invalid. |
| 1044 | * * matches zero or more characters |
| 1045 | * ? matches a single character |
| 1046 | * [abc] matches 'a', 'b' or 'c' |
| 1047 | * \c quotes character c |
| 1048 | * Adapted from code written by Ingo Wilken, and |
| 1049 | * then taken from sash, Copyright (c) 1999 by David I. Bell |
| 1050 | * Permission is granted to use, distribute, or modify this source, |
| 1051 | * provided that this copyright notice remains intact. |
| 1052 | * Permission to distribute this code under the GPL has been granted. |
| 1053 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1054 | extern int check_wildcard_match(const char *text, const char *pattern) |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1055 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1056 | const char *retryPat; |
| 1057 | const char *retryText; |
| 1058 | int ch; |
| 1059 | int found; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1060 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1061 | retryPat = NULL; |
| 1062 | retryText = NULL; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1063 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1064 | while (*text || *pattern) { |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1065 | ch = *pattern++; |
| 1066 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1067 | switch (ch) { |
| 1068 | case '*': |
| 1069 | retryPat = pattern; |
| 1070 | retryText = text; |
| 1071 | break; |
| 1072 | |
| 1073 | case '[': |
| 1074 | found = FALSE; |
| 1075 | |
| 1076 | while ((ch = *pattern++) != ']') { |
| 1077 | if (ch == '\\') |
| 1078 | ch = *pattern++; |
| 1079 | |
| 1080 | if (ch == '\0') |
| 1081 | return FALSE; |
| 1082 | |
| 1083 | if (*text == ch) |
| 1084 | found = TRUE; |
| 1085 | } |
| 1086 | |
| 1087 | //if (!found) |
| 1088 | if (found == TRUE) { |
| 1089 | pattern = retryPat; |
| 1090 | text = ++retryText; |
| 1091 | } |
| 1092 | |
| 1093 | /* fall into next case */ |
| 1094 | |
| 1095 | case '?': |
| 1096 | if (*text++ == '\0') |
| 1097 | return FALSE; |
| 1098 | |
| 1099 | break; |
| 1100 | |
| 1101 | case '\\': |
| 1102 | ch = *pattern++; |
| 1103 | |
| 1104 | if (ch == '\0') |
| 1105 | return FALSE; |
| 1106 | |
| 1107 | /* fall into next case */ |
| 1108 | |
| 1109 | default: |
| 1110 | if (*text == ch) { |
| 1111 | if (*text) |
| 1112 | text++; |
| 1113 | break; |
| 1114 | } |
| 1115 | |
| 1116 | if (*text) { |
| 1117 | pattern = retryPat; |
| 1118 | text = ++retryText; |
| 1119 | break; |
| 1120 | } |
| 1121 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1122 | return FALSE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1125 | if (pattern == NULL) |
| 1126 | return FALSE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1129 | return TRUE; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1130 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1131 | #endif /* BB_FIND */ |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 1132 | |
| 1133 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1134 | |
| 1135 | |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1136 | #if defined BB_DF || defined BB_MTAB |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1137 | /* |
| 1138 | * Given a block device, find the mount table entry if that block device |
| 1139 | * is mounted. |
| 1140 | * |
| 1141 | * Given any other file (or directory), find the mount table entry for its |
| 1142 | * filesystem. |
| 1143 | */ |
| 1144 | extern struct mntent *findMountPoint(const char *name, const char *table) |
| 1145 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1146 | struct stat s; |
| 1147 | dev_t mountDevice; |
| 1148 | FILE *mountTable; |
| 1149 | struct mntent *mountEntry; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1150 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1151 | if (stat(name, &s) != 0) |
| 1152 | return 0; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1153 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1154 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 1155 | mountDevice = s.st_rdev; |
| 1156 | else |
| 1157 | mountDevice = s.st_dev; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1158 | |
| 1159 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1160 | if ((mountTable = setmntent(table, "r")) == 0) |
| 1161 | return 0; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1162 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1163 | while ((mountEntry = getmntent(mountTable)) != 0) { |
| 1164 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 1165 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 1166 | break; |
| 1167 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 1168 | break; |
| 1169 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 1170 | break; |
| 1171 | } |
| 1172 | endmntent(mountTable); |
| 1173 | return mountEntry; |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1174 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1175 | #endif /* BB_DF || BB_MTAB */ |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
| 1178 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1179 | #if defined BB_DD || defined BB_TAIL |
| 1180 | /* |
| 1181 | * Read a number with a possible multiplier. |
| 1182 | * Returns -1 if the number format is illegal. |
| 1183 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1184 | extern long getNum(const char *cp) |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1185 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1186 | long value; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1187 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1188 | if (!isDecimal(*cp)) |
| 1189 | return -1; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1190 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1191 | value = 0; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1192 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1193 | while (isDecimal(*cp)) |
| 1194 | value = value * 10 + *cp++ - '0'; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1195 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1196 | switch (*cp++) { |
| 1197 | case 'M': |
| 1198 | case 'm': /* `tail' uses it traditionally */ |
| 1199 | value *= 1048576; |
| 1200 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1201 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1202 | case 'k': |
| 1203 | value *= 1024; |
| 1204 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1205 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1206 | case 'b': |
| 1207 | value *= 512; |
| 1208 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1209 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1210 | case 'w': |
| 1211 | value *= 2; |
| 1212 | break; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1213 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1214 | case '\0': |
| 1215 | return value; |
| 1216 | |
| 1217 | default: |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
| 1221 | if (*cp) |
| 1222 | return -1; |
| 1223 | |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1224 | return value; |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1225 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1226 | #endif /* BB_DD || BB_TAIL */ |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1227 | |
Eric Andersen | 6805d5d | 1999-12-09 22:39:55 +0000 | [diff] [blame] | 1228 | |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1229 | #if defined BB_INIT || defined BB_SYSLOGD |
| 1230 | /* try to open up the specified device */ |
| 1231 | extern int device_open(char *device, int mode) |
| 1232 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1233 | int m, f, fd = -1; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1234 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1235 | m = mode | O_NONBLOCK; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1236 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1237 | /* Retry up to 5 times */ |
| 1238 | for (f = 0; f < 5; f++) |
| 1239 | if ((fd = open(device, m, 0600)) >= 0) |
| 1240 | break; |
| 1241 | if (fd < 0) |
| 1242 | return fd; |
| 1243 | /* Reset original flags. */ |
| 1244 | if (m != mode) |
| 1245 | fcntl(fd, F_SETFL, mode); |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1246 | return fd; |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1247 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1248 | #endif /* BB_INIT BB_SYSLOGD */ |
Erik Andersen | 31cf8e0 | 2000-02-07 20:31:19 +0000 | [diff] [blame] | 1249 | |
| 1250 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1251 | #if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF )) |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1252 | #ifdef BB_FEATURE_USE_DEVPS_PATCH |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1253 | #include <linux/devps.h> |
Eric Andersen | 6805d5d | 1999-12-09 22:39:55 +0000 | [diff] [blame] | 1254 | #endif |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1255 | |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1256 | #if defined BB_FEATURE_USE_DEVPS_PATCH |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1257 | /* findPidByName() |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1258 | * |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1259 | * This finds the pid of the specified process, |
| 1260 | * by using the /dev/ps device driver. |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1261 | * |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1262 | * Returns a list of all matching PIDs |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1263 | */ |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1264 | extern pid_t* findPidByName( char* pidName) |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1265 | { |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1266 | int fd, i, j; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1267 | char device[] = "/dev/ps"; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1268 | pid_t num_pids; |
| 1269 | pid_t* pid_array = NULL; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1270 | pid_t* pidList=NULL; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1271 | |
| 1272 | /* open device */ |
| 1273 | fd = open(device, O_RDONLY); |
| 1274 | if (fd < 0) |
| 1275 | fatalError( "open failed for `%s': %s\n", device, strerror (errno)); |
| 1276 | |
| 1277 | /* Find out how many processes there are */ |
| 1278 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) |
| 1279 | fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno)); |
| 1280 | |
| 1281 | /* Allocate some memory -- grab a few extras just in case |
| 1282 | * some new processes start up while we wait. The kernel will |
| 1283 | * just ignore any extras if we give it too many, and will trunc. |
| 1284 | * the list if we give it too few. */ |
| 1285 | pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t)); |
| 1286 | pid_array[0] = num_pids+10; |
| 1287 | |
| 1288 | /* Now grab the pid list */ |
| 1289 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) |
| 1290 | fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno)); |
| 1291 | |
| 1292 | /* Now search for a match */ |
| 1293 | for (i=1; i<pid_array[0] ; i++) { |
| 1294 | struct pid_info info; |
| 1295 | |
| 1296 | info.pid = pid_array[i]; |
| 1297 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) |
| 1298 | fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno)); |
| 1299 | |
| 1300 | if ((strstr(info.command_line, pidName) != NULL)) { |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1301 | pidList=realloc( pidList, sizeof(pid_t) * (j+2)); |
| 1302 | if (pidList==NULL) |
| 1303 | fatalError("out of memory\n"); |
| 1304 | pidList[j++]=info.pid; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1307 | pidList[j]=0; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1308 | |
| 1309 | /* Free memory */ |
| 1310 | free( pid_array); |
| 1311 | |
| 1312 | /* close device */ |
| 1313 | if (close (fd) != 0) |
| 1314 | fatalError( "close failed for `%s': %s\n",device, strerror (errno)); |
| 1315 | |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1316 | return pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1317 | } |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1318 | #else /* BB_FEATURE_USE_DEVPS_PATCH */ |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1319 | #if ! defined BB_FEATURE_USE_PROCFS |
| 1320 | #error Sorry, I depend on the /proc filesystem right now. |
| 1321 | #endif |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1322 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1323 | /* findPidByName() |
| 1324 | * |
| 1325 | * This finds the pid of the specified process. |
| 1326 | * Currently, it's implemented by rummaging through |
| 1327 | * the proc filesystem. |
| 1328 | * |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1329 | * Returns a list of all matching PIDs |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1330 | */ |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1331 | extern pid_t* findPidByName( char* pidName) |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1332 | { |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1333 | DIR *dir; |
| 1334 | struct dirent *next; |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1335 | pid_t* pidList=NULL; |
| 1336 | int i=0; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1337 | |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1338 | dir = opendir("/proc"); |
| 1339 | if (!dir) |
| 1340 | fatalError( "Cannot open /proc: %s\n", strerror (errno)); |
| 1341 | |
| 1342 | while ((next = readdir(dir)) != NULL) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1343 | FILE *status; |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1344 | char filename[256]; |
| 1345 | char buffer[256]; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1346 | |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1347 | /* If it isn't a number, we don't want it */ |
| 1348 | if (!isdigit(*next->d_name)) |
| 1349 | continue; |
| 1350 | |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1351 | /* Now open the status file */ |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 1352 | sprintf(filename, "/proc/%s/status", next->d_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1353 | status = fopen(filename, "r"); |
| 1354 | if (!status) { |
| 1355 | continue; |
| 1356 | } |
| 1357 | fgets(buffer, 256, status); |
| 1358 | fclose(status); |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1359 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 1360 | if ((strstr(buffer, pidName) != NULL)) { |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1361 | pidList=realloc( pidList, sizeof(pid_t) * (i+2)); |
| 1362 | if (pidList==NULL) |
| 1363 | fatalError("out of memory\n"); |
| 1364 | pidList[i++]=strtol(next->d_name, NULL, 0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1365 | } |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1366 | } |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1367 | pidList[i]=0; |
| 1368 | return pidList; |
John Beppu | f95ca97 | 1999-12-09 22:10:18 +0000 | [diff] [blame] | 1369 | } |
Erik Andersen | 269bba2 | 2000-03-08 14:50:47 +0000 | [diff] [blame] | 1370 | #endif /* BB_FEATURE_USE_DEVPS_PATCH */ |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1371 | #endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */ |
Eric Andersen | 1792f8c | 1999-12-09 06:11:36 +0000 | [diff] [blame] | 1372 | |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 1373 | /* this should really be farmed out to libbusybox.a */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1374 | extern void *xmalloc(size_t size) |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1375 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1376 | void *cp = malloc(size); |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1377 | |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 1378 | if (cp == NULL) |
Erik Andersen | 825aead | 2000-04-07 06:00:07 +0000 | [diff] [blame] | 1379 | fatalError("out of memory\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1380 | return cp; |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1383 | #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT) |
| 1384 | extern int vdprintf(int d, const char *format, va_list ap) |
| 1385 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1386 | char buf[BUF_SIZE]; |
| 1387 | int len; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1388 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1389 | len = vsprintf(buf, format, ap); |
| 1390 | return write(d, buf, len); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1391 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1392 | #endif /* BB_SYSLOGD */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 1393 | |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1394 | #if defined BB_FEATURE_MOUNT_LOOP |
| 1395 | extern int del_loop(const char *device) |
| 1396 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1397 | int fd; |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1398 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1399 | if ((fd = open(device, O_RDONLY)) < 0) { |
| 1400 | perror(device); |
| 1401 | return (FALSE); |
| 1402 | } |
| 1403 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
| 1404 | perror("ioctl: LOOP_CLR_FD"); |
| 1405 | return (FALSE); |
| 1406 | } |
| 1407 | close(fd); |
| 1408 | return (TRUE); |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 1409 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1410 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1411 | extern int set_loop(const char *device, const char *file, int offset, |
| 1412 | int *loopro) |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1413 | { |
| 1414 | struct loop_info loopinfo; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1415 | int fd, ffd, mode; |
| 1416 | |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1417 | mode = *loopro ? O_RDONLY : O_RDWR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1418 | if ((ffd = open(file, mode)) < 0 && !*loopro |
| 1419 | && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { |
| 1420 | perror(file); |
| 1421 | return 1; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1422 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1423 | if ((fd = open(device, mode)) < 0) { |
| 1424 | close(ffd); |
| 1425 | perror(device); |
| 1426 | return 1; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1427 | } |
| 1428 | *loopro = (mode == O_RDONLY); |
| 1429 | |
| 1430 | memset(&loopinfo, 0, sizeof(loopinfo)); |
| 1431 | strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1432 | loopinfo.lo_name[LO_NAME_SIZE - 1] = 0; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1433 | |
| 1434 | loopinfo.lo_offset = offset; |
| 1435 | |
| 1436 | loopinfo.lo_encrypt_key_size = 0; |
| 1437 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { |
| 1438 | perror("ioctl: LOOP_SET_FD"); |
| 1439 | close(fd); |
| 1440 | close(ffd); |
| 1441 | return 1; |
| 1442 | } |
| 1443 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { |
| 1444 | (void) ioctl(fd, LOOP_CLR_FD, 0); |
| 1445 | perror("ioctl: LOOP_SET_STATUS"); |
| 1446 | close(fd); |
| 1447 | close(ffd); |
| 1448 | return 1; |
| 1449 | } |
| 1450 | close(fd); |
| 1451 | close(ffd); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1455 | extern char *find_unused_loop_device(void) |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1456 | { |
| 1457 | char dev[20]; |
| 1458 | int i, fd; |
| 1459 | struct stat statbuf; |
| 1460 | struct loop_info loopinfo; |
| 1461 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1462 | for (i = 0; i <= 7; i++) { |
| 1463 | sprintf(dev, "/dev/loop%d", i); |
| 1464 | if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { |
| 1465 | if ((fd = open(dev, O_RDONLY)) >= 0) { |
| 1466 | if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) { |
| 1467 | if (errno == ENXIO) { /* probably free */ |
| 1468 | close(fd); |
| 1469 | return strdup(dev); |
| 1470 | } |
| 1471 | } |
| 1472 | close(fd); |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1473 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1474 | } |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1475 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1476 | return NULL; |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1477 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1478 | #endif /* BB_FEATURE_MOUNT_LOOP */ |
Erik Andersen | de7965c | 2000-01-26 23:49:21 +0000 | [diff] [blame] | 1479 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1480 | #if defined BB_MOUNT || defined BB_DF |
| 1481 | extern int find_real_root_device_name(char* name) |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1482 | { |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1483 | DIR *dir; |
| 1484 | struct dirent *entry; |
| 1485 | struct stat statBuf, rootStat; |
| 1486 | char fileName[BUFSIZ]; |
| 1487 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1488 | if (stat("/", &rootStat) != 0) { |
| 1489 | errorMsg("could not stat '/'\n"); |
| 1490 | return( FALSE); |
| 1491 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1492 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1493 | dir = opendir("/dev"); |
| 1494 | if (!dir) { |
| 1495 | errorMsg("could not open '/dev'\n"); |
| 1496 | return( FALSE); |
| 1497 | } |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1498 | |
| 1499 | while((entry = readdir(dir)) != NULL) { |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1500 | |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1501 | /* Must skip ".." since that is "/", and so we |
| 1502 | * would get a false positive on ".." */ |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1503 | if (strcmp(entry->d_name, "..") == 0) |
| 1504 | continue; |
| 1505 | |
| 1506 | sprintf( fileName, "/dev/%s", entry->d_name); |
| 1507 | |
| 1508 | if (stat(fileName, &statBuf) != 0) |
| 1509 | continue; |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1510 | /* Some char devices have the same dev_t as block |
| 1511 | * devices, so make sure this is a block device */ |
| 1512 | if (! S_ISBLK(statBuf.st_mode)) |
| 1513 | continue; |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1514 | if (statBuf.st_rdev == rootStat.st_rdev) { |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1515 | strcpy(name, fileName); |
| 1516 | return ( TRUE); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Erik Andersen | ec5bd90 | 2000-03-22 07:12:05 +0000 | [diff] [blame] | 1520 | return( FALSE); |
Erik Andersen | 016ffe9 | 2000-03-22 00:58:54 +0000 | [diff] [blame] | 1521 | } |
| 1522 | #endif |
| 1523 | |
| 1524 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1525 | /* END CODE */ |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 1526 | /* |
| 1527 | Local Variables: |
| 1528 | c-file-style: "linux" |
| 1529 | c-basic-offset: 4 |
| 1530 | tab-width: 4 |
| 1531 | End: |
| 1532 | */ |