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

hal_hotplug.c

00001 /***************************************************************************
00002  * CVSID: $Id: hal_hotplug.c,v 1.7 2004/01/16 13:26:47 david Exp $
00003  *
00004  * hal_hotplug.c : Tiny program to transform a linux-hotplug 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 <unistd.h>
00035 
00036 #include <dbus/dbus.h>
00037 
00059 int main(int argc, char* argv[], char* envp[])
00060 {
00061     int i, j, len;
00062     char* str;
00063     DBusError error;
00064     DBusConnection* sysbus_connection;
00065     DBusMessage* message;
00066     DBusMessageIter iter;
00067     DBusMessageIter iter_dict;
00068 
00069     if( argc!=2 )
00070         return 1;
00071 
00072     /* Connect to a well-known bus instance, the system bus */
00073     dbus_error_init(&error);
00074     sysbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
00075     if( sysbus_connection==NULL ) 
00076         return 1;
00077 
00078     /* service, object, interface, member */
00079     message = dbus_message_new_method_call(
00080         "org.freedesktop.Hal",
00081         "/org/freedesktop/Hal/Linux/Hotplug", 
00082         "org.freedesktop.Hal.Linux.Hotplug",
00083         "HotplugEvent");
00084 
00085     /* not interested in a reply */
00086     dbus_message_set_no_reply(message, TRUE);
00087     
00088     dbus_message_iter_init(message, &iter);
00089     dbus_message_iter_append_string(&iter, argv[1]);
00090     dbus_message_iter_append_dict(&iter, &iter_dict);
00091     for(i=0; envp[i]!=NULL; i++)
00092     {
00093         str = envp[i];
00094         len = strlen(str);
00095         for(j=0; j<len && str[j]!='='; j++)
00096             ;
00097         str[j]='\0';
00098 
00099         dbus_message_iter_append_dict_key(&iter_dict, str);
00100         dbus_message_iter_append_string(&iter_dict, str+j+1);
00101     }
00102 
00103     /* Do some sleep here so the kernel have time to publish it's
00104      * stuff in sysfs
00105      */
00106     usleep(1000*1000);
00107 
00108     if ( !dbus_connection_send(sysbus_connection, message, NULL) )
00109         return 1;
00110 
00111     dbus_message_unref(message);        
00112     dbus_connection_flush(sysbus_connection);
00113 
00114     /* Do some sleep here so messages are not lost.. */
00115     usleep(500*1000);
00116 
00117     dbus_connection_disconnect(sysbus_connection);
00118 
00119     return 0;
00120 }
00121 

Generated on Sat Feb 7 22:11:46 2004 for HAL by doxygen 1.3.5