sleep.c revision 3ffd4af22052963e7a29431721ee204e634bea75
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
Copyright 2013 Zbigniew Jędrzejewski-Szmek
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include "sd-messages.h"
#include "def.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "sleep-config.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
static int write_mode(char **modes) {
int r = 0;
char **mode;
int k;
if (k == 0)
return 0;
log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
*mode);
if (r == 0)
r = k;
}
if (r < 0)
log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
return r;
}
char **state;
int r = 0;
int k;
k = write_string_stream(*f, *state, true);
if (k == 0)
return 0;
log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
*state);
if (r == 0)
r = k;
fclose(*f);
if (!*f)
}
return r;
}
char *arguments[] = {
NULL,
(char*) "pre",
};
int r;
/* This file is opened first, so that if we hit an error,
* we can abort before modifying any state. */
if (!f)
/* Configure the hibernation mode */
r = write_mode(modes);
if (r < 0)
return r;
LOG_MESSAGE("Suspending system..."),
"SLEEP=%s", arg_verb,
NULL);
r = write_state(&f, states);
if (r < 0)
return r;
LOG_MESSAGE("System resumed."),
"SLEEP=%s", arg_verb,
NULL);
return r;
}
static void help(void) {
printf("%s COMMAND\n\n"
"Suspend the system, hibernate the system, or both.\n\n"
"Commands:\n"
" -h --help Show this help and exit\n"
" --version Print version string and exit\n"
" suspend Suspend the system\n"
" hibernate Hibernate the system\n"
" hybrid-sleep Both hibernate and suspend the system\n"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch(c) {
case 'h':
help();
return 0; /* done */
case ARG_VERSION:
return version();
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Usage: %s COMMAND",
return -EINVAL;
}
return -EINVAL;
}
return 1 /* work to do */;
}
int r;
log_open();
if (r <= 0)
goto finish;
if (r < 0)
goto finish;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}