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

mntudev.c

00001 /***************************************************************************
00002  * CVSID: $Id: mntudev.c,v 1.3 2004/12/19 00:13:08 stefanb Exp $
00003  *
00004  * mntudev.c : Sending dbus signal to MNT daemon from udev call
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 #include <glib.h>
00032 
00033 #include "mntdbus.h"
00034 #include "mntudev.h"
00035 #include "errmanager.h"
00036 #include "libmnt/libmnt.h"
00037 #include "dbug_mem.h"
00038 
00039 
00057 int main(int argc, char **argv)
00058 {
00059     DBusConnection *conn = NULL;
00060     char *subsystem = NULL;
00061     char *action = NULL;
00062     char *devname = NULL;
00063     char *devpath = NULL;
00064     gchar **gparts = NULL;
00065     gchar **gparts_it = NULL;
00066     int gparts_idx = 0;
00067     int res = 0;
00068     
00069     // initialize error manager
00070     emInit(LOG_DEBUG, EM_TYPE_SYSLOG, NULL, NULL, NULL, "mntudev");
00071     
00072     // check for devpath
00073     devpath = mntdbus_getenv("DEVPATH");
00074     if (devpath == NULL) {
00075         goto error;
00076     }
00077     
00078     // check for action
00079     action = mntdbus_getenv("ACTION");
00080     if (action == NULL) {
00081         goto error;
00082     }
00083     
00084     // check for devname
00085     devname = mntdbus_getenv("DEVNAME");
00086     if (devname == NULL) {
00087         goto error;
00088     }
00089     
00090     // get subsystem
00091     gparts = g_strsplit(devpath, G_DIR_SEPARATOR_S, 0);
00092     if (gparts == NULL) {
00093         MSG_ERR("Out of memory.");
00094         goto error;
00095     }
00096     gparts_it = gparts;
00097     while (*gparts_it != NULL) {
00098         // check for first path of e.g. '/block/...'
00099         if (gparts_idx == 1) {
00100             subsystem = *gparts_it;
00101         }
00102         gparts_it++;
00103         gparts_idx++;
00104     }
00105     if (subsystem == NULL) {
00106         MSG_ERR("DEVPATH '%s' in invalid.", devpath);
00107         goto error;
00108     }
00109 
00110     // check handled types
00111     if (strcmp(subsystem, "block")!=0 && 
00112          strcmp(subsystem, "tty")!=0 &&
00113          strcmp(subsystem, "disc")!=0) {
00114         MSG_ERR("type '%s' not handled by mntudev (only 'tty', 'block' and 'disc').", subsystem);
00115         goto error;
00116     }
00117     
00118     // connect to dbus
00119     conn = mntdbus_init();
00120     if (conn == NULL) {
00121         MSG_ERR("Couldn't connect to dbus!");
00122         goto error;
00123     }
00124     
00125     // handle action
00126     if (strcmp(action, "add")==0) {
00127         if (mntdbus_add(conn, subsystem, devname) != 0) {
00128             MSG_ERR("Error sending add message");
00129             goto error;
00130         }
00131     } else if (strcmp(action, "remove")==0) {
00132         if (mntdbus_remove(conn, subsystem, devname) != 0) {
00133             MSG_ERR("Error sending remove message");
00134             goto error;
00135         }
00136     } else {
00137         MSG_ERR("Unknown ACTION '%s'.", action);
00138         goto error;
00139     }
00140     
00141     goto cleanup;
00142     
00143 error:
00144     if (conn!=NULL) {
00145         mntdbus_quit(conn);
00146         conn = NULL;
00147     }
00148     res = -1;
00149     
00150 cleanup:    
00151     if (gparts != NULL) {
00152         g_strfreev(gparts);
00153         gparts = NULL;
00154     }
00155 
00156     return res;
00157 }
00158 
00159 

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