]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/pivot_root.c
6462a5b95f2925bec15716fb75b9a80dd77ba439
[thirdparty/util-linux.git] / sys-utils / pivot_root.c
1 /*
2 * pivot_root.c - Change the root file system
3 *
4 * Copyright (C) 2000 Werner Almesberger
5 *
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16 #include <err.h>
17 #include <errno.h>
18 #include <getopt.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/syscall.h>
22 #include <unistd.h>
23
24 #include "c.h"
25 #include "nls.h"
26 #include "closestream.h"
27
28 #define pivot_root(new_root,put_old) syscall(SYS_pivot_root,new_root,put_old)
29
30 static void __attribute__((__noreturn__)) usage(void)
31 {
32 FILE *out = stdout;
33 fputs(USAGE_HEADER, out);
34 fprintf(out, _(" %s [options] new_root put_old\n"),
35 program_invocation_short_name);
36
37 fputs(USAGE_SEPARATOR, out);
38 fputs(_("Change the root filesystem.\n"), out);
39
40 fputs(USAGE_OPTIONS, out);
41 fprintf(out, USAGE_HELP_OPTIONS(16));
42 fprintf(out, USAGE_MAN_TAIL("pivot_root(8)"));
43 exit(EXIT_SUCCESS);
44 }
45
46 int main(int argc, char **argv)
47 {
48 int ch;
49 static const struct option longopts[] = {
50 {"version", no_argument, NULL, 'V'},
51 {"help", no_argument, NULL, 'h'},
52 {NULL, 0, NULL, 0}
53 };
54
55 setlocale(LC_ALL, "");
56 bindtextdomain(PACKAGE, LOCALEDIR);
57 textdomain(PACKAGE);
58 close_stdout_atexit();
59
60 while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
61 switch (ch) {
62 case 'V':
63 print_version(EXIT_SUCCESS);
64 case 'h':
65 usage();
66 default:
67 errtryhelp(EXIT_FAILURE);
68 }
69
70 if (argc != 3) {
71 warnx(_("bad usage"));
72 errtryhelp(EXIT_FAILURE);
73 }
74 if (pivot_root(argv[1], argv[2]) < 0)
75 err(EXIT_FAILURE, _("failed to change root from `%s' to `%s'"),
76 argv[1], argv[2]);
77
78 return EXIT_SUCCESS;
79 }