summaryrefslogtreecommitdiff
path: root/src/pm-reset-swap.c
blob: 559fce6eb69e14d684c347d793b050f4e21a8bcf (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * mark a swap device as not usable for thaw
 *
 * Copyright 2006 Red Hat, Inc.
 *
 * Authors:
 *   Peter Jones <pjones@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#define _GNU_SOURCE 1

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

/* XXX this is a total hack for bad system headers. */
typedef u_int32_t __u32;
#include <linux/pmu.h>

typedef u_int64_t sector_t;

/*
 * Here's what the disk looks like after a successful hibernate:

 00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 *
 00000400  01 00 00 00 ff df 03 00  00 00 00 00 00 00 00 00  |................|
 00000410  00 00 00 00 00 00 00 00  00 00 00 00 73 77 61 70  |............swap|
 00000420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 *
 00000fe0  00 00 00 00 01 00 00 00  00 00 00 00 53 57 41 50  |............SWAP|
 00000ff0  53 50 41 43 45 32 53 31  53 55 53 50 45 4e 44 00  |SPACE2S1SUSPEND.|

 * And here's what it looks like if you haven't hibernated to it:

 00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 *
 00000400  01 00 00 00 ff df 03 00  00 00 00 00 00 00 00 00  |................|
 00000410  00 00 00 00 00 00 00 00  00 00 00 00 73 77 61 70  |............swap|
 00000420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 *
 00000ff0  00 00 00 00 00 00 53 57  41 50 53 50 41 43 45 32  |......SWAPSPACE2|

 *
 */

/* The signature offset is annoying.  The struct is:
 * struct swsusp_header {
 *     char reserved[PAGE_SIZE - 20 - sizeof(sector_t)];
 *     sector_t image;
 *     char orig_sig[10];
 *     char sig[10];
 * }
 */
off_t get_sig_offset(int orig)
{
    int pagesize = sysconf(_SC_PAGESIZE);

    return (off_t)((sizeof (char) * pagesize) - (orig ? 20 : 10));
}

static inline int
print_usage(FILE *output, int retval)
{
    fprintf(output, "usage: pm-reset-swap <device node>\n");
    return retval;
}

int check_resume_block(FILE *dev, off_t offset)
{
    char buf[11] = {0,};
    off_t location = offset + get_sig_offset(0);

    if (fseek(dev, location, SEEK_SET) < 0)
        return -1;
    if (fread(buf, sizeof (char), 10, dev) != 10)
        return -1;

    if (!strncmp(buf, "S1SUSPEND", 9) ||
            !strncmp(buf, "ULSUSPEND", 9))
        return 1;

    return 0;
}

int clear_resume_block(FILE *dev, off_t offset)
{
    char buf[21] = {0,};
    off_t location = offset + get_sig_offset(1);

    if (fseek(dev, location, SEEK_SET) < 0)
        return -1;

    if (fread(buf, sizeof (char), 20, dev) != 20)
        return -1;

    if (fseek(dev, location, SEEK_SET) < 0)
        return -1;

    memmove(buf+10, buf, 10);
    memset(buf, '\0', 10);

    if (fwrite(buf, sizeof (char), 20, dev) != 20)
        return -1;

    return 0;
}

int main(int argc, char *argv[])
{
    FILE *dev = NULL;
    int rc;

    if (argc != 2)
        return print_usage(stderr, 1);

    if (!strcmp(argv[1], "--help")) {
        return print_usage(stdout, 0);
    }

    if (!(dev = fopen(argv[1], "r+b"))) {
        fprintf(stderr, "Could not open \"%s\": %m\n", argv[1]);
        return 1;
    }

    rc = check_resume_block(dev, 0);
    if (rc < 0) {
        fprintf(stderr, "Could not check \"%s\" for swap signature: %m\n",
                argv[1]);
        fclose(dev);
        return 2;
    }

    if (rc == 1) {
        if (clear_resume_block(dev, 0)) {
            fprintf(stderr, "Could not clear swap signature on \"%s\": %m\n",
                    argv[1]);
            fclose(dev);
            return 1;
        }
    }
        
    fclose(dev);
    return 0;
}

/*
 * vim:ts=8:sw=4:sts=4:et
 */