util-linux/mkswap.c: fix warning
[platform/upstream/busybox.git] / util-linux / mkswap.c
1 /* vi: set sw=4 ts=4: */
2 /* mkswap.c - format swap device (Linux v1 only)
3  *
4  * Copyright 2006 Rob Landley <rob@landley.net>
5  *
6  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
7  */
8 #include "libbb.h"
9
10 #if ENABLE_SELINUX
11 static void mkswap_selinux_setcontext(int fd, const char *path)
12 {
13         struct stat stbuf;
14
15         if (!is_selinux_enabled())
16                 return;
17
18         if (fstat(fd, &stbuf) < 0)
19                 bb_perror_msg_and_die("fstat failed");
20         if (S_ISREG(stbuf.st_mode)) {
21                 security_context_t newcon;
22                 security_context_t oldcon = NULL;
23                 context_t context;
24
25                 if (fgetfilecon(fd, &oldcon) < 0) {
26                         if (errno != ENODATA)
27                                 goto error;
28                         if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
29                                 goto error;
30                 }
31                 context = context_new(oldcon);
32                 if (!context || context_type_set(context, "swapfile_t"))
33                         goto error;
34                 newcon = context_str(context);
35                 if (!newcon)
36                         goto error;
37                 /* fsetfilecon_raw is hidden */
38                 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
39                         goto error;
40                 if (ENABLE_FEATURE_CLEAN_UP) {
41                         context_free(context);
42                         freecon(oldcon);
43                 }
44         }
45         return;
46  error:
47         bb_perror_msg_and_die("SELinux relabeling failed");
48 }
49 #else
50 # define mkswap_selinux_setcontext(fd, path) ((void)0)
51 #endif
52
53 #if 0 /* from Linux 2.6.23 */
54 /*
55  * Magic header for a swap area. The first part of the union is
56  * what the swap magic looks like for the old (limited to 128MB)
57  * swap area format, the second part of the union adds - in the
58  * old reserved area - some extra information. Note that the first
59  * kilobyte is reserved for boot loader or disk label stuff...
60  */
61 union swap_header {
62         struct {
63                 char reserved[PAGE_SIZE - 10];
64                 char magic[10];                 /* SWAP-SPACE or SWAPSPACE2 */
65         } magic;
66         struct {
67                 char            bootbits[1024]; /* Space for disklabel etc. */
68                 __u32           version;        /* second kbyte, word 0 */
69                 __u32           last_page;      /* 1 */
70                 __u32           nr_badpages;    /* 2 */
71                 unsigned char   sws_uuid[16];   /* 3,4,5,6 */
72                 unsigned char   sws_volume[16]; /* 7,8,9,10  */
73                 __u32           padding[117];   /* 11..127 */
74                 __u32           badpages[1];    /* 128, total 129 32-bit words */
75         } info;
76 };
77 #endif
78
79 #define NWORDS 129
80 #define hdr ((uint32_t*)(&bb_common_bufsiz1))
81
82 struct BUG_bufsiz1_is_too_small {
83         char BUG_bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
84 };
85
86 /* Stored without terminating NUL */
87 static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
88
89 int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
90 int mkswap_main(int argc, char **argv)
91 {
92         int fd, pagesize;
93         off_t len;
94
95         // No options supported.
96
97         if (argc != 2) bb_show_usage();
98
99         // Figure out how big the device is and announce our intentions.
100
101         fd = xopen(argv[1], O_RDWR);
102         /* fdlength was reported to be unreliable - use seek */
103         len = xlseek(fd, 0, SEEK_END);
104 #if ENABLE_SELINUX
105         xlseek(fd, 0, SEEK_SET);
106 #endif
107         pagesize = getpagesize();
108         printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n",
109                         len - pagesize);
110         mkswap_selinux_setcontext(fd, argv[1]);
111
112         // Make a header. hdr is zero-filled so far...
113         hdr[0] = 1;
114         hdr[1] = (len / pagesize) - 1;
115         if (ENABLE_FEATURE_MKSWAP_UUID) {
116                 char uuid_string[32];
117                 generate_uuid((void*) &hdr[3]);
118                 bin2hex(uuid_string, (void*) &hdr[3], 16);
119                 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
120                 printf("UUID=%.8s"  "-%.4s-%.4s-%.4s-%.12s\n",
121                         uuid_string,
122                         uuid_string+8,
123                         uuid_string+8+4,
124                         uuid_string+8+4+4,
125                         uuid_string+8+4+4+4
126                 );
127         }
128
129         // Write the header.  Sync to disk because some kernel versions check
130         // signature on disk (not in cache) during swapon.
131         xlseek(fd, 1024, SEEK_SET);
132         xwrite(fd, hdr, NWORDS * 4);
133         xlseek(fd, pagesize - 10, SEEK_SET);
134         xwrite(fd, SWAPSPACE2, 10);
135         fsync(fd);
136
137         if (ENABLE_FEATURE_CLEAN_UP) close(fd);
138
139         return 0;
140 }