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