]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/pivot_root.c
Merge branch 'getrandom' of git://github.com/kerolasa/lelux-utiliteetit
[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(FILE * out)
31 {
32 fprintf(out, USAGE_HEADER);
33 fprintf(out, _(" %s [options] new_root put_old\n"),
34 program_invocation_short_name);
35
36 fputs(USAGE_SEPARATOR, out);
37 fputs(_("Change the root filesystem.\n"), out);
38
39 fprintf(out, USAGE_OPTIONS);
40 fprintf(out, USAGE_HELP);
41 fprintf(out, USAGE_VERSION);
42 fprintf(out, USAGE_MAN_TAIL("pivot_root(8)"));
43 exit(out == stderr ? EXIT_FAILURE : 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 atexit(close_stdout);
59
60 while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
61 switch (ch) {
62 case 'V':
63 printf(UTIL_LINUX_VERSION);
64 return EXIT_SUCCESS;
65 case 'h':
66 usage(stdout);
67 default:
68 errtryhelp(EXIT_FAILURE);
69 }
70
71 if (argc != 3)
72 usage(stderr);
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 }