Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 2 | /* mkswap.c - format swap device (Linux v1 only) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | * |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 4 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 5 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 6 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 11 | #if ENABLE_SELINUX |
| 12 | static 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 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 53 | int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 54 | int mkswap_main(int argc, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | { |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 56 | int fd, pagesize; |
| 57 | off_t len; |
| 58 | unsigned int hdr[129]; |
| 59 | |
| 60 | // No options supported. |
| 61 | |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 62 | if (argc != 2) bb_show_usage(); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 63 | |
| 64 | // Figure out how big the device is and announce our intentions. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 66 | fd = xopen(argv[1], O_RDWR); |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 67 | /* fdlength was reported to be unreliable - use seek */ |
| 68 | len = xlseek(fd, 0, SEEK_END); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 69 | #if ENABLE_SELINUX |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 70 | xlseek(fd, 0, SEEK_SET); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 71 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 72 | pagesize = getpagesize(); |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 73 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 74 | len - pagesize); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 75 | mkswap_selinux_setcontext(fd, argv[1]); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 76 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 77 | // Make a header. |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 79 | memset(hdr, 0, sizeof(hdr)); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 80 | hdr[0] = 1; |
| 81 | hdr[1] = (len / pagesize) - 1; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 82 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 83 | // Write the header. Sync to disk because some kernel versions check |
| 84 | // signature on disk (not in cache) during swapon. |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 85 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 86 | xlseek(fd, 1024, SEEK_SET); |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 87 | xwrite(fd, hdr, sizeof(hdr)); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 88 | xlseek(fd, pagesize - 10, SEEK_SET); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 89 | xwrite(fd, "SWAPSPACE2", 10); |
| 90 | fsync(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 91 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 92 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 93 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 94 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | } |