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

mntdbus.c

00001 /***************************************************************************
00002  * CVSID: $Id: mntdbus.c,v 1.3 2004/12/19 00:13:08 stefanb Exp $
00003  *
00004  * mntdbus.c : Sending dbus signal to MNT daemon
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <sbambach@gmx.net>
00007  *
00008  * Licensed under the GNU General Public License 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025  
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include <stdlib.h>
00031 
00032 #include "mntdbus.h"
00033 #include "errmanager.h"
00034 #include "libmnt/libmnt.h"
00035 #include "dbug_mem.h"
00036 
00037 
00051 char *mntdbus_getenv(char *name)
00052 {
00053     char *value = NULL;
00054     
00055     value = getenv(name);
00056     if (value == NULL) {
00057         MSG_ERR("No '%s' in environment found!", name);
00058         return NULL;
00059     }
00060     if (strlen(value)==0) {
00061         MSG_ERR("'%s' is empty!", name);
00062         return NULL;
00063     }
00064     return value;
00065 }
00066 
00067 
00072 DBusConnection *mntdbus_init(void)
00073 {
00074     DBusConnection *conn = NULL;
00075     DBusError error;
00076 
00077     // connect to dbus daemon and register myself
00078     dbus_error_init(&error);
00079     conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
00080     if (conn==NULL) {
00081         MSG_ERR("Couldn't connect to DBUS, error %s: %s", 
00082             error.name, error.message);
00083         goto error;
00084     }
00085     
00086     // test for running service from mntd
00087     if (!dbus_bus_service_exists(conn, DBUS_SERVICE_MNT, &error)) {
00088         MSG_ERR("Service '%s' doesn't exist !\n", DBUS_SERVICE_MNT);
00089         goto error;
00090     }
00091     
00092     if (dbus_bus_acquire_service(conn, DBUS_SERVICE_MNT, 0, &error) == -1) {
00093         MSG_ERR("Error Acquiring service '" DBUS_SERVICE_MNT "'\n");
00094         if (dbus_error_is_set(&error)) {
00095             MSG_ERR("Can't acquire '" DBUS_SERVICE_MNT "' service, error %s: %s'",
00096                 error.name, error.message);
00097         }
00098         goto error;
00099     }
00100 
00101     goto cleanup;
00102     
00103 error:
00104     if (conn != NULL) {
00105         dbus_connection_disconnect(conn);
00106     }
00107     conn = NULL;
00108     
00109 cleanup:    
00110     if (dbus_error_is_set(&error)) {
00111         dbus_error_free(&error);
00112     }
00113 
00114     return conn;
00115 }
00116 
00117 
00122 void mntdbus_quit(DBusConnection* conn)
00123 {
00124     // check parameter
00125     if (conn == NULL) {
00126         MSG_ERR("Connection is NULL ! Cannot disconnect.");
00127         return;
00128     }
00129     
00130     dbus_connection_disconnect(conn);
00131     
00132     return;
00133 }
00134 
00135 
00145 int _mntdbus_signal(DBusConnection* conn, char *signalname, char *subsystem, char *devname)
00146 {
00147     DBusMessage *message = NULL;
00148     DBusMessageIter it;
00149     char *dbuspath = NULL;
00150     char *dbusinterface = NULL;
00151 
00152     // check parameter
00153     if (conn == NULL) {
00154         MSG_ERR("Connection is NULL ! Cannot send device signal ('%s', '%s', '%s').", 
00155                   signalname, subsystem, devname);
00156         return -1;
00157     }
00158     
00159     // send signal
00160     if (strcmp(subsystem, "block") == 0) {
00161         dbuspath = DBUS_PATH_MNT_BLOCK;
00162         dbusinterface = DBUS_INTERFACE_MNT_BLOCK;
00163     } else if (strcmp(subsystem, "disc") == 0) {
00164         dbuspath = DBUS_PATH_MNT_DISC;
00165         dbusinterface = DBUS_INTERFACE_MNT_DISC;
00166     } else if (strcmp(subsystem, "tty") == 0) {
00167         dbuspath = DBUS_PATH_MNT_TTY;
00168         dbusinterface = DBUS_INTERFACE_MNT_TTY;
00169     }
00170     
00171     // check for errors
00172     if ((dbuspath==NULL) || (dbusinterface==NULL)) {
00173         return -1;
00174     }
00175     
00176     // create message
00177     message = dbus_message_new_signal(
00178                                             dbuspath, 
00179                                             dbusinterface,
00180                                             signalname);
00181     if (message==NULL) {
00182         MSG_EMERG("Out of memory");
00183         return -1;
00184     }
00185     
00186     // append parameter
00187     dbus_message_iter_init(message, &it);
00188     dbus_message_iter_append_string(&it, devname);
00189     
00190     // send it
00191     if (!dbus_connection_send(conn, message, NULL)) {
00192         MSG_EMERG("error broadcasting message");
00193         return -1;
00194     }
00195     
00196     dbus_message_unref(message);
00197     
00198     dbus_connection_flush(conn);
00199     
00200     return 0;
00201 }
00202 
00203 
00211 int mntdbus_add(DBusConnection* conn, char *subsystem, char *devname)
00212 {
00213     MSG_DEBUG("mntdbus_add('%s', '%s') called.", subsystem, devname);
00214 
00215     // check parameter
00216     if (conn == NULL) {
00217         MSG_ERR("Connection is NULL ! Cannot send Add('%s', '%s').", 
00218                   subsystem, devname);
00219         return -1;
00220     }
00221 
00222     // send signal
00223     return _mntdbus_signal(conn, "Add", subsystem, devname);
00224 }
00225 
00226 
00234 int mntdbus_remove(DBusConnection* conn, char *subsystem, char *devname)
00235 {
00236     MSG_DEBUG("mntdbus_remove('%s', '%s') called.", subsystem, devname);
00237 
00238     // check parameter
00239     if (conn == NULL) {
00240         MSG_ERR("Connection is NULL ! Cannot send Remove('%s', '%s').", 
00241                   subsystem, devname);
00242         return -1;
00243     }
00244     
00245     // send signal
00246     return _mntdbus_signal(conn, "Remove", subsystem, devname);
00247 }
00248 
00249 

Generated on Wed Mar 30 13:43:27 2005 for Mntd by  doxygen 1.3.9.1