rtcwake: improve read_clock_mode()
Make skipping two lines more robust, and add message about unexpected adjfile contents when running with --verbose. Reviewed-by: Karel Zak <kzak@redhat.com> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
5fde2f0d34
commit
3e5a54554d
|
@ -6,6 +6,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
/* default strtoxx_or_err() exit code */
|
/* default strtoxx_or_err() exit code */
|
||||||
#ifndef STRTOXX_EXIT_CODE
|
#ifndef STRTOXX_EXIT_CODE
|
||||||
|
@ -207,4 +208,6 @@ extern char *strnappend(const char *s, const char *suffix, size_t b);
|
||||||
extern char *strappend(const char *s, const char *suffix);
|
extern char *strappend(const char *s, const char *suffix);
|
||||||
extern const char *split(const char **state, size_t *l, const char *separator, int quoted);
|
extern const char *split(const char **state, size_t *l, const char *separator, int quoted);
|
||||||
|
|
||||||
|
extern int skip_fline(FILE *fp);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -859,6 +859,18 @@ const char *split(const char **state, size_t *l, const char *separator, int quot
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Rewind file pointer forward to new line. */
|
||||||
|
int skip_fline(FILE *fp)
|
||||||
|
{
|
||||||
|
char ch;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if ((ch = fgetc(fp)) == EOF)
|
||||||
|
return 1;
|
||||||
|
if (ch == '\n')
|
||||||
|
return 0;
|
||||||
|
} while (1);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef TEST_PROGRAM
|
#ifdef TEST_PROGRAM
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@
|
||||||
#define RTC_AF 0x20
|
#define RTC_AF 0x20
|
||||||
#define RTC_UF 0x10
|
#define RTC_UF 0x10
|
||||||
|
|
||||||
#define MAX_LINE 1024
|
#define ADJTIME_ZONE_STRLEN 8
|
||||||
|
|
||||||
#define RTC_PATH "/sys/class/rtc/%s/device/power/wakeup"
|
#define RTC_PATH "/sys/class/rtc/%s/device/power/wakeup"
|
||||||
#define SYS_POWER_STATE_PATH "/sys/power/state"
|
#define SYS_POWER_STATE_PATH "/sys/power/state"
|
||||||
|
@ -294,30 +294,23 @@ static void suspend_system(struct rtcwake_control *ctl, int suspend)
|
||||||
errx(EXIT_FAILURE, _("write error"));
|
errx(EXIT_FAILURE, _("write error"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int read_clock_mode(struct rtcwake_control *ctl)
|
static int read_clock_mode(struct rtcwake_control *ctl)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
char linebuf[MAX_LINE];
|
char linebuf[ADJTIME_ZONE_STRLEN];
|
||||||
|
|
||||||
fp = fopen(ctl->adjfile, "r");
|
fp = fopen(ctl->adjfile, "r");
|
||||||
if (!fp)
|
if (!fp)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* skip first line */
|
/* skip two lines */
|
||||||
if (!fgets(linebuf, MAX_LINE, fp)) {
|
if (skip_fline(fp) || skip_fline(fp)) {
|
||||||
fclose(fp);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* skip second line */
|
|
||||||
if (!fgets(linebuf, MAX_LINE, fp)) {
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read third line */
|
/* read third line */
|
||||||
if (!fgets(linebuf, MAX_LINE, fp)) {
|
if (!fgets(linebuf, sizeof linebuf, fp)) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -326,6 +319,8 @@ static int read_clock_mode(struct rtcwake_control *ctl)
|
||||||
ctl->clock_mode = CM_UTC;
|
ctl->clock_mode = CM_UTC;
|
||||||
else if (strncmp(linebuf, "LOCAL", 5) == 0)
|
else if (strncmp(linebuf, "LOCAL", 5) == 0)
|
||||||
ctl->clock_mode = CM_LOCAL;
|
ctl->clock_mode = CM_LOCAL;
|
||||||
|
else if (ctl->verbose)
|
||||||
|
warnx(_("unexpected third line in: %s: %s"), ctl->adjfile, linebuf);
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue