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