seg fault when receiving HAL signals (learning HAL API part 2)

Alexander Bierbrauer abierbrauer at gmx.org
Wed Jun 15 06:51:41 PDT 2005


Hey,

after studying the code of gnome.volume-manager, I've started to implement 
something similar for excercise.

So far it was no problem, but now my program crashes when it shoud receive 
signals from HAL. 

Here's some output from ddd:
run
hal_mainloop_integration
fired event: /dev/sdb2
fired event: /dev/sdb1
fired event: /dev/hda2

Program received signal SIGSEGV, Segmentation fault.
0x08088167 in ?? ()
(gdb) frame 3
#3  0xb7b3cd0f in g_main_depth () from /usr/lib/libglib-2.0.so.0
(gdb) frame 1
#1  0xb7ba3232 in dbus_connection_dispatch () from /usr/lib/libdbus-1.so.0
(gdb) Quit
(gdb) 


It would be great if someone could have a short look over my source. There's 
for sure one thing I'm not aware of...

---source begin

// main.cpp
int main(int argc,char **argv)
{
 gtk_init(&argc,&argv);

 VolumeManager *volManager = VolumeManager::instance();
 volManager->initialize();

 gtk_main();
 return 0;
}

//volumemananger.cpp
#include "volumemanager.h"
#include "volumeevent.h"

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

/**
 * BEGIN: functions for interacting with libhal
*/
static void hal_device_added (LibHalContext *ctx , const char *udi)
{
 printf("new device added: %s\n",udi);
}

static void hal_device_removed (LibHalContext *ctx , const char *udi)
{
 printf("hal_device_removed\n");
}

static void hal_device_new_capability (LibHalContext *ctx ,const char *udi , 
const char *capability )
{
 printf("hal_device_new_capability\n");
}

static void hal_device_lost_capability (LibHalContext *ctx , const char 
*udi ,const char *capability )
{
 printf("hal_device_lost_capability\n");
}

static void hal_property_modified (LibHalContext *ctx , const char *udi,const 
char *key,dbus_bool_t is_removed , dbus_bool_t is_added )
{
 printf("hal_property_modified\n");
}

static void hal_device_condition (LibHalContext *ctx ,const char *udi ,const 
char *condition_name , DBusMessage * message )
{
 printf("hal_device_condition\n");
}

static void hal_mainloop_integration (LibHalContext *ctx ,DBusConnection * 
dbus_connection)
{
 printf("hal_mainloop_integration\n");
 dbus_connection_setup_with_g_main (dbus_connection, NULL);
}

/**
 * END: functions for interacting with libhal
*/

VolumeManager::VolumeManager()
{
 m_pHalContext = NULL;
}

VolumeManager::~VolumeManager()
{
}

/**
 * initializes the VolumeManager
 * @return true on success
*/
bool VolumeManager::initialize()
{
 char **devices;
 int nr;

 // setup for dynamic binding of the hal functions ....
 LibHalFunctions hal_functions = { hal_mainloop_integration,
       hal_device_added,
       hal_device_removed,
       hal_device_new_capability,
       hal_device_lost_capability,
       hal_property_modified,
       hal_device_condition };


 m_pHalContext = hal_initialize (&hal_functions, FALSE);
 if (!m_pHalContext) {
  printf("failed to initialize HAL!\n");
  return false;
 }

 if (hal_device_property_watch_all (m_pHalContext)) {
  printf("failed to watch all HAL properties!\n");
  hal_shutdown (m_pHalContext);
  return false;
 }

 /*
  * Do something to ping the HAL daemon - the above functions will
  * succeed even if hald is not running, so long as DBUS is.  But we
  * want to exit silently if hald is not running, to behave on
  * pre-2.6 systems.
  */
 devices = hal_get_all_devices (m_pHalContext, &nr);
 if (!devices) {
  printf("seems that HAL is not running\n");
  hal_shutdown (m_pHalContext);
  return false;
 }
 hal_free_string_array (devices);

 checkMounts(); // everything went fine so far, so let's check the mounts
 return true;
}

/**
 * checks all mounts after initializing the VolumeManager
 * if an unmounted volume gets noticed, an event will be fired for the volume
 */
void VolumeManager::checkMounts()
{
 int i;
 int num_volumes;
 char **volumes;
 char *udi;
 char *device_file;

 if(m_pHalContext == NULL)
 {
  printf("void VolumeManager::checkMounts() -> hal device context is not 
initialized!\n");
  return;
 }

 volumes = hal_find_device_by_capability (m_pHalContext, "volume", 
&num_volumes);
 for (i = 0; i < num_volumes; i++) {
  udi = volumes [i];

  /* don't attempt to mount already mounted volumes */
  if (!hal_device_property_exists (m_pHalContext, udi, 
      "volume.is_mounted") ||
      hal_device_get_property_bool (m_pHalContext, udi, 
        "volume.is_mounted"))
   continue;

  /* only mount if the block device got a sensible filesystem */
  if (!hal_device_property_exists (m_pHalContext, udi, 
      "volume.fsusage") ||
      strcmp (hal_device_get_property_string (m_pHalContext, udi, 
           "volume.fsusage"), 
       "filesystem") != 0)
   continue;

  device_file = hal_device_get_property_string (m_pHalContext, udi,
             "block.device");

  if (device_file != NULL ) {

   VolumeEvent *event = new VolumeEvent(device_file);

   fireEvent(event);
   delete event;

   //gvm_device_mount (device_file);

   hal_free_string (device_file);
  } else
   printf ("no device_file for udi=%s\n", udi);
 }

 hal_free_string_array (volumes);
}

/**
 * fires a VolumeEvent to all registered handlers when a HAL event got noticed 
by the VolumeManager
 */
void VolumeManager::fireEvent(VolumeEvent* e)
{
 printf("fired event: %s\n",e->infoString().c_str());
}

-- end of source

any help is welcome !!!

regards,

Alexander Bierbrauer
_______________________________________________
hal mailing list
hal at lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/hal



More information about the Hal mailing list