File: swap-offset.c

package info (click to toggle)
uswsusp 1.0%2B20120915-6.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,572 kB
  • ctags: 1,440
  • sloc: ansic: 7,164; sh: 566; makefile: 223; perl: 65
file content (127 lines) | stat: -rw-r--r-- 2,488 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * swap-offset.c
 *
 * This program determines the location of the swap offset for given swap file.
 *
 * Copyright (C) 2006 Luca Tettamanti <kronos.it@gmail.com>
 *
 * This file is released under the GPLv2.
 *
 */

#include "config.h"
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <errno.h>

#define SWAP_SIG	"SWAPSPACE2"
#define SWAP_SIG_SIZE	10

int main(int argc, char **argv)
{
	unsigned int block, last_block, first_block, blocks_per_page;
	unsigned int offset;
	int size, blk_size;
	int fd;
	unsigned int i;
	ssize_t ret;
	struct stat stat;
	unsigned char buf[SWAP_SIG_SIZE];
	int err = 0;
	int const page_size = getpagesize();

	if (argc < 2) {
		fprintf(stderr, "Usage: swap-offset <file_name>\n");
		return EINVAL;
	}

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		err = errno;
		perror("open()");
		return err;
	}
	if (fstat(fd, &stat)) {
		err = errno;
		perror("fstat()");
		goto out;
	}
	if (!S_ISREG(stat.st_mode)) {
		fprintf(stderr, "Not a regular file\n");
		err = EINVAL;
		goto out;
	}

	/* Check swap signature */
	if (lseek(fd, page_size - SWAP_SIG_SIZE, SEEK_SET) < 0) {
		err = errno;
		perror("lseek()");
		goto out;
	}
	ret = read(fd, buf, SWAP_SIG_SIZE);
	if (ret < 0) {
		err = errno;
		perror("read()");
		goto out;
	} else if (ret < SWAP_SIG_SIZE) {
		fprintf(stderr, "Failed to read swap signature: file is too short.\n");
		err = EINVAL;
		goto out;
	}

	if (memcmp(buf, SWAP_SIG, SWAP_SIG_SIZE)) {
		fprintf(stderr, "Swap signature not found.\n");
		err = EINVAL;
		goto out;
	}

	if (ioctl(fd, FIGETBSZ, &blk_size)) {
		err = errno;
		perror("ioctl(FIGETBSZ) failed");
		goto out;
	}

	blocks_per_page = page_size / blk_size;

	/* Check that the header is contiguous */
	last_block = 0;
	first_block = 0;
	size = 0;
	for (i = 0; i < blocks_per_page; i++) {
		block = i;

		if (ioctl(fd, FIBMAP, &block)) {
			err = errno;
			perror("ioctl(FIBMAP) failed");
			goto out;
		}

		if (last_block && block != last_block + 1)
			break;

		if (!first_block)
			first_block = block;

		size += blk_size;
		last_block = block;
	}
	if (size < page_size) {
		fprintf(stderr, "Swapfile header is not contiguous and cannot "
				"be used for suspension.\n");
		err = EINVAL;
	} else {
		offset = (unsigned long long)first_block * blk_size / page_size;
		printf("resume offset = %u\n", offset);
	}

out:
	close(fd);

	return err;
}