Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

hal_dev.c

00001 /***************************************************************************
00002  * CVSID: $Id: hal_dev.c,v 1.1 2004/04/07 23:41:22 david Exp $
00003  *
00004  * hal_dev.c : Tiny program to transform an udev device event into
00005  *             a D-BUS message
00006  *
00007  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
00008  *
00009  * Licensed under the Academic Free License version 2.0
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  **************************************************************************/
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <unistd.h>
00037 #include <mntent.h>
00038 #include <syslog.h>
00039 #include <linux/limits.h>
00040 
00041 #include <dbus/dbus.h>
00042 
00058 static char sysfs_mnt_path[PATH_MAX];
00059 
00065 static int
00066 get_sysfs_mnt_path ()
00067 {
00068     FILE *mnt;
00069     struct mntent *mntent;
00070     int ret = 0;
00071     size_t dirlen = 0;
00072 
00073     if ((mnt = setmntent ("/proc/mounts", "r")) == NULL) {
00074         return -1;
00075     }
00076 
00077     while (ret == 0 && dirlen == 0
00078            && (mntent = getmntent (mnt)) != NULL) {
00079         if (strcmp (mntent->mnt_type, "sysfs") == 0) {
00080             dirlen = strlen (mntent->mnt_dir);
00081             if (dirlen <= (PATH_MAX - 1)) {
00082                 strcpy (sysfs_mnt_path, mntent->mnt_dir);
00083             } else {
00084                 ret = -1;
00085             }
00086         }
00087     }
00088     endmntent (mnt);
00089 
00090     if (dirlen == 0 && ret == 0) {
00091         ret = -1;
00092     }
00093     return ret;
00094 }
00102 int
00103 main (int argc, char *argv[], char *envp[])
00104 {
00105     int i, j, len;
00106     char *str;
00107     char *devpath;
00108     char *devname;
00109     int is_add;
00110     DBusError error;
00111     DBusConnection *sysbus_connection;
00112     DBusMessage *message;
00113     DBusMessageIter iter;
00114     DBusMessageIter iter_dict;
00115 
00116     if (argc != 2)
00117         return 1;
00118 
00119     if (get_sysfs_mnt_path () != 0)
00120         return 1;
00121 
00122     openlog ("hal.dev", LOG_PID, LOG_USER);
00123 
00124     /* Connect to a well-known bus instance, the system bus */
00125     dbus_error_init (&error);
00126     sysbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
00127     if (sysbus_connection == NULL)
00128         return 1;
00129 
00130     /* service, object, interface, member */
00131     message = dbus_message_new_method_call (
00132         "org.freedesktop.Hal",
00133         "/org/freedesktop/Hal/Linux/Hotplug",
00134         "org.freedesktop.Hal.Linux.Hotplug",
00135         "DeviceEvent");
00136 
00137     /* not interested in a reply */
00138     dbus_message_set_no_reply (message, TRUE);
00139 
00140     devpath = NULL;
00141     devname = NULL;
00142     is_add = FALSE;
00143 
00144     dbus_message_iter_init (message, &iter);
00145     for (i = 0; envp[i] != NULL; i++) {
00146         str = envp[i];
00147         len = strlen (str);
00148         for (j = 0; j < len && str[j] != '='; j++);
00149         str[j] = '\0';
00150 
00151         if (strcmp (str, "DEVPATH") == 0) {
00152             devpath = str + j + 1;
00153         } else if (strcmp (str, "DEVNODE") == 0) {
00154             devname = str + j + 1;
00155         } else if (strcmp (str, "DEVNAME") == 0) {
00156             devname = str + j + 1;
00157         } else if (strcmp (str, "ACTION") == 0) {
00158             if (strcmp (str + j + 1, "add") == 0) {
00159                 is_add = TRUE;
00160             }
00161         }
00162     }
00163 
00164     if (devname == NULL || devpath == NULL) {
00165         syslog (LOG_ERR, "Missing devname or devpath");
00166         goto out;
00167     }
00168 
00169     dbus_message_iter_append_boolean (&iter, is_add);
00170     dbus_message_iter_append_string (&iter, devname);
00171     dbus_message_iter_append_string (&iter, devpath);
00172 
00173     if (!dbus_connection_send (sysbus_connection, message, NULL))
00174         return 1;
00175 
00176     dbus_message_unref (message);
00177 
00178     dbus_connection_flush (sysbus_connection);
00179 
00180     /* Do some sleep here so messages are not lost.. */
00181     usleep (500 * 1000);
00182     
00183 out:
00184     dbus_connection_disconnect (sysbus_connection);
00185 
00186     return 0;
00187 }
00188 

Generated on Sat Apr 24 19:57:44 2004 for HAL by doxygen 1.3.6-20040222