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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/***************************************************************************
* CVSID: $Id$
*
* mmap_cache.c : FDI cache routines.
*
* Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
* Copyright (C) 2006 Kay Sievers, <kay.sievers@vrfy.org>
* Copyright (C) 2006 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2007 Mikhail Kshevetskiy <mikhail.kshevetskiy@gmail.com>
* Copyright (C) 2007 Sergey Lapin <slapinid@gmail.com>
*
* Licensed under the Academic Free License version 2.1
*
* 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
*
**************************************************************************/
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
#include "logger.h"
#include "rule.h"
#include "mmap_cache.h"
#include "hald_runner.h"
#include "hal-file-monitor.h"
#include "osspec.h"
extern void *rules_ptr;
static size_t rules_size = 0;
int di_rules_init (void)
{
struct cache_header *header;
char *cachename;
int fd;
struct stat statbuf;
cachename = getenv ("HAL_FDI_CACHE_NAME");
if(cachename == NULL)
cachename = HALD_CACHE_FILE;
if (rules_ptr != NULL) {
HAL_INFO (("Unmapping old cache file"));
munmap (rules_ptr, rules_size);
}
if((fd = open (cachename, O_RDONLY)) < 0)
DIE(("Unable to open cache %s\n", cachename));
if(fstat (fd,&statbuf) < 0)
DIE(("Unable to stat cache %s\n", cachename));
rules_size = statbuf.st_size;
rules_ptr = mmap (NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if(rules_ptr == MAP_FAILED)
DIE (("Couldn't mmap file '%s', errno=%d: %s", cachename, errno, strerror (errno)));
header = (struct cache_header*) rules_ptr;
HAL_INFO(("preprobe: offset=%08lx, size=%d", header->fdi_rules_preprobe,
header->fdi_rules_information - header->fdi_rules_preprobe));
HAL_INFO(("information: offset=%08lx, size=%d", header->fdi_rules_information,
header->fdi_rules_policy - header->fdi_rules_information));
HAL_INFO(("policy: offset=%08lx, size=%d", header->fdi_rules_policy,
header->all_rules_size - header->fdi_rules_policy));
close(fd);
return 0;
}
static gboolean regen_cache_done;
static gint regen_cache_success;
static void
regen_cache_cb (HalDevice *d,
guint32 exit_type,
gint return_code,
gchar **error,
gpointer data1,
gpointer data2)
{
HAL_INFO (("In regen_cache_cb exit_type=%d, return_code=%d", exit_type, return_code));
/* see create_cache.c - rc==0 means success - rc==2 means "success, but some fdi files skipped" */
if (exit_type == HALD_RUN_SUCCESS && ( return_code == 0 || return_code == 2)) {
regen_cache_success = TRUE;
} else {
regen_cache_success = FALSE;
}
regen_cache_done = TRUE;
}
static void
regen_cache (void)
{
int n, m;
char *extra_env[5] = {NULL, NULL, NULL, NULL, NULL};
char *env_names[5] = {"HAL_FDI_SOURCE_PREPROBE",
"HAL_FDI_SOURCE_INFORMATION",
"HAL_FDI_SOURCE_POLICY",
"HAL_FDI_CACHE_NAME",
NULL};
HAL_INFO (("Regenerating fdi cache.."));
/* pass these variables to the helper */
for (n = 0, m = 0; env_names[n] != NULL; n++) {
char *name;
char *val;
name = env_names[n];
val = getenv(name);
if (val != NULL) {
extra_env [m++] = g_strdup_printf ("%s=%s", name, val);
}
}
regen_cache_done = FALSE;
hald_runner_run_sync (NULL,
"hald-generate-fdi-cache",
extra_env,
60000,
regen_cache_cb,
NULL,
NULL);
for (n = 0; extra_env[n] != NULL; n++) {
g_free (extra_env[n]);
}
if (!regen_cache_success) {
HAL_ERROR (("fdi cache regeneration failed!"));
}
HAL_INFO (("fdi cache generation done"));
}
static gboolean cache_valid = FALSE;
static void
cache_invalidated (HalFileMonitor *monitor,
HalFileMonitorEvent event,
const char *path,
gpointer user_data)
{
HAL_INFO (("dir '%s' changed - marking fdi cache as invalid", path));
cache_valid = FALSE;
}
static void
dir_mtime (const char *path, time_t *mt, gboolean setup_watches)
{
struct dirent **namelist;
struct stat st;
int n;
if (setup_watches) {
HalFileMonitor *file_monitor;
file_monitor = osspec_get_file_monitor ();
if (file_monitor != NULL) {
hal_file_monitor_add_notify (file_monitor,
path,
HAL_FILE_MONITOR_EVENT_CREATE|
HAL_FILE_MONITOR_EVENT_DELETE|
HAL_FILE_MONITOR_EVENT_CHANGE,
cache_invalidated,
NULL);
}
}
if (!stat(path, &st)) {
if(st.st_mtime > *mt)
*mt = st.st_mtime;
if(S_ISDIR(st.st_mode)) {
n = scandir(path, &namelist, 0, alphasort);
if (n < 0)
return;
else {
while(n--) {
gchar *cpath;
cpath = g_build_filename(path, namelist[n]->d_name, NULL);
if(namelist[n]->d_name[0] != '.')
dir_mtime(cpath, mt, setup_watches);
free(namelist[n]);
g_free(cpath);
}
free(namelist);
}
}
}
}
gboolean
di_cache_coherency_check (gboolean setup_watches)
{
char *hal_fdi_source_preprobe;
char *hal_fdi_source_information;
char *hal_fdi_source_policy;
char *cachename;
time_t mt;
struct stat st;
gboolean did_regen;
if (cache_valid)
return FALSE;
did_regen = FALSE;
mt = 0;
hal_fdi_source_preprobe = getenv ("HAL_FDI_SOURCE_PREPROBE");
hal_fdi_source_information = getenv ("HAL_FDI_SOURCE_INFORMATION");
hal_fdi_source_policy = getenv ("HAL_FDI_SOURCE_POLICY");
if (hal_fdi_source_preprobe != NULL) {
dir_mtime (hal_fdi_source_preprobe, &mt, setup_watches);
} else {
dir_mtime (PACKAGE_DATA_DIR "/hal/fdi/preprobe", &mt, setup_watches);
dir_mtime (PACKAGE_SYSCONF_DIR "/hal/fdi/preprobe", &mt, setup_watches);
}
if (hal_fdi_source_information != NULL) {
dir_mtime (hal_fdi_source_information, &mt, setup_watches);
} else {
dir_mtime (PACKAGE_DATA_DIR "/hal/fdi/information", &mt, setup_watches);
dir_mtime (PACKAGE_SYSCONF_DIR "/hal/fdi/information", &mt, setup_watches);
}
if (hal_fdi_source_policy != NULL) {
dir_mtime (hal_fdi_source_policy, &mt, setup_watches);
} else {
dir_mtime (PACKAGE_DATA_DIR "/hal/fdi/policy", &mt, setup_watches);
dir_mtime (PACKAGE_SYSCONF_DIR "/hal/fdi/policy", &mt, setup_watches);
}
cachename = getenv ("HAL_FDI_CACHE_NAME");
if(cachename == NULL)
cachename = HALD_CACHE_FILE;
if(stat (cachename, &st) == 0) {
if(st.st_mtime < mt) {
HAL_INFO(("Cache needs update"));
regen_cache();
did_regen = TRUE;
}
} else {
regen_cache();
did_regen = TRUE;
}
HAL_INFO(("cache mtime is %d",mt));
cache_valid = TRUE;
return did_regen;
}
|