2008-08-01 12:07:56 +01:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
2008-05-08 22:29:38 -04:00
|
|
|
*
|
2008-11-11 10:13:24 -05:00
|
|
|
* Copyright (C) 2008 David Zeuthen <davidz@redhat.com>
|
2008-05-08 22:29:38 -04:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
# include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <pwd.h>
|
|
|
|
|
#include <grp.h>
|
2010-11-04 18:45:10 -04:00
|
|
|
#include <stdint.h>
|
2008-05-08 22:29:38 -04:00
|
|
|
#include <linux/fs.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
2009-03-30 14:39:08 +01:00
|
|
|
#include <glib.h>
|
2008-05-08 22:29:38 -04:00
|
|
|
|
|
|
|
|
#include "sysfs-utils.h"
|
|
|
|
|
|
2013-10-18 16:10:46 +02:00
|
|
|
double
|
|
|
|
|
sysfs_get_double_with_error (const char *dir, const char *attribute)
|
|
|
|
|
{
|
|
|
|
|
double result;
|
|
|
|
|
char *contents;
|
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
|
|
filename = g_build_filename (dir, attribute, NULL);
|
|
|
|
|
if (g_file_get_contents (filename, &contents, NULL, NULL)) {
|
2013-10-20 13:31:21 +02:00
|
|
|
result = g_ascii_strtod (contents, NULL);
|
2013-10-18 16:10:46 +02:00
|
|
|
g_free (contents);
|
|
|
|
|
} else {
|
|
|
|
|
result = -1.0;
|
|
|
|
|
}
|
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-08 22:29:38 -04:00
|
|
|
double
|
|
|
|
|
sysfs_get_double (const char *dir, const char *attribute)
|
|
|
|
|
{
|
2008-08-01 12:07:56 +01:00
|
|
|
double result;
|
|
|
|
|
char *contents;
|
|
|
|
|
char *filename;
|
2008-05-08 22:29:38 -04:00
|
|
|
|
2008-08-01 12:07:56 +01:00
|
|
|
result = 0.0;
|
|
|
|
|
filename = g_build_filename (dir, attribute, NULL);
|
|
|
|
|
if (g_file_get_contents (filename, &contents, NULL, NULL)) {
|
2013-10-20 13:31:21 +02:00
|
|
|
result = g_ascii_strtod (contents, NULL);
|
2008-08-01 12:07:56 +01:00
|
|
|
g_free (contents);
|
|
|
|
|
}
|
|
|
|
|
g_free (filename);
|
2008-05-08 22:29:38 -04:00
|
|
|
|
|
|
|
|
|
2008-08-01 12:07:56 +01:00
|
|
|
return result;
|
2008-05-08 22:29:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
sysfs_get_string (const char *dir, const char *attribute)
|
|
|
|
|
{
|
2008-08-01 12:07:56 +01:00
|
|
|
char *result;
|
|
|
|
|
char *filename;
|
2008-05-08 22:29:38 -04:00
|
|
|
|
2008-08-01 12:07:56 +01:00
|
|
|
result = NULL;
|
|
|
|
|
filename = g_build_filename (dir, attribute, NULL);
|
|
|
|
|
if (!g_file_get_contents (filename, &result, NULL, NULL)) {
|
|
|
|
|
result = g_strdup ("");
|
|
|
|
|
}
|
|
|
|
|
g_free (filename);
|
2008-05-08 22:29:38 -04:00
|
|
|
|
2008-08-01 12:07:56 +01:00
|
|
|
return result;
|
2008-05-08 22:29:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
sysfs_get_int (const char *dir, const char *attribute)
|
|
|
|
|
{
|
2008-08-01 12:07:56 +01:00
|
|
|
int result;
|
|
|
|
|
char *contents;
|
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
|
filename = g_build_filename (dir, attribute, NULL);
|
|
|
|
|
if (g_file_get_contents (filename, &contents, NULL, NULL)) {
|
|
|
|
|
result = atoi (contents);
|
|
|
|
|
g_free (contents);
|
|
|
|
|
}
|
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
|
|
return result;
|
2008-07-24 17:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
sysfs_get_bool (const char *dir, const char *attribute)
|
|
|
|
|
{
|
2013-10-17 22:27:36 +02:00
|
|
|
gboolean result;
|
2008-08-01 12:07:56 +01:00
|
|
|
char *contents;
|
|
|
|
|
char *filename;
|
|
|
|
|
|
2013-10-17 22:27:36 +02:00
|
|
|
result = FALSE;
|
2008-08-01 12:07:56 +01:00
|
|
|
filename = g_build_filename (dir, attribute, NULL);
|
|
|
|
|
if (g_file_get_contents (filename, &contents, NULL, NULL)) {
|
2009-04-01 09:14:45 +01:00
|
|
|
g_strdelimit (contents, "\n", '\0');
|
2013-10-17 22:27:36 +02:00
|
|
|
result = (g_strcmp0 (contents, "1") == 0);
|
2008-08-01 12:07:56 +01:00
|
|
|
g_free (contents);
|
|
|
|
|
}
|
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
|
|
return result;
|
2008-05-08 22:29:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
sysfs_file_exists (const char *dir, const char *attribute)
|
|
|
|
|
{
|
2008-08-01 12:07:56 +01:00
|
|
|
gboolean result;
|
|
|
|
|
char *filename;
|
2008-05-08 22:29:38 -04:00
|
|
|
|
2008-08-01 12:07:56 +01:00
|
|
|
result = FALSE;
|
|
|
|
|
filename = g_build_filename (dir, attribute, NULL);
|
|
|
|
|
if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
|
|
|
|
|
result = TRUE;
|
|
|
|
|
}
|
|
|
|
|
g_free (filename);
|
2008-05-08 22:29:38 -04:00
|
|
|
|
2008-08-01 12:07:56 +01:00
|
|
|
return result;
|
2008-05-08 22:29:38 -04:00
|
|
|
}
|