dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / hal / probing / printer / probe-printer.c
blob181008e10dbc294eef17a9df7e6010a1e35060d9
1 /***************************************************************************
3 * probe-printer.c : Probe for prnio(7i) printer device information
5 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
6 * Use is subject to license terms.
8 * Licensed under the Academic Free License version 2.1
10 **************************************************************************/
12 #pragma ident "%Z%%M% %I% %E% SMI"
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
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/param.h>
25 #include <sys/types.h>
26 #include <dirent.h>
27 #include <sys/ioctl.h>
28 #include <sys/prnio.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <ctype.h>
33 #include <libhal.h>
34 #include <logger.h>
36 #include "printer.h"
38 static int
39 prnio_printer_info(char *device_file, char **manufacturer, char **model,
40 char **description, char **serial_number, char ***command_set)
42 struct prn_1284_device_id id;
43 char buf[BUFSIZ];
44 int fd = -1, rc = -1;
46 memset(&id, 0, sizeof (id));
47 memset(&buf, 0, sizeof (buf));
48 id.id_data = buf;
49 id.id_len = sizeof (buf);
51 if ((fd = open (device_file, O_RDONLY | O_NONBLOCK)) < 0) {
52 goto prnio_out;
55 if (ioctl(fd, PRNIOC_GET_1284_DEVID, &id) < 0) {
56 goto prnio_out;
59 HAL_DEBUG(("IEEE-1284 DeviceId = %s", buf));
61 rc = ieee1284_devid_to_printer_info(buf, manufacturer, model,
62 description, NULL, serial_number, command_set);
64 prnio_out:
65 if (fd != -1)
66 close(fd);
68 return (rc);
72 * It is assumed that all devices that support prnio(7i), also have a link
73 * in /dev/printers.
75 static char *
76 prnio_device_name(void)
78 char *result = NULL;
79 char *devfs_path;
80 DIR *dp;
82 if (((devfs_path = getenv("HAL_PROP_SOLARIS_DEVFS_PATH")) != NULL) &&
83 ((dp = opendir("/dev/printers")) != NULL)) {
84 struct dirent *ep;
86 while ((ep = readdir(dp)) != NULL) {
87 char path[MAXPATHLEN], lpath[MAXPATHLEN];
89 snprintf(path, sizeof (path), "/dev/printers/%s",
90 ep->d_name);
91 memset(lpath, 0, sizeof (lpath));
92 if ((readlink(path, lpath, sizeof (lpath)) > 0) &&
93 (strstr(lpath, devfs_path) != NULL)) {
94 result = strdup(path);
95 break;
98 closedir(dp);
101 return (result);
105 main(int argc, char *argv[])
107 int ret = 1;
108 char *udi;
109 char *device_file;
110 char *manufacturer = NULL,
111 *model = NULL,
112 *serial_number = NULL,
113 *description = NULL,
114 **command_set = NULL;
115 DBusError error;
116 LibHalContext *ctx = NULL;
117 LibHalChangeSet *cs = NULL;
119 if ((udi = getenv("UDI")) == NULL)
120 goto out;
121 if ((device_file = getenv("HAL_PROP_PRINTER_DEVICE")) == NULL)
122 device_file = prnio_device_name();
124 if (device_file == NULL)
125 goto out;
127 setup_logger();
129 dbus_error_init(&error);
130 if ((ctx = libhal_ctx_init_direct(&error)) == NULL)
131 goto out;
133 if ((cs = libhal_device_new_changeset(udi)) == NULL) {
134 HAL_DEBUG(("Cannot allocate changeset"));
135 goto out;
138 /* Probe the printer for characteristics via prnio(7i) */
139 ret = prnio_printer_info(device_file, &manufacturer, &model,
140 &description, &serial_number, &command_set);
141 if (ret < 0) {
142 HAL_DEBUG(("Cannot get prnio data for %s: %s",
143 device_file, strerror(errno)));
144 goto out;
147 /* Add printer characteristics to the HAL device tree */
148 ret = add_printer_info(cs, udi, manufacturer, model, description,
149 serial_number, command_set, device_file);
150 if (ret < 0) {
151 HAL_DEBUG(("Cannot add printer data for %s to %s: %s",
152 device_file, udi, strerror(errno)));
153 goto out;
156 libhal_device_commit_changeset(ctx, cs, &error);
158 ret = 0;
160 out:
161 if (cs != NULL) {
162 libhal_device_free_changeset(cs);
165 if (ctx != NULL) {
166 if (dbus_error_is_set(&error)) {
167 dbus_error_free(&error);
169 libhal_ctx_shutdown(ctx, &error);
170 libhal_ctx_free(ctx);
173 return (ret);