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

lsmnt.c

00001 /***************************************************************************
00002  * CVSID: $Id: lsmnt.c,v 1.22 2004/08/28 21:07:28 stefanb Exp $
00003  *
00004  * lsmnt.c : main() for 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 <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <getopt.h>
00035 
00036 #include <glib.h>
00037 #include <dbus/dbus-glib.h>
00038 #include <dbus/dbus-glib-lowlevel.h>
00039 
00040 #include <libmnt/libmnt.h>
00041 
00042 
00043 void main_loop(DBusConnection* dbus_connection, void *user_data);
00044 void volume_mounted(const char* udi, const char* mntpnt, void *user_data);
00045 void volume_unmounted(const char* udi, const char* mntpnt, void *user_data);
00046 void dbus_disconnect(void *user_data);
00047 
00048 
00050 void main_loop(DBusConnection* dbus_connection, void *user_data)
00051 {
00052     dbus_connection_setup_with_g_main(dbus_connection, NULL);
00053     return;
00054 }
00055 
00056 
00058 void volume_mounted(const char* udi, const char* mntpnt, void *user_data)
00059 {
00060     printf("volume_mounted('%s', '%s', 0x%p) called\n", udi, mntpnt, user_data);
00061     return;
00062 }
00063 
00064 
00066 void volume_unmounted(const char* udi, const char* mntpnt, void *user_data)
00067 {
00068     printf("volume_unmounted('%s', '%s', 0x%p) called\n", udi, mntpnt, user_data);
00069     return;
00070 }
00071 
00072 
00074 void dbus_disconnect(void *user_data)
00075 {
00076     GMainLoop *loop = (GMainLoop *) user_data;
00077     
00078     printf("Disconnected from DBUS ! exiting....\n");
00079     
00080     g_main_loop_quit(loop);
00081     
00082     return;
00083 }
00084 
00085 
00087 LIBMNTFUNCS mnt_functions = {   main_loop,
00088 //LIBMNTFUNCS mnt_functions = { NULL,
00089                                         volume_mounted, 
00090                                         volume_unmounted,
00091                                         dbus_disconnect};
00092 
00093 
00094 DBusDispatchStatus gstatus = DBUS_DISPATCH_COMPLETE;
00095 
00096 void _dispatch_status(DBusConnection *connection,DBusDispatchStatus new_status,void *data);
00097 void _dispatch_status(
00098     DBusConnection *connection,
00099     DBusDispatchStatus new_status,
00100     void *data)
00101 {
00102     gstatus = new_status;
00103     switch(new_status) {
00104         case DBUS_DISPATCH_DATA_REMAINS:
00105             printf("DBUS_DISPATCH_DATA_REMAINS\n");
00106             break;
00107         case DBUS_DISPATCH_COMPLETE:
00108             printf("DBUS_DISPATCH_COMPLETE\n");
00109             break;
00110         case DBUS_DISPATCH_NEED_MEMORY:
00111             printf("DBUS_DISPATCH_NEED_MEMORY\n");
00112             break;
00113         default:
00114             printf("UNKNOWN\n");
00115             break;
00116     }
00117 }
00118 
00119 dbus_bool_t _add_watch(DBusWatch *watch, void *data);
00120 dbus_bool_t _add_watch(DBusWatch *watch, void *data)
00121 {
00122     printf("_add_watch called.\n");
00123     if (!dbus_watch_get_enabled(watch)) {
00124         return TRUE;
00125     }
00126     return TRUE;
00127 }
00128 
00129 void _remove_watch(DBusWatch *watch, void *data);
00130 void _remove_watch(DBusWatch *watch, void *data)
00131 {
00132     printf("_remove_watch called.\n");
00133 }
00134 
00135 void _toggle_watch(DBusWatch *watch, void *data);
00136 void _toggle_watch(DBusWatch *watch, void *data)
00137 {
00138     printf("_toggle_watch called.\n");
00139     if (dbus_watch_get_enabled(watch)) {
00140         _add_watch(watch, data);
00141     } else {
00142         _remove_watch(watch, data);
00143     }
00144 }
00145 
00146 dbus_bool_t _add_timeout(DBusTimeout *timeout, void *data);
00147 dbus_bool_t _add_timeout(DBusTimeout *timeout, void *data)
00148 {
00149     printf("_add_timeout called.\n");
00150     printf("_add_timeout called. %d\n", dbus_timeout_get_interval(timeout));
00151     return TRUE;
00152 }
00153 
00154 void _remove_timeout(DBusTimeout *timeout, void *data);
00155 void _remove_timeout(DBusTimeout *timeout, void *data)
00156 {
00157     printf("_remove_timeout called.\n");
00158 }
00159 
00160 void _toggle_timeout(DBusTimeout *timeout, void *data);
00161 void _toggle_timeout(DBusTimeout *timeout, void *data)
00162 {
00163     printf("_toggle_timeout called.\n");
00164     if (dbus_timeout_get_enabled (timeout)) {
00165         _add_timeout(timeout, data);
00166     } else {
00167         _remove_timeout(timeout, data);
00168     }
00169 }
00170 
00171 void _wakeup_main(void *data);
00172 void _wakeup_main(void *data)
00173 {
00174     printf("_wakeup_main called.\n");
00175 }
00176 
00177 
00178 gboolean mntcheck(gpointer user_data);
00179 gboolean mntcheck(gpointer user_data)
00180 {
00181     int num_volumes=0;
00182     char **data = NULL;
00183     int i=0;
00184     char *udi = NULL;
00185     char *mntpnt = NULL;
00186 
00187     // get all data
00188     data = mnt_get_all_volumes(&num_volumes);
00189     if (data != NULL) {
00190         for (i=0; i<num_volumes; i++) {
00191             udi = data[i];
00192             if (udi != NULL) {
00193                 //printf("volume%d = '%s'\n", i, udi);
00194                 // get the mount point
00195                 mntpnt = mnt_get_mntpnt(udi);
00196                 if (mntpnt != NULL) {
00197                     printf("    -> mntpnt%d = '%s'\n", i, mntpnt);
00198                     free(mntpnt);
00199                     mntpnt = NULL;
00200                 }
00201                 free(udi);
00202                 udi = NULL;
00203             }
00204         }
00205         free(data);
00206         data = NULL;
00207         num_volumes = 0;
00208     }
00209     return TRUE;
00210 }
00211 
00212 
00219 int main(int argc, char* argv[])
00220 {
00221     GMainLoop* loop;
00222     int num_volumes=0;
00223     char **data = NULL;
00224     int i=0;
00225     char *udi = NULL;
00226     char *mntpnt = NULL;
00227     int res = -1;
00228 //  DBusConnection *c = NULL;
00229 
00230     // print version info
00231     fprintf(stderr, "lsmnt v" PACKAGE_VERSION "\n");
00232     
00233     // get glib main loop
00234     loop = g_main_loop_new (NULL, FALSE);
00235     
00236     // initialize mnt library
00237     if (mnt_init(&mnt_functions, loop) == -1) {
00238         fprintf(stderr, "error: mnt_init failed\n");
00239         exit(1);
00240     }
00241     
00242     // TESTING
00243 /*  c = _mnt_get_connection();
00244     dbus_connection_set_dispatch_status_function(c, _dispatch_status, NULL, NULL);
00245     dbus_connection_set_watch_functions(c, _add_watch, _remove_watch, 
00246                                                     _toggle_watch, NULL, NULL);
00247     dbus_connection_set_timeout_functions(c, _add_timeout, _remove_timeout, 
00248                                                     _toggle_timeout, NULL, NULL);
00249     dbus_connection_set_wakeup_main_function (c, _wakeup_main, NULL, NULL);
00250     */
00251 
00252     // add mount point checker
00253     g_timeout_add(1000, mntcheck, NULL);
00254     
00255     // get all data
00256     data = mnt_get_all_volumes(&num_volumes);
00257     if (data != NULL) {
00258         for (i=0; i<num_volumes; i++) {
00259             udi = data[i];
00260             if (udi != NULL) {
00261                 printf("volume%d = '%s'\n", i, udi);
00262                 // remount read/write
00263                 res = mnt_remount_rw(udi);
00264                 printf("remounting read/write '%s' returns %d\n", udi, res);
00265                 // remount read only
00266                 res = mnt_remount_rd(udi);
00267                 printf("remounting read only '%s' returns %d\n", udi, res);
00268                 // get the mount point
00269                 mntpnt = mnt_get_mntpnt(udi);
00270                 if (mntpnt != NULL) {
00271                     printf("    -> mntpnt = '%s'\n", mntpnt);
00272                     free(mntpnt);
00273                     mntpnt = NULL;
00274                 }
00275                 free(udi);
00276                 udi = NULL;
00277             }
00278         }
00279         free(data);
00280         data = NULL;
00281         num_volumes = 0;
00282     }
00283     
00284     // start program
00285     g_main_loop_run(loop);
00286     
00287 /*  while(1) {
00288         DBusDispatchStatus status = DBUS_DISPATCH_COMPLETE;
00289         status = dbus_connection_get_dispatch_status(c);
00290         switch(status) {
00291             case DBUS_DISPATCH_DATA_REMAINS:
00292                 printf("DBUS_DISPATCH_DATA_REMAINS\n");
00293                 break;
00294             case DBUS_DISPATCH_COMPLETE:
00295                 printf("DBUS_DISPATCH_COMPLETE\n");
00296                 break;
00297             case DBUS_DISPATCH_NEED_MEMORY:
00298                 printf("DBUS_DISPATCH_NEED_MEMORY\n");
00299                 break;
00300             default:
00301                 printf("UNKNOWN\n");
00302                 break;
00303         }
00304         if (status == DBUS_DISPATCH_DATA_REMAINS) {
00305             printf("dispatching ...\n");
00306             dbus_connection_dispatch(c);
00307         }
00308         sleep(1);
00309     }*/
00310     
00311     // free libraries resources
00312     mnt_quit();
00313     
00314     // exit
00315     return 0;
00316 }

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