aboutsummaryrefslogtreecommitdiff
path: root/util-linux/mkswap.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-10-19 21:49:48 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-10-19 21:49:48 +0000
commit85ff862753be5b7b5d3855681f9234c131659b8a (patch)
tree03f108c8ef00faa5df9ba37296eac3d518347ed8 /util-linux/mkswap.c
parent1a7afb48daf054d8cc9d57e8a569a6ac90acdd7d (diff)
downloadbusybox-w32-85ff862753be5b7b5d3855681f9234c131659b8a.tar.gz
busybox-w32-85ff862753be5b7b5d3855681f9234c131659b8a.tar.bz2
busybox-w32-85ff862753be5b7b5d3855681f9234c131659b8a.zip
mkswap: selinux support by KaiGai Kohei <kaigai@ak.jp.nec.com>
Diffstat (limited to 'util-linux/mkswap.c')
-rw-r--r--util-linux/mkswap.c45
1 files changed, 44 insertions, 1 deletions
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c
index af4cc10f7..8e1fbc384 100644
--- a/util-linux/mkswap.c
+++ b/util-linux/mkswap.c
@@ -8,6 +8,48 @@
8 8
9#include "libbb.h" 9#include "libbb.h"
10 10
11#if ENABLE_SELINUX
12static void mkswap_selinux_setcontext(int fd, const char *path)
13{
14 struct stat stbuf;
15
16 if (!is_selinux_enabled())
17 return;
18
19 if (fstat(fd, &stbuf) < 0)
20 bb_perror_msg_and_die("fstat failed");
21 if (S_ISREG(stbuf.st_mode)) {
22 security_context_t newcon;
23 security_context_t oldcon = NULL;
24 context_t context;
25
26 if (fgetfilecon_raw(fd, &oldcon) < 0) {
27 if (errno != ENODATA)
28 goto error;
29 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
30 goto error;
31 }
32 context = context_new(oldcon);
33 if (!context || context_type_set(context, "swapfile_t"))
34 goto error;
35 newcon = context_str(context);
36 if (!newcon)
37 goto error;
38 if (strcmp(oldcon, newcon) != 0 && fsetfilecon_raw(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
11int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 53int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
12int mkswap_main(int argc, char **argv) 54int mkswap_main(int argc, char **argv)
13{ 55{
@@ -26,6 +68,7 @@ int mkswap_main(int argc, char **argv)
26 pagesize = getpagesize(); 68 pagesize = getpagesize();
27 printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n", 69 printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n",
28 len - pagesize); 70 len - pagesize);
71 mkswap_selinux_setcontext(fd, argv[1]);
29 72
30 // Make a header. 73 // Make a header.
31 74
@@ -38,7 +81,7 @@ int mkswap_main(int argc, char **argv)
38 81
39 xlseek(fd, 1024, SEEK_SET); 82 xlseek(fd, 1024, SEEK_SET);
40 xwrite(fd, hdr, sizeof(hdr)); 83 xwrite(fd, hdr, sizeof(hdr));
41 xlseek(fd, pagesize-10, SEEK_SET); 84 xlseek(fd, pagesize - 10, SEEK_SET);
42 xwrite(fd, "SWAPSPACE2", 10); 85 xwrite(fd, "SWAPSPACE2", 10);
43 fsync(fd); 86 fsync(fd);
44 87