summaryrefslogtreecommitdiff
path: root/src/sysfs-utils.c
blob: 4784439b78db9d4846cbdf332c6ae8d54e179346 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 David Zeuthen <davidz@redhat.com>
 *
 * 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>
#include <linux/fs.h>
#include <sys/ioctl.h>

#include "sysfs-utils.h"

double
sysfs_get_double (const char *dir, const char *attribute)
{
	double result;
	char *contents;
	char *filename;

	result = 0.0;
	filename = g_build_filename (dir, attribute, NULL);
	if (g_file_get_contents (filename, &contents, NULL, NULL)) {
		result = atof (contents);
		g_free (contents);
	}
	g_free (filename);


	return result;
}

gboolean
sysfs_file_contains (const char *dir, const char *attribute, const char *string)
{
	gboolean result;
	char *filename;
	char *s;

	result = FALSE;

	filename = g_build_filename (dir, attribute, NULL);
	if (g_file_get_contents (filename, &s, NULL, NULL)) {
		result = (strstr(s, string) != NULL);
		g_free (s);
	}
	g_free (filename);

	return result;
}

char *
sysfs_get_string (const char *dir, const char *attribute)
{
	char *result;
	char *filename;

	result = NULL;
	filename = g_build_filename (dir, attribute, NULL);
	if (!g_file_get_contents (filename, &result, NULL, NULL)) {
		result = g_strdup ("");
	}
	g_free (filename);

	return result;
}

int
sysfs_get_int (const char *dir, const char *attribute)
{
	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;
}

guint
sysfs_get_hex (const char *dir, const char *attribute)
{
	guint result;
	char *contents;
	char *filename;

	result = 0;
	filename = g_build_filename (dir, attribute, NULL);
	if (g_file_get_contents (filename, &contents, NULL, NULL)) {
		result = strtol (contents, (char **) NULL, 16);
		g_free (contents);
	}
	g_free (filename);

	return result;
}

gboolean
sysfs_get_bool (const char *dir, const char *attribute)
{
	gboolean result = FALSE;
	char *contents;
	char *filename;

	result = 0;
	filename = g_build_filename (dir, attribute, NULL);
	if (g_file_get_contents (filename, &contents, NULL, NULL)) {
		if (strcmp (contents, "1"))
			result = TRUE;
		g_free (contents);
	}
	g_free (filename);

	return result;
}

guint64
sysfs_get_uint64 (const char *dir, const char *attribute)
{
	guint64 result;
	char *contents;
	char *filename;

	result = 0;
	filename = g_build_filename (dir, attribute, NULL);
	if (g_file_get_contents (filename, &contents, NULL, NULL)) {
		result = atoll (contents);
		g_free (contents);
	}
	g_free (filename);


	return result;
}

gboolean
sysfs_file_exists (const char *dir, const char *attribute)
{
	gboolean result;
	char *filename;

	result = FALSE;
	filename = g_build_filename (dir, attribute, NULL);
	if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
		result = TRUE;
	}
	g_free (filename);

	return result;
}

char *
_dupv8 (const char *s)
{
	const char *end_valid;

	if (!g_utf8_validate (s,
			     -1,
			     &end_valid)) {
		g_warning ("The string '%s' is not valid UTF-8. Invalid characters begins at '%s'", s, end_valid);
		return g_strndup (s, end_valid - s);
	} else {
		return g_strdup (s);
	}
}

char *
sysfs_resolve_link (const char *dir, const char *attribute)
{
	char *full_path;
	char link_path[PATH_MAX];
	char resolved_path[PATH_MAX];
	ssize_t num;
	gboolean found_it;

	found_it = FALSE;

	full_path = g_build_filename (dir, attribute, NULL);

	//egg_warning ("attribute='%s'", attribute);
	//egg_warning ("full_path='%s'", full_path);
	num = readlink (full_path, link_path, sizeof (link_path) - 1);
	if (num != -1) {
		char *absolute_path;

		link_path[num] = '\0';

		//egg_warning ("link_path='%s'", link_path);
		absolute_path = g_build_filename (dir, link_path, NULL);
		//egg_warning ("absolute_path='%s'", absolute_path);
		if (realpath (absolute_path, resolved_path) != NULL) {
			//egg_warning ("resolved_path='%s'", resolved_path);
			found_it = TRUE;
		}
		g_free (absolute_path);
	}
	g_free (full_path);

	if (found_it)
		return g_strdup (resolved_path);
	else
		return NULL;
}