xref: /titanic_41/usr/src/cmd/hal/probing/acpi/probe-acpi.c (revision 2e1a94749a6ae1ee25de9f4492d4b90a3b408c4c)
1 /***************************************************************************
2  *
3  * probe-acpi.c : Probe for ACPI device information
4  *
5  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
6  * Use is subject to license terms.
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  **************************************************************************/
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17 
18 #include <errno.h>
19 #include <string.h>
20 #include <strings.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <sys/ioctl.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <glib.h>
28 
29 #include <libhal.h>
30 #include <logger.h>
31 #include "../utils/acpi.h"
32 
33 int
34 main(int argc, char *argv[])
35 {
36 	int ret = 1;
37 	int fd = -1;
38 	char *udi;
39 	char device_file[HAL_PATH_MAX] = "/devices";
40 	char *devfs_path;
41 	LibHalContext *ctx = NULL;
42 	DBusError error;
43 
44 	if ((udi = getenv("UDI")) == NULL)
45 		goto out;
46 	if ((devfs_path = getenv("HAL_PROP_SOLARIS_DEVFS_PATH")) == NULL)
47 		goto out;
48 	strlcat(device_file, devfs_path, HAL_PATH_MAX);
49 
50 	setup_logger();
51 
52 	dbus_error_init(&error);
53 	if ((ctx = libhal_ctx_init_direct(&error)) == NULL)
54 		goto out;
55 
56 	HAL_DEBUG(("Doing probe-acpi for %s (udi=%s)",
57 	    device_file, udi));
58 
59 	if ((fd = open(device_file, O_RDONLY | O_NONBLOCK)) < 0) {
60 		HAL_DEBUG(("Cannot open %s: %s", device_file, strerror(errno)));
61 		goto out;
62 	}
63 	if (strstr(udi, "_ac")) {
64 		ac_adapter_update(ctx, udi, fd);
65 	} else if (strstr(udi, "_battery")) {
66 		battery_update(ctx, udi, fd);
67 	} else if (strstr(udi, "_lid")) {
68 		lid_update(ctx, udi, fd);
69 	} else if (strstr(udi, "_output")) {
70 		laptop_panel_update(ctx, udi, fd);
71 	}
72 
73 	ret = 0;
74 
75 out:
76 	if (fd >= 0) {
77 		close(fd);
78 	}
79 
80 	if (ctx != NULL) {
81 		libhal_ctx_shutdown(ctx, &error);
82 		libhal_ctx_free(ctx);
83 		if (dbus_error_is_set(&error)) {
84 			dbus_error_free(&error);
85 		}
86 	}
87 
88 	return (ret);
89 }
90