]> git.wh0rd.org Git - patches.git/blob - hal-use-libpci.patch
initial import
[patches.git] / hal-use-libpci.patch
1 diff --git a/configure.in b/configure.in
2 index 0b6734c..647c0c6 100644
3 --- a/configure.in
4 +++ b/configure.in
5 @@ -46,20 +46,10 @@ AC_ARG_WITH([pid-file],
6             AS_HELP_STRING([--with-pid-file=<file>],
7                            [PID file for HAL daemon]))
8  
9 -AC_ARG_WITH([hwdata],
10 -           AS_HELP_STRING([--with-hwdata=<dir>],
11 -                          [Where PCI and USB IDs are found]))
12 -AC_ARG_WITH([pci-ids],
13 -           AS_HELP_STRING([--with-pci-ids=<dir>],
14 -                          [Where PCI IDs are found (overrides --with-hwdata)]))
15  AC_ARG_WITH([usb-ids],
16             AS_HELP_STRING([--with-usb-ids=<dir>],
17                            [Where USB IDs are found (overrides --with-hwdata)]))
18  
19 -AC_ARG_ENABLE([pci-ids],
20 -             AS_HELP_STRING([--disable-pci-ids],
21 -                            [Do not build with PCI IDs support]),
22 -             [enable_pci_ids=$enableval], [enable_pci_ids=yes])
23  AC_ARG_ENABLE([usb-ids],
24               AS_HELP_STRING([--disable-usb-ids],
25                              [Do not build with USB IDs support]),
26 @@ -73,34 +63,10 @@ AC_ARG_WITH([socket-dir],
27             AS_HELP_STRING([--with-socket-dir=<dir>],
28                            [Location of the HAL D-BUS listening sockets (auto)]))
29  
30 -if ! test -z "$with_hwdata" ; then
31 -  PCI_IDS_DIR="$with_hwdata"
32 -  USB_IDS_DIR="$with_hwdata"
33 -fi
34 -if ! test -z "$with_pci_ids" ; then
35 -  PCI_IDS_DIR="$with_pci_ids"
36 -fi
37  if ! test -z "$with_usb_ids" ; then
38    USB_IDS_DIR="$with_usb_ids"
39  fi
40  
41 -if test "x$enable_pci_ids" = "xno" ; then
42 -  USE_PCI_IDS=no
43 -else
44 -   if test -z "$PCI_IDS_DIR"; then
45 -     for dir in /usr/share/hwdata /usr/share/misc /usr/share /var/lib/misc; do
46 -       AC_CHECK_FILE([$dir/pci.ids], [PCI_IDS_DIR=$dir])
47 -     done
48 -     if test -z "$PCI_IDS_DIR"; then
49 -       AC_MSG_ERROR([cannot find pci.ids. Use --with-pci-ids to specify location])
50 -     else
51 -       AC_MSG_WARN([autodetected pci.ids in $PCI_IDS_DIR])
52 -     fi
53 -   fi
54 -   USE_PCI_IDS=yes
55 -   AC_DEFINE([USE_PCI_IDS], [1], [Whether pci.ids is to be used])
56 -fi
57 -
58  if test "x$enable_usb_ids" = "xno" ; then
59    USE_USB_IDS=no
60  else 
61 @@ -125,9 +91,7 @@ else
62    AC_DEFINE([USE_PNP_IDS], [1], [Whether builtin PNP IDs are to be used])
63  fi
64  
65 -AC_SUBST(PCI_IDS_DIR)
66  AC_SUBST(USB_IDS_DIR)
67 -AC_SUBST(USE_PCI_IDS)
68  AC_SUBST(USE_USB_IDS)
69  AC_SUBST(USE_PNP_IDS)
70  
71 @@ -422,11 +386,13 @@ AC_ARG_WITH([libpci],
72             [], [with_libpci=yes])
73  
74  USE_LIBPCI="no"
75 +PCI_LIBS=""
76  if test "x$with_libpci" != xno ; then
77    dnl check for libpci
78    AC_CHECK_HEADERS([pci/pci.h],
79 -                   [AC_CHECK_LIB([pci], [pci_init], [USE_LIBPCI="yes"], [], [-lz])])
80 -fi 
81 +                   [AC_CHECK_LIB([pci], [pci_init], [USE_LIBPCI="yes" PCI_LIBS="-lpci -lz"], [], [-lz])])
82 +fi
83 +AC_SUBST([PCI_LIBS])
84  AM_CONDITIONAL([HAVE_LIBPCI], [test "x$USE_LIBPCI" = "xyes"])
85  
86  AC_ARG_WITH([backend],
87 @@ -979,7 +945,6 @@ echo "
88          localstatedir:               ${localstatedir}
89          docdir:                      ${docdir}
90          dbus-1 system.d dir:         ${DBUS_SYS_DIR}
91 -        pci.ids dir:                 ${PCI_IDS_DIR}
92          usb.ids dir:                 ${USB_IDS_DIR}
93  
94          compiler:                    ${CC}
95 diff --git a/hald/Makefile.am b/hald/Makefile.am
96 index ea396f6..fbf2d5d 100644
97 --- a/hald/Makefile.am
98 +++ b/hald/Makefile.am
99 @@ -12,7 +12,6 @@ AM_CPPFLAGS = \
100         -DPACKAGE_SCRIPT_DIR=\""$(libdir)/hal/scripts"\" \
101         -DHALD_SOCKET_DIR=\""$(HALD_SOCKET_DIR)"\" \
102         -DHALD_PID_FILE=\""$(HALD_PID_FILE)"\" \
103 -       -DPCI_IDS_DIR=\""$(PCI_IDS_DIR)"\" \
104         -DUSB_IDS_DIR=\""$(USB_IDS_DIR)"\" \
105         -I$(top_srcdir) \
106         @GLIB_CFLAGS@ @DBUS_CFLAGS@ @POLKIT_CFLAGS@
107 @@ -73,7 +72,7 @@ if HAVE_CONKIT
108  hald_SOURCES += ck-tracker.h ck-tracker.c
109  endif
110  
111 -hald_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ @POLKIT_LIBS@ -lm @HALD_OS_LIBS@ $(top_builddir)/hald/$(HALD_BACKEND)/libhald_$(HALD_BACKEND).la
112 +hald_LDADD = @PCI_LIBS@ @GLIB_LIBS@ @DBUS_LIBS@ @POLKIT_LIBS@ -lm @HALD_OS_LIBS@ $(top_builddir)/hald/$(HALD_BACKEND)/libhald_$(HALD_BACKEND).la
113  
114  #### Init scripts fun
115  SCRIPT_IN_FILES=haldaemon.in
116 diff --git a/hald/ids.c b/hald/ids.c
117 index 9949eab..b8dc344 100644
118 --- a/hald/ids.c
119 +++ b/hald/ids.c
120 @@ -44,72 +44,17 @@
121  
122  #include "ids.h"
123  
124 -#ifdef USE_PCI_IDS
125 -/** Pointer to where the pci.ids file is loaded */
126 -static char *pci_ids = NULL;
127 -
128 -/** Length of data store at at pci_ids */
129 -static size_t pci_ids_len;
130 +#ifdef HAVE_PCI
131  
132 -/** Iterator position into pci_ids */
133 -static size_t pci_ids_iter_pos;
134 +#include <pci/pci.h>
135  
136 -/** Initialize the pci.ids line iterator to the beginning of the file */
137 -static void
138 -pci_ids_line_iter_init ()
139 -{
140 -       pci_ids_iter_pos = 0;
141 -}
142 +/** Pointer to where the pci.ids file is loaded */
143 +static struct pci_access *pacc = NULL;
144  
145  /** Maximum length of lines in pci.ids */
146  #define PCI_IDS_MAX_LINE_LEN 512
147  
148 -/**  
149 - *  pci_ids_line_iter_get_line:
150 - *  @line_len:           Pointer to where number of bytes in line will
151 - *                       be stored
152 - *  Returns:             Pointer to the line; only valid until the
153 - *                       next invocation of this function
154 - *
155 - *  Get the next line from pci.ids
156 - */
157 -static char *
158 -pci_ids_line_iter_get_line (unsigned int *line_len)
159 -{
160 -       unsigned int i;
161 -       static char line[PCI_IDS_MAX_LINE_LEN];
162 -
163 -       for (i = 0;
164 -            pci_ids_iter_pos < pci_ids_len &&
165 -            i < PCI_IDS_MAX_LINE_LEN - 1 &&
166 -            pci_ids[pci_ids_iter_pos] != '\n'; i++, pci_ids_iter_pos++) {
167 -               line[i] = pci_ids[pci_ids_iter_pos];
168 -       }
169 -
170 -       line[i] = '\0';
171 -       if (line_len != NULL)
172 -               *line_len = i;
173 -
174 -       pci_ids_iter_pos++;
175 -
176 -       return line;
177 -}
178 -
179 -/** 
180 - *  pci_ids_line_iter_has_more:
181 - *
182 - *  Returns:              #TRUE iff there are more lines to process
183 - *
184 - *  See if there are more lines to process in pci.ids 
185 - */
186 -static dbus_bool_t
187 -pci_ids_line_iter_has_more ()
188 -{
189 -       return pci_ids_iter_pos < pci_ids_len;
190 -}
191 -
192 -
193 -/** 
194 +/**
195   *  ids_find_pci:
196   *  @vendor_id:           PCI vendor id or 0 if unknown
197   *  @product_id:          PCI product id or 0 if unknown
198 @@ -131,199 +76,64 @@ ids_find_pci (int vendor_id, int product_id,
199               char **vendor_name, char **product_name,
200               char **subsys_vendor_name, char **subsys_product_name)
201  {
202 -       char *line;
203 -       unsigned int i;
204 -       unsigned int line_len;
205 -       unsigned int num_tabs;
206 -       char rep_vi[8];
207 -       char rep_pi[8];
208 -       char rep_svi[8];
209 -       char rep_spi[8];
210 -       dbus_bool_t vendor_matched = FALSE;
211 -       dbus_bool_t product_matched = FALSE;
212         static char store_vn[PCI_IDS_MAX_LINE_LEN];
213         static char store_pn[PCI_IDS_MAX_LINE_LEN];
214         static char store_svn[PCI_IDS_MAX_LINE_LEN];
215         static char store_spn[PCI_IDS_MAX_LINE_LEN];
216  
217 -       snprintf (rep_vi, 8, "%04x", vendor_id);
218 -       snprintf (rep_pi, 8, "%04x", product_id);
219 -       snprintf (rep_svi, 8, "%04x", subsys_vendor_id);
220 -       snprintf (rep_spi, 8, "%04x", subsys_product_id);
221 -
222         *vendor_name = NULL;
223         *product_name = NULL;
224         *subsys_vendor_name = NULL;
225         *subsys_product_name = NULL;
226  
227 -       for (pci_ids_line_iter_init (); pci_ids_line_iter_has_more ();) {
228 -               line = pci_ids_line_iter_get_line (&line_len);
229 -
230 -               /* skip lines with no content */
231 -               if (line_len < 4)
232 -                       continue;
233 -
234 -               /* skip comments */
235 -               if (line[0] == '#')
236 -                       continue;
237 -
238 -               /* count number of tabs */
239 -               num_tabs = 0;
240 -               for (i = 0; i < line_len; i++) {
241 -                       if (line[i] != '\t')
242 -                               break;
243 -                       num_tabs++;
244 -               }
245 -
246 -               switch (num_tabs) {
247 -               case 0:
248 -                       /* vendor names */
249 -                       vendor_matched = FALSE;
250 -
251 -                       /* first check subsys_vendor_id, if haven't done 
252 -                        * already */
253 -                       if (*subsys_vendor_name == NULL
254 -                           && subsys_vendor_id != 0) {
255 -                               if ((*((dbus_uint32_t *) line)) ==
256 -                                   (*((dbus_uint32_t *) rep_svi))) {
257 -                                       /* found it */
258 -                                       for (i = 4; i < line_len; i++) {
259 -                                               if (!isspace (line[i]))
260 -                                                       break;
261 -                                       }
262 -                                       strncpy (store_svn, line + i,
263 -                                                PCI_IDS_MAX_LINE_LEN);
264 -                                       *subsys_vendor_name = store_svn;
265 -                               }
266 -                       }
267 -
268 -                       /* check vendor_id */
269 -                       if (vendor_id != 0) {
270 -                               if (memcmp (line, rep_vi, 4) == 0) {
271 -                                       /* found it */
272 -                                       vendor_matched = TRUE;
273 -
274 -                                       for (i = 4; i < line_len; i++) {
275 -                                               if (!isspace (line[i]))
276 -                                                       break;
277 -                                       }
278 -                                       strncpy (store_vn, line + i,
279 -                                                PCI_IDS_MAX_LINE_LEN);
280 -                                       *vendor_name = store_vn;
281 -                               }
282 -                       }
283 -
284 -                       break;
285 -
286 -               case 1:
287 -                       product_matched = FALSE;
288 -
289 -                       /* product names */
290 -                       if (!vendor_matched)
291 -                               continue;
292 -
293 -                       /* check product_id */
294 -                       if (product_id != 0) {
295 -                               if (memcmp (line + 1, rep_pi, 4) == 0) {
296 -                                       /* found it */
297 -
298 -                                       product_matched = TRUE;
299 -
300 -                                       for (i = 5; i < line_len; i++) {
301 -                                               if (!isspace (line[i]))
302 -                                                       break;
303 -                                       }
304 -                                       strncpy (store_pn, line + i,
305 -                                                PCI_IDS_MAX_LINE_LEN);
306 -                                       *product_name = store_pn;
307 -                               }
308 -                       }
309 -                       break;
310 -
311 -               case 2:
312 -                       /* subsystem_vendor subsystem_product */
313 -                       if (!vendor_matched || !product_matched)
314 -                               continue;
315 -
316 -                       /* check product_id */
317 -                       if (subsys_vendor_id != 0
318 -                           && subsys_product_id != 0) {
319 -                               if (memcmp (line + 2, rep_svi, 4) == 0
320 -                                   && memcmp (line + 7, rep_spi,
321 -                                              4) == 0) {
322 -                                       /* found it */
323 -                                       for (i = 11; i < line_len; i++) {
324 -                                               if (!isspace (line[i]))
325 -                                                       break;
326 -                                       }
327 -                                       strncpy (store_spn, line + i,
328 -                                                PCI_IDS_MAX_LINE_LEN);
329 -                                       *subsys_product_name = store_spn;
330 -                               }
331 -                       }
332 -
333 -                       break;
334 -
335 -               default:
336 -                       break;
337 -               }
338 -
339 -       }
340 +       if (vendor_id == 0)
341 +               return;
342 +       *vendor_name = pci_lookup_name (pacc, store_vn, sizeof(store_vn),
343 +               PCI_LOOKUP_VENDOR, vendor_id, product_id);
344 +       if (*vendor_name == NULL)
345 +               return;
346 +
347 +       if (product_id == 0)
348 +               return;
349 +       *product_name = pci_lookup_name (pacc, store_pn, sizeof(store_pn),
350 +               PCI_LOOKUP_DEVICE, vendor_id, product_id);
351 +       if (*product_name == NULL)
352 +               return;
353 +
354 +       if (subsys_vendor_id == 0)
355 +               return;
356 +       *subsys_vendor_name = pci_lookup_name (pacc, store_svn, sizeof(store_svn),
357 +               PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, subsys_vendor_id);
358 +       if (*subsys_vendor_name == NULL)
359 +               return;
360 +
361 +       if (subsys_product_id == 0)
362 +               return;
363 +       *subsys_product_name = pci_lookup_name (pacc, store_spn, sizeof(store_spn),
364 +               PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, subsys_vendor_id, subsys_product_id);
365 +       if (*subsys_product_name == NULL)
366 +               return;
367  }
368  
369 -
370 -/**  
371 - *  pci_ids_load:
372 - *  @path:               Path of the pci.ids file, e.g. /usr/share/hwdata/pci.ids
373 - *  
374 - *  Returns:             #TRUE if the file was succesfully loaded
375 - *
376 - *  Load the PCI database used for mapping vendor, product, subsys_vendor
377 - *  and subsys_product numbers into names.
378 - */
379 -static dbus_bool_t
380 -pci_ids_load (const char *path)
381 +static void
382 +pci_ids_error (char *msg, ...)
383  {
384 -       int fd;
385 -       struct stat statbuf;
386 -       gboolean ret;
387 -
388 -       ret = FALSE;
389 -
390 -       if (stat (path, &statbuf) != 0) {
391 -               HAL_WARNING (("Couldn't stat pci.ids file '%s', errno=%d: %s", path, errno, strerror (errno)));
392 -               goto out;
393 -       }
394 -       pci_ids_len = statbuf.st_size;
395 -
396 -       fd = open (path, O_RDONLY);
397 -       if (fd < 0) {
398 -               HAL_WARNING (("Couldn't open pci.ids file '%s', errno=%d: %s", path, errno, strerror (errno)));
399 -               goto out;
400 -       }
401 -
402 -       pci_ids = mmap (NULL, pci_ids_len, PROT_READ, MAP_SHARED, fd, 0);
403 -       if (pci_ids == MAP_FAILED) {
404 -               HAL_WARNING (("Couldn't mmap pci.ids file '%s', errno=%d: %s", path, errno, strerror (errno)));
405 -               close (fd);
406 -               goto out;
407 -       }
408 -
409 -       ret = TRUE;
410 -
411 -       close (fd);
412 -out:
413 -       return ret;
414 +       va_list args;
415 +       va_start (args, msg);
416 +       logger_setup (HAL_LOGPRI_ERROR, __FILE__, __LINE__, __FUNCTION__);
417 +       logger_emit_var (msg, args);
418 +       va_end (args);
419  }
420  
421  void
422  pci_ids_init (void)
423  {
424 -       /* Load /usr/share/hwdata/pci.ids */
425 -       pci_ids_load (PCI_IDS_DIR "/pci.ids");
426 +       pacc = pci_alloc();
427 +       pacc->error = pci_ids_error;
428 +       pci_init(pacc);
429  }
430  
431 -#endif /*USE_PCI_IDS*/
432 +#endif /*HAVE_PCI*/
433  
434  /*==========================================================================*/
435  
436 diff --git a/hald/ids.h b/hald/ids.h
437 index 7773211..e0a8679 100644
438 --- a/hald/ids.h
439 +++ b/hald/ids.h
440 @@ -28,7 +28,7 @@
441  
442  #include <glib.h>
443  
444 -#ifdef USE_PCI_IDS
445 +#ifdef HAVE_PCI
446  
447  void pci_ids_init (void);
448  
449 @@ -38,7 +38,7 @@ ids_find_pci (int vendor_id, int product_id,
450               char **vendor_name, char **product_name,
451               char **subsys_vendor_name, char **subsys_product_name);
452  
453 -#else /*USE_PCI_IDS*/
454 +#else /*HAVE_PCI*/
455  static inline void pci_ids_init (void) {return;};
456  
457  static inline void
458 @@ -46,7 +46,7 @@ ids_find_pci (int vendor_id, int product_id,
459               int subsys_vendor_id, int subsys_product_id,
460               char **vendor_name, char **product_name,
461               char **subsys_vendor_name, char **subsys_product_name) {return;}
462 -#endif /*USE_PCI_IDS*/
463 +#endif /*HAVE_PCI*/
464  
465  #ifdef USE_PNP_IDS
466  
467 diff --git a/hald/logger.c b/hald/logger.c
468 index a2d04b5..c23ea71 100644
469 --- a/hald/logger.c
470 +++ b/hald/logger.c
471 @@ -138,16 +138,15 @@ logger_setup (int _priority, const char *_file, int _line, const char *_function
472  }
473  
474  /** 
475 - *  logger_emit:
476 + *  logger_emit_var:
477   *  @format:             Message format string, printf style
478 - *  @...:                Parameters for message, printf style
479 + *  @args:               Parameters for message, vprintf style
480   *
481   *  Emit logging entry 
482   */
483  void
484 -logger_emit (const char *format, ...)
485 +logger_emit_var (const char *format, va_list args)
486  {
487 -       va_list args;
488         char buf[512];
489         char *pri;
490         char tbuf[256];
491 @@ -160,7 +159,6 @@ logger_emit (const char *format, ...)
492         if (!is_enabled)
493                 return;
494  
495 -       va_start (args, format);
496         vsnprintf (buf, sizeof (buf), format, args);
497  
498         switch (priority) {
499 @@ -213,7 +211,21 @@ logger_emit (const char *format, ...)
500                                 break;
501                 }
502         }
503 +}
504  
505 +/**
506 + *  loger_emit:
507 + *  @format:             Message format string, printf style
508 + *  @...:                Parameters for message, printf style
509 + *
510 + *  Emit logging entry
511 + */
512 +void
513 +logger_emit (const char *format, ...)
514 +{
515 +       va_list args;
516 +       va_start (args, format);
517 +       logger_emit_var (format, args);
518         va_end (args);
519  }
520  
521 diff --git a/hald/logger.h b/hald/logger.h
522 index f32216c..dd55eb5 100644
523 --- a/hald/logger.h
524 +++ b/hald/logger.h
525 @@ -30,6 +30,7 @@ extern "C" {
526  #ifndef LOGGER_H
527  #define LOGGER_H
528  
529 +#include <stdarg.h>
530  #include <stdio.h>
531  #include <stdlib.h>
532  
533 @@ -45,6 +46,7 @@ enum {
534  void logger_setup (int priority, const char *file, int line, const char *function);
535  
536  void logger_emit (const char *format, ...);
537 +void logger_emit_var (const char *format, va_list args);
538  /* void logger_forward_debug (const char *format, va_list args); */
539  void logger_forward_debug (const char *format, ...);
540  
541 @@ -72,7 +74,7 @@ void setup_logger (void);
542  /** Warning level logging macro */
543  #define HAL_WARNING(expr) do {logger_setup(HAL_LOGPRI_WARNING, __FILE__, __LINE__, __FUNCTION__); logger_emit expr; } while(0)
544  
545 -/** Error leve logging macro */
546 +/** Error level logging macro */
547  #define HAL_ERROR(expr)   do {logger_setup(HAL_LOGPRI_ERROR,   __FILE__, __LINE__, __FUNCTION__); logger_emit expr; } while(0)
548  
549  /** Macro for terminating the program on an unrecoverable error */