blob: 8b34490677f59f3a5b708a8502b5bcafa22a3ff2 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * util.c --- utilities for the debugfs program
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o3839e651997-04-26 13:21:57 +00004 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
6 *
7 */
8
Theodore Ts'oebabf2a2008-07-13 15:32:37 -04009#define _XOPEN_SOURCE 600 /* needed for strptime */
Andreas Dilgerde8f3a72007-05-25 11:18:11 -040010
Theodore Ts'o3839e651997-04-26 13:21:57 +000011#include <stdio.h>
12#include <unistd.h>
13#include <stdlib.h>
14#include <ctype.h>
15#include <string.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000016#include <time.h>
Theodore Ts'o2c4a5402000-08-19 17:33:28 +000017#include <signal.h>
Theodore Ts'o88494bb2003-05-13 23:03:43 -040018#ifdef HAVE_GETOPT_H
19#include <getopt.h>
Theodore Ts'oefc6f622008-08-27 23:07:54 -040020#else
Theodore Ts'o88494bb2003-05-13 23:03:43 -040021extern int optind;
22extern char *optarg;
23#endif
24#ifdef HAVE_OPTRESET
25extern int optreset; /* defined by BSD, but not others */
26#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000027
JP Abgralle0ed7402014-03-19 19:08:39 -070028#include "ss/ss.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include "debugfs.h"
30
Theodore Ts'o88494bb2003-05-13 23:03:43 -040031/*
32 * This function resets the libc getopt() function, which keeps
33 * internal state. Bad design! Stupid libc API designers! No
34 * biscuit!
35 *
36 * BSD-derived getopt() functions require that optind be reset to 1 in
37 * order to reset getopt() state. This used to be generally accepted
38 * way of resetting getopt(). However, glibc's getopt()
39 * has additional getopt() state beyond optind, and requires that
40 * optind be set zero to reset its state. So the unfortunate state of
41 * affairs is that BSD-derived versions of getopt() misbehave if
42 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
Theodore Ts'o9c07dc02006-05-29 11:06:16 -040043 * will core dump if optind is set 1 in order to reset getopt().
Theodore Ts'oefc6f622008-08-27 23:07:54 -040044 *
Theodore Ts'o88494bb2003-05-13 23:03:43 -040045 * More modern versions of BSD require that optreset be set to 1 in
46 * order to reset getopt(). Sigh. Standards, anyone?
47 *
48 * We hide the hair here.
49 */
50void reset_getopt(void)
51{
Theodore Ts'o9c07dc02006-05-29 11:06:16 -040052#if defined(__GLIBC__) || defined(__linux__)
Theodore Ts'o88494bb2003-05-13 23:03:43 -040053 optind = 0;
54#else
55 optind = 1;
56#endif
57#ifdef HAVE_OPTRESET
58 optreset = 1; /* Makes BSD getopt happy */
59#endif
Theodore Ts'oec7fdb82003-12-07 13:16:25 -050060}
61
Theodore Ts'o2b5ddd72004-01-24 18:54:41 -050062static const char *pager_search_list[] = { "pager", "more", "less", 0 };
Theodore Ts'oec7fdb82003-12-07 13:16:25 -050063static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
64
65static const char *find_pager(char *buf)
66{
67 const char **i, **j;
68
69 for (i = pager_search_list; *i; i++) {
70 for (j = pager_dir_list; *j; j++) {
71 sprintf(buf, "%s/%s", *j, *i);
72 if (access(buf, X_OK) == 0)
73 return(buf);
74 }
75 }
76 return 0;
77}
Theodore Ts'o88494bb2003-05-13 23:03:43 -040078
Theodore Ts'o3839e651997-04-26 13:21:57 +000079FILE *open_pager(void)
80{
Theodore Ts'oec7fdb82003-12-07 13:16:25 -050081 FILE *outfile = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -070082 const char *pager = ss_safe_getenv("DEBUGFS_PAGER");
Theodore Ts'oec7fdb82003-12-07 13:16:25 -050083 char buf[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +000084
Theodore Ts'o2c4a5402000-08-19 17:33:28 +000085 signal(SIGPIPE, SIG_IGN);
Theodore Ts'oa7ee4e02008-03-20 10:57:55 -040086 if (!isatty(1))
87 return stdout;
Theodore Ts'o2b696a92003-12-25 14:28:55 -050088 if (!pager)
JP Abgralle0ed7402014-03-19 19:08:39 -070089 pager = ss_safe_getenv("PAGER");
Theodore Ts'o2b696a92003-12-25 14:28:55 -050090 if (!pager)
Theodore Ts'oec7fdb82003-12-07 13:16:25 -050091 pager = find_pager(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040092 if (!pager ||
Theodore Ts'o2b696a92003-12-25 14:28:55 -050093 (strcmp(pager, "__none__") == 0) ||
94 ((outfile = popen(pager, "w")) == 0))
95 return stdout;
96 return outfile;
Theodore Ts'o3839e651997-04-26 13:21:57 +000097}
98
99void close_pager(FILE *stream)
100{
Theodore Ts'o571fc5a2001-12-02 17:23:27 +0100101 if (stream && stream != stdout) pclose(stream);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102}
103
104/*
105 * This routine is used whenever a command needs to turn a string into
106 * an inode.
107 */
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000108ext2_ino_t string_to_inode(char *str)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000110 ext2_ino_t ino;
111 int len = strlen(str);
112 char *end;
113 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114
115 /*
116 * If the string is of the form <ino>, then treat it as an
117 * inode number.
118 */
119 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
Theodore Ts'o9131a752000-08-23 04:36:25 +0000120 ino = strtoul(str+1, &end, 0);
121 if (*end=='>')
122 return ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123 }
124
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000125 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126 if (retval) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400127 com_err(str, retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 return 0;
129 }
130 return ino;
131}
132
133/*
134 * This routine returns 1 if the filesystem is not open, and prints an
135 * error message to that effect.
136 */
137int check_fs_open(char *name)
138{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000139 if (!current_fs) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 com_err(name, 0, "Filesystem not open");
141 return 1;
142 }
143 return 0;
144}
145
146/*
147 * This routine returns 1 if a filesystem is open, and prints an
148 * error message to that effect.
149 */
150int check_fs_not_open(char *name)
151{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000152 if (current_fs) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153 com_err(name, 0,
154 "Filesystem %s is still open. Close it first.\n",
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000155 current_fs->device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 return 1;
157 }
158 return 0;
159}
160
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000161/*
162 * This routine returns 1 if a filesystem is not opened read/write,
163 * and prints an error message to that effect.
164 */
165int check_fs_read_write(char *name)
166{
167 if (!(current_fs->flags & EXT2_FLAG_RW)) {
168 com_err(name, 0, "Filesystem opened read/only");
169 return 1;
170 }
171 return 0;
172}
173
174/*
Theodore Ts'od61f6172000-05-27 16:04:00 +0000175 * This routine returns 1 if a filesystem is doesn't have its inode
176 * and block bitmaps loaded, and prints an error message to that
177 * effect.
178 */
179int check_fs_bitmaps(char *name)
180{
181 if (!current_fs->block_map || !current_fs->inode_map) {
182 com_err(name, 0, "Filesystem bitmaps not loaded");
183 return 1;
184 }
185 return 0;
186}
187
188/*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000189 * This function takes a __u32 time value and converts it to a string,
190 * using ctime
191 */
192char *time_to_string(__u32 cl)
193{
Theodore Ts'oacb79d92004-12-15 12:21:41 -0500194 static int do_gmt = -1;
195 time_t t = (time_t) cl;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -0400196 const char *tz;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000197
Theodore Ts'o8ff1a862004-11-30 19:57:20 -0500198 if (do_gmt == -1) {
199 /* The diet libc doesn't respect the TZ environemnt variable */
JP Abgralle0ed7402014-03-19 19:08:39 -0700200 tz = ss_safe_getenv("TZ");
Theodore Ts'oacb79d92004-12-15 12:21:41 -0500201 if (!tz)
202 tz = "";
203 do_gmt = !strcmp(tz, "GMT");
Theodore Ts'o8ff1a862004-11-30 19:57:20 -0500204 }
205
206 return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000207}
208
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500209/*
Theodore Ts'o4efae602005-09-24 21:56:38 -0400210 * Parse a string as a time. Return ((time_t)-1) if the string
211 * doesn't appear to be a sane time.
212 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700213time_t string_to_time(const char *arg)
Theodore Ts'o4efae602005-09-24 21:56:38 -0400214{
215 struct tm ts;
Theodore Ts'oa4ea6b92007-04-10 21:10:55 -0400216 time_t ret;
Theodore Ts'o4efae602005-09-24 21:56:38 -0400217 char *tmp;
218
219 if (strcmp(arg, "now") == 0) {
220 return time(0);
221 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700222 if (arg[0] == '@') {
223 /* interpret it as an integer */
224 ret = strtoul(arg+1, &tmp, 0);
225 if (*tmp)
226 return ((time_t) -1);
227 return ret;
228 }
Theodore Ts'o4efae602005-09-24 21:56:38 -0400229 memset(&ts, 0, sizeof(ts));
230#ifdef HAVE_STRPTIME
231 strptime(arg, "%Y%m%d%H%M%S", &ts);
232#else
233 sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
234 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
235 ts.tm_year -= 1900;
236 ts.tm_mon -= 1;
237 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
238 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
239 ts.tm_min > 59 || ts.tm_sec > 61)
240 ts.tm_mday = 0;
241#endif
Theodore Ts'oa2ff0f32008-03-21 09:10:09 -0400242 ts.tm_isdst = -1;
Theodore Ts'oa4ea6b92007-04-10 21:10:55 -0400243 ret = mktime(&ts);
244 if (ts.tm_mday == 0 || ret == ((time_t) -1)) {
Theodore Ts'o4efae602005-09-24 21:56:38 -0400245 /* Try it as an integer... */
Theodore Ts'o4efae602005-09-24 21:56:38 -0400246 ret = strtoul(arg, &tmp, 0);
247 if (*tmp)
248 return ((time_t) -1);
249 }
Theodore Ts'oa4ea6b92007-04-10 21:10:55 -0400250 return ret;
Theodore Ts'o4efae602005-09-24 21:56:38 -0400251}
252
253/*
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500254 * This function will convert a string to an unsigned long, printing
255 * an error message if it fails, and returning success or failure in err.
256 */
257unsigned long parse_ulong(const char *str, const char *cmd,
258 const char *descr, int *err)
259{
260 char *tmp;
261 unsigned long ret;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400262
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500263 ret = strtoul(str, &tmp, 0);
264 if (*tmp == 0) {
Theodore Ts'oe5b3b272002-04-01 15:42:21 -0500265 if (err)
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500266 *err = 0;
267 return ret;
268 }
269 com_err(cmd, 0, "Bad %s - %s", descr, str);
Theodore Ts'oe5b3b272002-04-01 15:42:21 -0500270 if (err)
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500271 *err = 1;
272 else
273 exit(1);
274 return 0;
275}
276
277/*
JP Abgralle0ed7402014-03-19 19:08:39 -0700278 * This function will convert a string to an unsigned long long, printing
279 * an error message if it fails, and returning success or failure in err.
280 */
281unsigned long long parse_ulonglong(const char *str, const char *cmd,
282 const char *descr, int *err)
283{
284 char *tmp;
285 unsigned long long ret;
286
287 ret = strtoull(str, &tmp, 0);
288 if (*tmp == 0) {
289 if (err)
290 *err = 0;
291 return ret;
292 }
293 com_err(cmd, 0, "Bad %s - %s", descr, str);
294 if (err)
295 *err = 1;
296 else
297 exit(1);
298 return 0;
299}
300
301/*
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500302 * This function will convert a string to a block number. It returns
303 * 0 on success, 1 on failure.
304 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700305int strtoblk(const char *cmd, const char *str, blk64_t *ret)
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500306{
JP Abgralle0ed7402014-03-19 19:08:39 -0700307 blk64_t blk;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500308 int err;
309
JP Abgralle0ed7402014-03-19 19:08:39 -0700310 blk = parse_ulonglong(str, cmd, "block number", &err);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500311 *ret = blk;
Theodore Ts'od8d12552008-02-18 09:55:04 -0500312 if (err)
313 com_err(cmd, 0, "Invalid block number: %s", str);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500314 return err;
315}
316
317/*
318 * This is a common helper function used by the command processing
319 * routines
320 */
321int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
322 const char *cmd, const char *usage, int flags)
323{
324 if (argc < min_argc || argc > max_argc) {
325 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
326 return 1;
327 }
328 if (flags & CHECK_FS_NOTOPEN) {
329 if (check_fs_not_open(argv[0]))
330 return 1;
331 } else {
332 if (check_fs_open(argv[0]))
333 return 1;
334 }
335 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
336 return 1;
337 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
338 return 1;
339 return 0;
340}
341
342/*
343 * This is a helper function used by do_stat, do_freei, do_seti, and
344 * do_testi, etc. Basically, any command which takes a single
345 * argument which is a file/inode number specifier.
346 */
347int common_inode_args_process(int argc, char *argv[],
348 ext2_ino_t *inode, int flags)
349{
350 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
351 return 1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400352
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500353 *inode = string_to_inode(argv[1]);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400354 if (!*inode)
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500355 return 1;
356 return 0;
357}
358
359/*
360 * This is a helper function used by do_freeb, do_setb, and do_testb
361 */
362int common_block_args_process(int argc, char *argv[],
JP Abgralle0ed7402014-03-19 19:08:39 -0700363 blk64_t *block, blk64_t *count)
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500364{
365 int err;
366
367 if (common_args_process(argc, argv, 2, 3, argv[0],
368 "<block> [count]", CHECK_FS_BITMAPS))
369 return 1;
370
371 if (strtoblk(argv[0], argv[1], block))
372 return 1;
Theodore Ts'od8d12552008-02-18 09:55:04 -0500373 if (*block == 0) {
374 com_err(argv[0], 0, "Invalid block number 0");
375 err = 1;
376 }
377
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500378 if (argc > 2) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700379 err = strtoblk(argv[0], argv[2], count);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500380 if (err)
381 return 1;
382 }
383 return 0;
384}
385
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500386int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
387 const char *cmd, int bufsize)
388{
389 int retval;
390
391 retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
392 if (retval) {
393 com_err(cmd, retval, "while reading inode %u", ino);
394 return 1;
395 }
396 return 0;
397}
398
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500399int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
400 const char *cmd)
401{
402 int retval;
403
404 retval = ext2fs_read_inode(current_fs, ino, inode);
405 if (retval) {
406 com_err(cmd, retval, "while reading inode %u", ino);
407 return 1;
408 }
409 return 0;
410}
411
JP Abgralle0ed7402014-03-19 19:08:39 -0700412int debugfs_write_inode_full(ext2_ino_t ino,
413 struct ext2_inode *inode,
414 const char *cmd,
415 int bufsize)
416{
417 int retval;
418
419 retval = ext2fs_write_inode_full(current_fs, ino,
420 inode, bufsize);
421 if (retval) {
422 com_err(cmd, retval, "while writing inode %u", ino);
423 return 1;
424 }
425 return 0;
426}
427
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500428int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
429 const char *cmd)
430{
431 int retval;
432
433 retval = ext2fs_write_inode(current_fs, ino, inode);
434 if (retval) {
435 com_err(cmd, retval, "while writing inode %u", ino);
436 return 1;
437 }
438 return 0;
439}
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000440
Theodore Ts'o030970e2005-03-20 20:05:22 -0500441int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode,
442 const char *cmd)
443{
444 int retval;
445
446 retval = ext2fs_write_new_inode(current_fs, ino, inode);
447 if (retval) {
448 com_err(cmd, retval, "while creating inode %u", ino);
449 return 1;
450 }
451 return 0;
452}
JP Abgralle0ed7402014-03-19 19:08:39 -0700453
454/*
455 * Given a mode, return the ext2 file type
456 */
457int ext2_file_type(unsigned int mode)
458{
459 if (LINUX_S_ISREG(mode))
460 return EXT2_FT_REG_FILE;
461
462 if (LINUX_S_ISDIR(mode))
463 return EXT2_FT_DIR;
464
465 if (LINUX_S_ISCHR(mode))
466 return EXT2_FT_CHRDEV;
467
468 if (LINUX_S_ISBLK(mode))
469 return EXT2_FT_BLKDEV;
470
471 if (LINUX_S_ISLNK(mode))
472 return EXT2_FT_SYMLINK;
473
474 if (LINUX_S_ISFIFO(mode))
475 return EXT2_FT_FIFO;
476
477 if (LINUX_S_ISSOCK(mode))
478 return EXT2_FT_SOCK;
479
480 return 0;
481}