File: s2ram.c

package info (click to toggle)
uswsusp 1.0%2B20120915-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,548 kB
  • ctags: 1,422
  • sloc: ansic: 7,164; sh: 566; makefile: 222; perl: 65
file content (73 lines) | stat: -rw-r--r-- 1,306 bytes parent folder | download | duplicates (3)
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
/*
 * Suspend-to-RAM
 *
 * Copyright 2006 Pavel Machek <pavel@suse.cz>
 * Copyright 2011 Rodolfo García Peñas (kix) <kix@kix.es>
 * Distribute under GPLv2.
 */

#include <ctype.h>
#include "config.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#ifndef S2RAM
#define S2RAM
#endif
#include "vt.h"
#include "s2ram.h"
#include "config_parser.h"



int s2ram_check_kms(void)
{
	DIR *sysfs_dir;
	struct dirent *dentry;
	int ret = -ENOENT;

	sysfs_dir = opendir("/sys/class/drm");
	if (!sysfs_dir)
		return errno;

	while ((dentry = readdir(sysfs_dir))) {
		if (!strncmp(dentry->d_name, "card0-", 6)) {
			ret = 0;
			break;
		}
	}

	closedir(sysfs_dir);

	return ret;
}


/* Actually enter the suspend. May be ran on frozen system. */
int s2ram_generic_do(void)
{
	int ret = 0;
	FILE *f = fopen("/sys/power/state", "w");
	if (!f) {
		printf("/sys/power/state does not exist; what kind of ninja mutant machine is this?\n");
		return ENODEV;
	}
	if (fprintf(f, "mem") < 0) {
		ret = errno;
		perror("s2ram_do");
	}
	/* usually only fclose fails, not fprintf, so it does not matter
	 * that we might overwrite the previous error.
	 */
	if (fclose(f) < 0) {
		ret = errno;
		perror("s2ram_do");
	}
	return ret;
}