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