Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/kinit/resume/resumelib.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1297 - (show annotations) (download)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File MIME type: text/plain
File size: 2061 byte(s)
-updated to klibc-1.5.22 with mntproc definitions patch included
1 /*
2 * Handle resume from suspend-to-disk
3 */
4
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <limits.h>
10 #include <sys/stat.h>
11 #include <sys/sysmacros.h>
12
13 #include "kinit.h"
14 #include "do_mounts.h"
15 #include "resume.h"
16
17 #ifndef CONFIG_PM_STD_PARTITION
18 # define CONFIG_PM_STD_PARTITION ""
19 #endif
20
21 int do_resume(int argc, char *argv[])
22 {
23 const char *resume_file = CONFIG_PM_STD_PARTITION;
24 const char *resume_arg;
25 unsigned long long resume_offset;
26
27 resume_arg = get_arg(argc, argv, "resume=");
28 resume_file = resume_arg ? resume_arg : resume_file;
29 /* No resume device specified */
30 if (!resume_file[0])
31 return 0;
32
33 resume_arg = get_arg(argc, argv, "resume_offset=");
34 resume_offset = resume_arg ? strtoull(resume_arg, NULL, 0) : 0ULL;
35
36 /* Fix: we either should consider reverting the device back to
37 ordinary swap, or (better) put that code into swapon */
38 /* Noresume requested */
39 if (get_flag(argc, argv, "noresume"))
40 return 0;
41 return resume(resume_file, resume_offset);
42 }
43
44 int resume(const char *resume_file, unsigned long long resume_offset)
45 {
46 dev_t resume_device;
47 int powerfd = -1;
48 char device_string[64];
49 int len;
50
51 resume_device = name_to_dev_t(resume_file);
52
53 if (major(resume_device) == 0) {
54 fprintf(stderr, "Invalid resume device: %s\n", resume_file);
55 goto failure;
56 }
57
58 if ((powerfd = open("/sys/power/resume", O_WRONLY)) < 0)
59 goto fail_r;
60
61 len = snprintf(device_string, sizeof device_string,
62 "%u:%u:%llu",
63 major(resume_device), minor(resume_device),
64 resume_offset);
65
66 /* This should never happen */
67 if (len >= sizeof device_string)
68 goto fail_r;
69
70 dprintf("kinit: trying to resume from %s\n", resume_file);
71
72 if (write(powerfd, device_string, len) != len)
73 goto fail_r;
74
75 /* Okay, what are we still doing alive... */
76 failure:
77 if (powerfd >= 0)
78 close(powerfd);
79 dprintf("kinit: No resume image, doing normal boot...\n");
80 return -1;
81
82 fail_r:
83 fprintf(stderr, "Cannot write /sys/power/resume "
84 "(no software suspend kernel support?)\n");
85 goto failure;
86 }