summaryrefslogtreecommitdiff
path: root/hald/linux/osspec.c
blob: 4599c7cf44a6aeb89481b47a1a6d3cbb9c3b920c (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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
/***************************************************************************
 * CVSID: $Id$
 *
 * osspec.c : New and improved HAL backend for Linux 2.6
 *
 * Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
 * Copyright (C) 2005,2006 Kay Sievers, <kay.sievers@vrfy.org>
 * Copyright (C) 2005 Danny Kukawka, <danny.kukawka@web.de>
 *
 * 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
 *
 **************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#define _GNU_SOURCE 1
#define DMI_SYSFS_PATH "/sys/class/dmi/id"

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <linux/types.h>
#include <net/if_arp.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/utsname.h>
#include <unistd.h>

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>

#include "../device_info.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../hald_runner.h"
#include "../logger.h"
#include "../osspec.h"
#include "../util.h"
#include "../util_helper.h"
#include "../ids.h"

#include "acpi.h"
#include "apm.h"
#include "blockdev.h"
#include "coldplug.h"
#include "hotplug.h"
#include "pmu.h"

#include "osspec_linux.h"

static gboolean hald_done_synthesizing_coldplug = FALSE;

static gboolean
hald_udev_data (GIOChannel *source, GIOCondition condition, gpointer user_data)
{
	int fd;
	int retval;
	struct msghdr smsg;
	struct cmsghdr *cmsg;
	struct iovec iov;
	struct ucred *cred;
	char cred_msg[CMSG_SPACE(sizeof(struct ucred))];

	char buf[4096];
	size_t bufpos = 0;
	const char *action = NULL;
	HotplugEvent *hotplug_event;

	memset(buf, 0x00, sizeof (buf));

	fd = g_io_channel_unix_get_fd (source);

	iov.iov_base = &buf;
	iov.iov_len = sizeof (buf);

	memset(&smsg, 0x00, sizeof (struct msghdr));
	smsg.msg_iov = &iov;
	smsg.msg_iovlen = 1;
	smsg.msg_control = cred_msg;
	smsg.msg_controllen = sizeof (cred_msg);

	retval = recvmsg (fd, &smsg, 0);
	if (retval <  0) {
		if (errno != EINTR)
			HAL_INFO (("Unable to receive message, errno=%d", errno));
		goto out;
	}
	cmsg = CMSG_FIRSTHDR (&smsg);
	cred = (struct ucred *) CMSG_DATA (cmsg);

	if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
		HAL_INFO (("No sender credentials received, message ignored"));
		goto out;
	}

	if (cred->uid != 0) {
		HAL_INFO (("Sender uid=%i, message ignored", cred->uid));
		goto out;
	}

	if (!strstr(buf, "@/")) {
		HAL_INFO (("invalid message format"));
		goto out;
	}

	hotplug_event = g_slice_new0 (HotplugEvent);
	hotplug_event->type = HOTPLUG_EVENT_SYSFS;

	while (bufpos < sizeof (buf)) {
		size_t keylen;
		char *key;
		char *str;

		key = &buf[bufpos];
		keylen = strlen(key);
		if (keylen == 0)
			break;
		bufpos += keylen + 1;

		if (strncmp(key, "ACTION=", 7) == 0)
			action = &key[7];
		else if (strncmp(key, "DEVPATH=", 8) == 0) {

                        /* md devices are handled via looking at /proc/mdstat */
                        if (g_str_has_prefix (key + 8, "/block/md")) {
                                HAL_INFO (("skipping md event for %s", key + 8));
                                goto invalid;
                        }

			g_snprintf (hotplug_event->sysfs.sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path),
				    "/sys%s", &key[8]);
		} else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {

                        /* md devices are handled via looking at /proc/mdstat */
                        if (g_str_has_prefix (key + 12, "/block/md")) {
                                HAL_INFO (("skipping md event for %s", key + 8));
                                goto invalid;
                        }

			g_snprintf (hotplug_event->sysfs.sysfs_path_old, sizeof (hotplug_event->sysfs.sysfs_path_old),
				    "/sys%s", &key[12]);
		} else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
			g_strlcpy (hotplug_event->sysfs.subsystem, &key[10], sizeof (hotplug_event->sysfs.subsystem));
		else if (strncmp(key, "DEVNAME=", 8) == 0)
			g_strlcpy (hotplug_event->sysfs.device_file, &key[8], sizeof (hotplug_event->sysfs.device_file));
		else if (strncmp(key, "SEQNUM=", 7) == 0)
			hotplug_event->sysfs.seqnum = strtoull(&key[7], NULL, 10);
		else if (strncmp(key, "IFINDEX=", 8) == 0)
			hotplug_event->sysfs.net_ifindex = strtoul(&key[8], NULL, 10);
		else if (strncmp(key, "ID_VENDOR=", 10) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[10])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.vendor, str, sizeof(hotplug_event->sysfs.vendor));
				g_free (str);
			}
		} else if (strncmp(key, "ID_MODEL=", 9) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[9])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.model, str, sizeof(hotplug_event->sysfs.model));
				g_free (str);
			}
		} else if (strncmp(key, "ID_REVISION=", 12) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[12])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.revision, str, sizeof(hotplug_event->sysfs.revision));
				g_free (str);
			}
		} else if (strncmp(key, "ID_SERIAL=", 10) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[10])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.serial, str, sizeof(hotplug_event->sysfs.serial));
				g_free (str);
			}
		} else if (strncmp(key, "ID_FS_USAGE=", 12) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[12])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.fsusage, str, sizeof(hotplug_event->sysfs.fsusage));
				g_free (str);
			}
		} else if (strncmp(key, "ID_FS_TYPE=", 11) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[11])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.fstype, str, sizeof(hotplug_event->sysfs.fstype));
				g_free (str);
			}
		} else if (strncmp(key, "ID_FS_VERSION=", 14) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[14])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.fsversion, str, sizeof(hotplug_event->sysfs.fsversion));
				g_free (str);
			}
		} else if (strncmp(key, "ID_FS_UUID=", 11) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[11])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.fsuuid, str, sizeof(hotplug_event->sysfs.fsuuid));
				g_free (str);
			}
		} else if (strncmp(key, "ID_FS_LABEL=", 12) == 0) {
			if ((str = hal_util_strdup_valid_utf8(&key[12])) != NULL ) {
				g_strlcpy (hotplug_event->sysfs.fslabel, str, sizeof(hotplug_event->sysfs.fslabel));
				g_free (str);
			}
		}
	}

	if (!action) {
		HAL_INFO (("missing ACTION"));
		goto invalid;
	}
	if (hotplug_event->sysfs.sysfs_path[0] == '\0') {
		HAL_INFO (("missing DEVPATH"));
		goto invalid;
	}
	if (hotplug_event->sysfs.subsystem[0] == '\0') {
		HAL_INFO (("missing SUSBSYSTEM"));
		goto invalid;
	}

	/* This is a workaround for temporary cryptsetup devices, HAL should ignore them. 
	 * There is already a fix for this issue in the udev git master as soon as a new
	 * udev release is available and HAL starts to depend on the new version we should 
	 * remove this again. (added 2008-03-04)
	 */
	if (strncmp (hotplug_event->sysfs.device_file, "/dev/mapper/temporary-cryptsetup-", 33) == 0) {
		HAL_INFO (("Temporary workaround: ignoring temporary cryptsetup file"));
		goto invalid;
	}
	if (strstr (hotplug_event->sysfs.device_file, "/dm-") != NULL) {
		HAL_DEBUG (("Found a dm-device (%s), mark it", hotplug_event->sysfs.device_file));
		hotplug_event->sysfs.is_dm_device = TRUE;
	}
		

	HAL_INFO (("SEQNUM=%lld, ACTION=%s, SUBSYSTEM=%s, DEVPATH=%s, DEVNAME=%s, IFINDEX=%d",
		   hotplug_event->sysfs.seqnum, action, hotplug_event->sysfs.subsystem, hotplug_event->sysfs.sysfs_path,
		   hotplug_event->sysfs.device_file, hotplug_event->sysfs.net_ifindex));

	/* ignore module and driver events, until we really need them */
	if ((strcmp (hotplug_event->sysfs.subsystem, "drivers") != 0) && 
	    (strcmp (hotplug_event->sysfs.subsystem, "module") != 0)) {
		if (strcmp (action, "add") == 0) {
			hotplug_event->action = HOTPLUG_ACTION_ADD;
			hotplug_event_enqueue (hotplug_event);
			hotplug_event_process_queue ();
			goto out;
		}

		if (strcmp (action, "change") == 0) {
			hotplug_event->action = HOTPLUG_ACTION_CHANGE;
			hotplug_event_enqueue (hotplug_event);
			hotplug_event_process_queue ();
			goto out;
		}

		if (strcmp (action, "move") == 0) {
			hotplug_event->action = HOTPLUG_ACTION_MOVE;
			hotplug_event_enqueue (hotplug_event);
			hotplug_event_process_queue ();
			goto out;
		}

		if (strcmp (action, "remove") == 0) {
			hotplug_event->action = HOTPLUG_ACTION_REMOVE;
			hotplug_event_enqueue (hotplug_event);
			hotplug_event_process_queue ();
			goto out;
		}
	}

invalid:
	g_slice_free (HotplugEvent, hotplug_event);

out:
	return TRUE;
}

static gboolean
mount_tree_changed_event (GIOChannel *channel, GIOCondition cond, gpointer user_data)
{
	if (cond & ~G_IO_ERR)
		return TRUE;

	HAL_INFO (("/proc/mounts tells, that the mount has tree changed"));
	blockdev_refresh_mount_state (NULL);

	return TRUE;
}

static gboolean
mdstat_changed_event (GIOChannel *channel, GIOCondition cond, gpointer user_data)
{
	if (cond & ~G_IO_PRI)
		return TRUE;

	HAL_INFO (("/proc/mdstat changed"));

        blockdev_process_mdstat ();

	return TRUE;
}

static HalFileMonitor *file_monitor = NULL;

HalFileMonitor *
osspec_get_file_monitor (void)
{
        return file_monitor;
}

static GIOChannel *mdstat_channel = NULL;

GIOChannel *get_mdstat_channel (void)
{
        return mdstat_channel;
}

/* 
 * NOTE: We need to use this function to parse if HAL is privileged 
 *       since some of the dmi keys in sysfs are only readable by root
 */
static void 
osspec_privileged_init_preparse_set_dmi (gboolean set, HalDevice *d) 
{
	gchar *buf;
	static char *product_serial;
	static char *product_uuid;
	static char *board_serial;
	static gboolean parsed = FALSE; 

	if (g_file_test (DMI_SYSFS_PATH, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {

		if (!set){
			if ((buf = hal_util_get_string_from_file(DMI_SYSFS_PATH, "product_serial")) != NULL)
				product_serial = g_strdup ( buf );
			if ((buf = hal_util_get_string_from_file(DMI_SYSFS_PATH, "product_uuid")) != NULL)
				product_uuid = g_strdup ( buf );
			if ((buf = hal_util_get_string_from_file(DMI_SYSFS_PATH, "board_serial")) != NULL)
				board_serial = g_strdup ( buf );

			parsed = TRUE;
		} else {
			if (d != NULL && parsed) {
				hal_device_property_set_string (d, "system.hardware.serial", product_serial);		
				hal_device_property_set_string (d, "system.hardware.uuid", product_uuid);
				hal_device_property_set_string (d, "system.board.serial", board_serial);
				g_free (product_serial);
				g_free (product_uuid);
				parsed = FALSE;
			}
		}
	}
}

void
osspec_privileged_init (void)
{
	GError *err = NULL;

        file_monitor = hal_file_monitor_new ();
        if (file_monitor == NULL) {
                DIE (("Cannot initialize file monitor"));
        }

	/* watch /proc/mdstat for md changes
	 * kernel 2.6.19 throws a POLLPRI event for every change
	 */
	mdstat_channel = g_io_channel_new_file ("/proc/mdstat", "r", &err);
	if (mdstat_channel != NULL) {
		g_io_add_watch (mdstat_channel, G_IO_PRI, mdstat_changed_event, NULL);
	} else {
		if (err != NULL)
			HAL_WARNING (("Unable to open /proc/mdstat: %s", err->message));

		/* if its not a reasonable error, abort */
		if (!g_error_matches (err, G_FILE_ERROR, G_FILE_ERROR_NOENT))
			DIE (("Unable to read /proc/mdstat"));
	}
	
	if (err != NULL)
		g_error_free (err);

	osspec_privileged_init_preparse_set_dmi(FALSE, NULL);
}

void
osspec_init (void)
{
	int udev_socket;
	struct sockaddr_un saddr;
	socklen_t addrlen;
	const int on = 1;
	GIOChannel *udev_channel;
	GIOChannel *mounts_channel;

	/*
	 * setup socket for listening from messages from udev
	 */

	hal_device_store_index_property (hald_get_gdl (), "linux.sysfs_path");

	memset(&saddr, 0x00, sizeof(saddr));
	saddr.sun_family = AF_LOCAL;
	/* use abstract namespace for socket path */
	strcpy(&saddr.sun_path[1], "/org/freedesktop/hal/udev_event");
	addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;

	udev_socket = socket(AF_LOCAL, SOCK_DGRAM, 0);
	if (udev_socket == -1) {
		DIE (("Couldn't open socket"));
	}

	if (bind(udev_socket, (struct sockaddr *) &saddr, addrlen) < 0) {
		fprintf (stderr, "Error binding udev_event socket: %s\n", strerror(errno));
		exit (1);
	}
	/* enable receiving of the sender credentials */
	setsockopt(udev_socket, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));

	udev_channel = g_io_channel_unix_new (udev_socket);
	g_io_add_watch (udev_channel, G_IO_IN, hald_udev_data, NULL);
	g_io_channel_unref (udev_channel);

	/* watch /proc/mounts for mount tree changes
	 * kernel 2.6.15 vfs throws a POLLERR event for every change
	 */
	mounts_channel = g_io_channel_new_file ("/proc/mounts", "r", NULL);
	if (mounts_channel == NULL)
		DIE (("Unable to read /proc/mounts"));
	g_io_add_watch (mounts_channel, G_IO_ERR, mount_tree_changed_event, NULL);

	/*
	 *Load various hardware id databases
	 */
	ids_init ();

	/* watch fdi directories */
	/* TODO: temporarily disabled... 
           watch_fdi_files (); */
}


static void 
computer_callouts_add_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
	HAL_INFO (("Add callouts completed udi=%s", hal_device_get_udi (d)));

	/* Move from temporary to global device store */
	hal_device_store_remove (hald_get_tdl (), d);
	hal_device_store_add (hald_get_gdl (), d);
}

void
hotplug_queue_now_empty (void)
{
	if (hald_is_initialising && hald_done_synthesizing_coldplug) {
		osspec_probe_done ();
        }
}


static void
computer_probing_helper_done (HalDevice *d)
{
	/* check if this is may a laptop */
	if (!hal_device_has_property (d, "system.formfactor") || 
	    (strcmp (hal_device_property_get_string (d, "system.formfactor"), "laptop") != 0)) {
		HAL_INFO (("Check if the machine is may a laptop ..."));
		acpi_check_is_laptop("BATTERY");
		acpi_check_is_laptop("LID");
	}
	/* if not set, set a default value */
	if (!hal_device_has_property (d, "system.formfactor")) {
		hal_device_property_set_string (d, "system.formfactor", "unknown");
	}

	/* will enqueue hotplug events for entire system */
	HAL_INFO (("Synthesizing sysfs events..."));
	coldplug_synthesize_events ();

	HAL_INFO (("Synthesizing powermgmt events..."));
	if (acpi_synthesize_hotplug_events ()) {
		HAL_INFO (("ACPI capabilities found"));
	} else if (pmu_synthesize_hotplug_events ()) {
		HAL_INFO (("PMU capabilities found"));		
	} else if (apm_synthesize_hotplug_events ()) {
		HAL_INFO (("APM capabilities found"));		
	} else {
		HAL_INFO (("No powermgmt capabilities"));		
	}
	HAL_INFO (("Done synthesizing events"));

        hald_done_synthesizing_coldplug = TRUE;

	/* we try again to match again on computer, now we have done coldplug
	 * and completed probing. In an ideal world, we would do this before
	 * _and_ after the coldplug, but this seems to work well. */
	di_search_and_merge (d, DEVICE_INFO_TYPE_INFORMATION);
	di_search_and_merge (d, DEVICE_INFO_TYPE_POLICY);

	hal_util_callout_device_add (d, computer_callouts_add_done, NULL, NULL);
}

static void 
computer_dmi_map (HalDevice *d, gboolean dmidecode) 
{
	/* Map the chassis type from dmidecode.c to a sensible type used in hal 
	 *
	 * See also 3.3.4.1 of the "System Management BIOS Reference Specification, 
	 * Version 2.6.1" (Preliminary Standard) document, available from 
	 * http://www.dmtf.org/standards/smbios.
	 *
	 * TODO: figure out WTF the mapping should be; "Lunch Box"? Give me a break :-)
	 */
	static const char *chassis_map[] = {
		"Other",                 "unknown", /* 0x01 */
		"Unknown",               "unknown",
		"Desktop",               "desktop",
		"Low Profile Desktop",   "desktop",
		"Pizza Box",             "server",
		"Mini Tower",            "desktop",
		"Tower",                 "desktop",
		"Portable",              "laptop",
		"Laptop",                "laptop",
		"Notebook",              "laptop",
		"Hand Held",             "handheld",
		"Docking Station",       "laptop",
		"All In One",            "unknown",
		"Sub Notebook",          "laptop",
		"Space-saving",          "desktop",
		"Lunch Box",             "unknown",
		"Main Server Chassis",   "server",
		"Expansion Chassis",     "unknown",
		"Sub Chassis",           "unknown",
		"Bus Expansion Chassis", "unknown",
		"Peripheral Chassis",    "unknown",
		"RAID Chassis",          "unknown",
		"Rack Mount Chassis",    "unknown",
		"Sealed-case PC",        "unknown",
		"Multi-system",          "unknown",
		"CompactPCI",		 "unknonw",
		"AdvancedTCA",		 "unknown", 
		"Blade",		 "server",
		"Blade Enclosure"	 "unknown", /* 0x1D */
		NULL
	};


	if (dmidecode) {
		/* do mapping from text to text type */ 
		unsigned int i;
		const char *chassis_type;

		/* now map the smbios.* properties to our generic system.formfactor property */
		if ((chassis_type = hal_device_property_get_string (d, "system.chassis.type")) != NULL) {
			
			for (i = 0; chassis_map[i] != NULL; i += 2) {
				if (strcmp (chassis_map[i], chassis_type) == 0) {
					hal_device_property_set_string (d, "system.formfactor", chassis_map[i+1]);
					break;
				}
			}
		} 
	} else {
		gint chassis_type;

		/* do mapping from integer type to text type*/
		/* get the chassis type and map it to the related text info */
		if (hal_util_get_int_from_file(DMI_SYSFS_PATH, "chassis_type", &chassis_type, 10)) {

			if ((chassis_type > 0) && (chassis_type < 28) && (chassis_map[(chassis_type-1)*2] != NULL)) {
				hal_device_property_set_string (d, "system.chassis.type", chassis_map[((chassis_type-1)*2)]);
				hal_device_property_set_string (d, "system.formfactor", chassis_map[((chassis_type-1)*2)+1]);
			}

		} else {
			hal_device_property_set_string (d, "system.chassis.type", "Unknown");
			hal_device_property_set_string (d, "system.formfactor", "unknown");
		}
	}

}

static void 
computer_probing_pcbios_helper_done (HalDevice *d, guint32 exit_type, 
	                             gint return_code, gchar **error, 
				     gpointer data1, gpointer data2)
{
	const char *system_manufacturer;
	const char *system_product;
	const char *system_version;

	if (exit_type == HALD_RUN_FAILED) {
		goto out;
	}

	if ((system_manufacturer = hal_device_property_get_string (d, "system.hardware.vendor")) != NULL &&
	    (system_product = hal_device_property_get_string (d, "system.hardware.product")) != NULL &&
	    (system_version = hal_device_property_get_string (d, "system.hardware.version")) != NULL) {
		char buf[128];

		if (strcmp(system_version, "Not Specified" ) != 0 ) {
			g_snprintf (buf, sizeof (buf), "%s %s", system_product, system_version);
			hal_device_property_set_string (d, "system.product", buf);
		} else {
			hal_device_property_set_string (d, "system.product", system_product);
		}
	}


	if (!hal_device_has_property (d, "system.formfactor")) {
		computer_dmi_map (d, TRUE);
	}
out:
	computer_probing_helper_done (d);
}

static void
set_suspend_hibernate_keys (HalDevice *d)
{
	gboolean can_suspend;
	gboolean can_hibernate;
	char *poweroptions;
	const char *pmtype;

	can_suspend = FALSE;
	can_hibernate = FALSE;

	/* try to find 'mem' and 'disk' in /sys/power/state */
	poweroptions = hal_util_get_string_from_file("/sys/power/", "state");
	if (poweroptions == NULL) {
		HAL_WARNING (("Contents of /sys/power/state invalid"));
		goto out;
	}
	if (strstr (poweroptions, "mem"))
		can_suspend = TRUE;
	if (strstr (poweroptions, "disk"))
		can_hibernate = TRUE;

	if (!can_suspend) {
		pmtype = hal_device_property_get_string (d, "power_management.type");
		if (pmtype != NULL && strcmp(pmtype, "pmu") == 0) {
			/* We got our own helper for suspend PMU machines */
			can_suspend = TRUE;
		}
	}

	if (!can_hibernate) {
		/* check for the presence of suspend2 */
		if (access ("/proc/software_suspend", F_OK) == 0) {
			can_hibernate = TRUE;
		} else if (access ("/proc/suspend2", F_OK) == 0) {
			can_hibernate = TRUE;
		} else if (access ("/sys/power/suspend2/version", F_OK) == 0) {
			can_hibernate = TRUE;
		}
	}
out:
	hal_device_property_set_bool (d, "power_management.can_suspend", can_suspend);
	hal_device_property_set_bool (d, "power_management.can_suspend_hybrid", FALSE);
	hal_device_property_set_bool (d, "power_management.can_hibernate", can_hibernate);
}

static void
get_openfirmware_entry(HalDevice *d, char *property, char *entry, 
                       gboolean multivalue) 
{
	char *contents;
	gsize length;
	if (!g_file_get_contents(entry, &contents, &length, NULL)) {
		return;
	}
	if (multivalue) {
		gsize offset = 0;
		while (offset < length) { 
			hal_device_property_strlist_append(d, property, contents + offset, FALSE);
			for (; offset < length - 1 && contents[offset] != '\0'; offset++)
				;
			offset++;
		}
	} else {
		hal_device_property_set_string(d, property, contents);
	}
	free(contents);
}

static void
detect_openfirmware_formfactor(HalDevice *root) 
{
	int x;
	struct { gchar *model; gchar *formfactor; } model_formfactor[] =
		{ 
			{ "RackMac"   , "server" },
			{ "AAPL,3400" , "laptop"  },
			{ "AAPL,3500" , "laptop"  },
			{ "PowerBook" , "laptop"  },
			{ "AAPL"      , "desktop" },
			{ "iMac"      , "desktop" },
			{ "PowerMac"  , "desktop" },
			{ "Pegasos"   , "desktop" },
			{NULL, NULL }
		};
	const gchar *model =
	  hal_device_property_get_string(root, "openfirmware.model");
	if (model == NULL) 
		return;
	for (x = 0 ; model_formfactor[x].model ; x++) {
		if (strstr(model, model_formfactor[x].model)) {
			hal_device_property_set_string (root, "system.formfactor",
				model_formfactor[x].formfactor);
			break;
		}
	}
}

static gboolean
decode_dmi_from_openfirmware (HalDevice *root)
{
#define DEVICE_TREE "/proc/device-tree/"
	if (!g_file_test(DEVICE_TREE, G_FILE_TEST_IS_DIR))
		return FALSE;

	get_openfirmware_entry(root, "openfirmware.model", 
		DEVICE_TREE "model", FALSE);
	get_openfirmware_entry(root, "openfirmware.compatible", 
		DEVICE_TREE "compatible", TRUE);
	detect_openfirmware_formfactor(root);
	return TRUE;
}

static gboolean
decode_dmi_from_sysfs (HalDevice *d)
{
	if (!g_file_test (DMI_SYSFS_PATH, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
		return FALSE;

	osspec_privileged_init_preparse_set_dmi(TRUE, d);
	hal_util_set_string_from_file(d, "system.firmware.vendor", DMI_SYSFS_PATH, "bios_vendor");
	hal_util_set_string_from_file(d, "system.firmware.version", DMI_SYSFS_PATH, "bios_version");
	hal_util_set_string_from_file(d, "system.firmware.release_date", DMI_SYSFS_PATH, "bios_date");
	hal_util_set_string_from_file(d, "system.hardware.vendor", DMI_SYSFS_PATH, "sys_vendor");
	hal_util_set_string_from_file(d, "system.hardware.product", DMI_SYSFS_PATH, "product_name");
	hal_util_set_string_from_file(d, "system.hardware.version", DMI_SYSFS_PATH, "product_version");
	hal_util_set_string_from_file(d, "system.chassis.manufacturer", DMI_SYSFS_PATH, "chassis_vendor");
	hal_util_set_string_from_file(d, "system.board.product", DMI_SYSFS_PATH, "board_name");
	hal_util_set_string_from_file(d, "system.board.version", DMI_SYSFS_PATH, "board_version");
	hal_util_set_string_from_file(d, "system.board.vendor", DMI_SYSFS_PATH, "board_vendor");
	computer_dmi_map (d, FALSE);

	return TRUE;
}

static void
decode_dmi (HalDevice *d)
{
	/* try to get the dmi infos from sysfs instead of call dmidecode*/
	if (decode_dmi_from_sysfs(d) ||
	    decode_dmi_from_openfirmware (d)) {
		HAL_INFO (("got DMI from files"));
		computer_probing_helper_done (d);
	} else {
		if (g_file_test ("/usr/sbin/dmidecode", G_FILE_TEST_IS_EXECUTABLE) ||
		    g_file_test ("/bin/dmidecode", G_FILE_TEST_IS_EXECUTABLE) ||
		    g_file_test ("/sbin/dmidecode", G_FILE_TEST_IS_EXECUTABLE) ||
		    g_file_test ("/usr/local/sbin/dmidecode", G_FILE_TEST_IS_EXECUTABLE)) {
			HAL_INFO (("getting DMI from prober"));
			hald_runner_run (d, "hald-probe-smbios", NULL, HAL_HELPER_TIMEOUT,
					 computer_probing_pcbios_helper_done, NULL, NULL);
		} else {
			/* no probing possible */
			HAL_INFO (("failed to probe DMI"));
			computer_probing_helper_done (d);
		}
	}
}

static void 
computer_probing_pm_is_supported_helper_done (HalDevice *d, guint32 exit_type, 
                                              gint return_code, gchar **error, 
                                              gpointer data1, gpointer data2)
{
	HAL_INFO (("In computer_probing_pm_is_supported_helper_done"));
	decode_dmi (d);
}

static void
get_primary_videocard (HalDevice *d)
{
        GDir *dir;
        const char *name;

        dir = g_dir_open ("/sys/bus/pci/devices", 0, NULL);
        if (dir == NULL)
                goto out;
        while ((name = g_dir_read_name (dir)) != NULL) {
                int class;
                char *path;
                path = g_strdup_printf ("/sys/bus/pci/devices/%s", name);
                if (hal_util_get_int_from_file (path, "class", &class, 0) && (class&0xffff00) == 0x030000 ) {
                        int vendor, device;
                        if (hal_util_get_int_from_file (path, "vendor", &vendor, 0) &&
                            hal_util_get_int_from_file (path, "device", &device, 0)) {
                                HAL_INFO (("got %x:%x as primary videocard", vendor, device));
                                hal_device_property_set_int (d, "system.hardware.primary_video.vendor", vendor);
                                hal_device_property_set_int (d, "system.hardware.primary_video.product", device);
                                g_free (path);
                                g_dir_close (dir);
                                goto out;
                        }
                }
                g_free (path);
        }
        g_dir_close (dir);
out:
        ;
}

void 
osspec_probe (void)
{
	HalDevice *root;
	struct utsname un;

	hald_runner_set_method_run_notify ((HaldRunnerRunNotify) hotplug_event_process_queue, NULL);
	root = hal_device_new ();
	hal_device_property_set_string (root, "info.subsystem", "unknown");
	hal_device_property_set_string (root, "info.product", "Computer");
	hal_device_set_udi (root, "/org/freedesktop/Hal/devices/computer");

	if (uname (&un) >= 0) {
		hal_device_property_set_string (root, "system.kernel.name", un.sysname);
		hal_device_property_set_string (root, "system.kernel.version", un.release);
		if (un.release != NULL && un.release[0] != '\0') {
                        int major, minor, micro ;

			/* check if we can parse the major.minor.micro info and ignore the rest */
                        if ( sscanf( un.release, "%d.%d.%d", &major, &minor, &micro ) >= 3 ) {
				hal_device_property_set_int (root, "system.kernel.version.major", major);
				hal_device_property_set_int (root, "system.kernel.version.minor", minor);
				hal_device_property_set_int (root, "system.kernel.version.micro", micro);
			}
                }

		hal_device_property_set_string (root, "system.kernel.machine", un.machine);
	}

	/* Let computer be in TDL while synthesizing all other events because some may write to the object */
	hal_device_store_add (hald_get_tdl (), root);

	/*
	 * Populate the powermgmt keys according to the kernel options.
	 * NOTE: This may not mean the machine is able to suspend
	 *	 or hibernate successfully, only that the machine has
	 *	 support compiled into the kernel.
	 */
	set_suspend_hibernate_keys (root);

        /* set the vendor/product of primary video card */
        get_primary_videocard (root);

        /* Try and set the suspend/hibernate keys using pm-is-supported
         */
        if (g_file_test ("/usr/bin/pm-is-supported", G_FILE_TEST_IS_EXECUTABLE)) {
                hald_runner_run (root, "hal-system-power-pm-is-supported", NULL, HAL_HELPER_TIMEOUT,
                                 computer_probing_pm_is_supported_helper_done, NULL, NULL);
        } else {
                decode_dmi (root);
        }
}

DBusHandlerResult
osspec_filter_function (DBusConnection *connection, DBusMessage *message, void *user_data)
{
	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

gboolean
osspec_device_rescan (HalDevice *d)
{
	return hotplug_rescan_device (d);
}

gboolean
osspec_device_reprobe (HalDevice *d)
{
	return hotplug_reprobe_tree (d);
}

gboolean
hal_util_get_driver_name (const char *sysfs_path, gchar *driver_name)
{
	gchar driver_path[HAL_PATH_MAX];
	struct stat statbuf;

	if (sysfs_path == NULL) {
		HAL_WARNING (("hal_util_get_driver_name: sysfs_path == NULL"));
		return FALSE;
	}

	g_snprintf (driver_path, sizeof (driver_path), "%s/driver", sysfs_path);
	if (stat (driver_path, &statbuf) == 0) {
		gchar buf[256];
		memset (buf, '\0', sizeof (buf));
		if (readlink (driver_path, buf, sizeof (buf) - 1) > 0) {
			g_snprintf (driver_name, strlen(buf), "%s", hal_util_get_last_element(buf));
			return TRUE;
		}
	}
	return FALSE;
}

gboolean
hal_util_set_driver (HalDevice *d, const char *property_name, const char *sysfs_path)
{
	gboolean ret;
	gchar driver_name[256];

	if (d == NULL || property_name == NULL || sysfs_path == NULL) {
		HAL_WARNING (("hal_util_set_driver: d, property_name or sysfs_path == NULL"));
		return FALSE;
	}

	memset (driver_name, '\0', sizeof (driver_name));
	ret = hal_util_get_driver_name (sysfs_path, driver_name);
	if (ret == TRUE)
		hal_device_property_set_string (d, property_name, driver_name);

	return ret;
}

static gboolean get_parent_device(char *path)
{
	/* go up one directory */
	if (!hal_util_path_ascend (path))
		return FALSE;
	if (g_str_has_suffix (path, "/class"))
		return FALSE;
	if (g_str_has_suffix (path, "/block"))
		return FALSE;
	if (g_str_has_suffix (path, "/devices"))
		return FALSE;
	return TRUE;
}

/* return the first already known parent device */
gboolean
hal_util_find_known_parent (const gchar *sysfs_path, HalDevice **parent, gchar **parent_path)
{
	gchar *target;
	HalDevice *parent_dev = NULL;
	gchar *parent_devpath;
	char parentdevpath[HAL_PATH_MAX];
	gboolean retval = FALSE;

	parent_devpath = g_strdup (sysfs_path);
	while (TRUE) {
		if (!get_parent_device (parent_devpath))
			break;

		parent_dev = hal_device_store_match_key_value_string (hald_get_gdl (),
								      "linux.sysfs_path",
								      parent_devpath);
		if (parent_dev != NULL)
			goto out;
	}
	g_free (parent_devpath);
	parent_devpath = NULL;

	/* try if the parent chain is constructed by the device-link */
	g_snprintf (parentdevpath, HAL_PATH_MAX, "%s/device", sysfs_path);
	if ((target = hal_util_readlink (parentdevpath)) != NULL) {
		parent_devpath = hal_util_get_normalized_path (sysfs_path, target);

		while (TRUE) {
			parent_dev = hal_device_store_match_key_value_string (hald_get_gdl (),
									      "linux.sysfs_path",
									      parent_devpath);
			if (parent_dev != NULL)
				goto out;

			/* go up one directory */
			if (!get_parent_device (parent_devpath))
				break;
		}
		g_free (parent_devpath);
		parent_devpath = NULL;
	}

out:
	if (parent_dev != NULL) {
		HAL_INFO (("hal_util_find_known_parent: '%s'->'%s'", sysfs_path, parent_devpath));
		retval = TRUE;
	}
	if (parent != NULL)
		*parent = parent_dev;
	if (parent_path != NULL)
		*parent_path = parent_devpath;
	else
		g_free (parent_devpath);
	return retval;
}

void
osspec_refresh_mount_state_for_block_device (HalDevice *d)
{
	blockdev_refresh_mount_state (d);
}