Example #1
0
int conf_file_merge(void)
{
  GDir *confdir;
  struct stat fileinfo, dir;
  char *mode = 0, *ip = 0, *gateway = 0, *udev = 0;
  gchar *filename_full;
  const gchar *filename;
  GString *keyfile_string = NULL;
  GKeyFile *settingsfile;
  int ret = 0, test = 0, conffile_created = 0;

  confdir = g_dir_open(CONFIG_FILE_DIR, 0, NULL);
  if(!confdir)
  {
      log_debug("No configuration. Creating defaults.\n");
      create_conf_file();
      /* since there was no configuration at all there is no info to be merged */
      return (ret);
  }

  if (stat(FS_MOUNT_CONFIG_FILE, &fileinfo) == -1) 
  {
	/* conf file not created yet, make default and merge all */
      	create_conf_file();
	conffile_created = 1;
  }

  /* config file is created, so the dir must exists */
  if(stat(CONFIG_FILE_DIR, &dir))
  {
	log_warning("Directory still does not exists. FS might be ro/corrupted!\n");
	ret = 1;
	goto end;
  }

  /* st_mtime is changed by file modifications, st_mtime of a directory is changed by the creation or deletion of files in that directory.
  So if the st_mtime of the config file is equal to the directory time we can be sure the config is untouched and we do not need 
  to re-merge the config.
  */
  if(fileinfo.st_mtime == dir.st_mtime)
  {
	/* if a conffile was created, the st_mtime would have been updated so this check will miss information that might be there already,
	   like after a config file removal for example. So we run a merge anyway if we needed to create the conf file */
	if(!conffile_created)
		goto end;
  }
  log_debug("Merging/creating configuration.\n");
  keyfile_string = g_string_new(NULL);
  /* check each ini file and get contents */
  while((filename = g_dir_read_name(confdir)) != NULL)
  {
	if(!strstr(filename, ".ini"))
	{
		/* skip this file as it might be a dir or not an ini file */
		continue;
	}
	filename_full = g_strconcat(CONFIG_FILE_DIR, "/", filename, NULL);
	log_debug("filename = %s\n", filename_full);
	if(!strcmp(filename_full, FS_MOUNT_CONFIG_FILE))
	{
		/* store mode info to add it later as we want to keep it */
		mode = get_mode_setting();
		/* store udev path (especially important for the upgrade path */
		udev = find_udev_path();
		ip = get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY);
		gateway = get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY);
		continue;
	}
	/* load contents of file, if it fails skip to next one */
	settingsfile = g_key_file_new();
 	test = g_key_file_load_from_file(settingsfile, filename_full, G_KEY_FILE_KEEP_COMMENTS, NULL);
	if(!test)
	{
		log_debug("%d failed loading config file %s\n", test, filename_full);
		goto next;
	}
        log_debug("file data = %s\n", g_key_file_to_data(settingsfile, NULL, NULL));
	keyfile_string = g_string_append(keyfile_string, g_key_file_to_data(settingsfile, NULL, NULL));
	log_debug("keyfile_string = %s\n", keyfile_string->str);
  	g_key_file_free(settingsfile);

next:	
	g_free(filename_full);
  }

  if(keyfile_string)
  {
	/* write out merged config file */
  	/* g_file_set_contents returns 1 on succes, this function returns 0 */
  	ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile_string->str, -1, NULL);
	g_string_free(keyfile_string, TRUE);
	if(mode)
	{
		set_mode_setting(mode);
	}
	if(udev)
	{
		set_config_setting(UDEV_PATH_ENTRY, UDEV_PATH_KEY, udev);
	}
	/* check if no network data came from an ini file */
	if( get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY))
		goto cleanup;
	if(ip) 
		set_network_setting(NETWORK_IP_KEY, ip);
	if(gateway)
		set_network_setting(NETWORK_GATEWAY_KEY, gateway);
  }
  else
	ret = 1;
cleanup:
  if(mode)
	free(mode);
  if(udev)
	free(udev);
  if(ip)
	free(ip);
  if(gateway)
	free(gateway);
	
end:
  g_dir_close(confdir);
  return(ret);
}
Example #2
0
gboolean hwal_init(void)
{
  const gchar *udev_path = NULL, *udev_subsystem = NULL;
  struct udev_device *dev;
  int ret = 0;
	
  /* Create the udev object */
  udev = udev_new();
  if (!udev) 
  {
    log_err("Can't create udev\n");
    return FALSE;
  }
  
  udev_path = find_udev_path();
  if(udev_path)
  {
	dev = udev_device_new_from_syspath(udev, udev_path);
	g_free((gpointer *)udev_path);
  }
  else
  	dev = udev_device_new_from_syspath(udev, "/sys/class/power_supply/usb");
  if (!dev) 
  {
    log_err("Unable to find $power_supply device.");
    /* communicate failure, mainloop will exit and call appropriate clean-up */
    return FALSE;
  }
  else
  {
    dev_name = udev_device_get_sysname(dev);
    log_debug("device name = %s\n", dev_name);
  } 
  mon = udev_monitor_new_from_netlink (udev, "udev");
  if (!mon) 
  {
    log_err("Unable to monitor the netlink\n");
    /* communicate failure, mainloop will exit and call appropriate clean-up */
    return FALSE;
  }
  udev_subsystem = find_udev_subsystem();
  if(udev_subsystem)
  {
	  ret = udev_monitor_filter_add_match_subsystem_devtype(mon, udev_subsystem, NULL);
	  g_free((gpointer *)udev_subsystem);
  }
  else
	  ret = udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
  if(ret != 0)
  {
    log_err("Udev match failed.\n");
    return FALSE;
  }
  ret = udev_monitor_enable_receiving (mon);
  if(ret != 0)
  { 
     log_err("Failed to enable monitor recieving.\n");
     return FALSE;
  }

  /* check if we are already connected */
  udev_parse(dev);
  
  iochannel = g_io_channel_unix_new(udev_monitor_get_fd(mon));
  watch_id = g_io_add_watch_full(iochannel, 0, G_IO_IN, monitor_udev, NULL,notify_issue);

  /* everything went well */
  return TRUE;
}
Example #3
0
int conf_file_merge(void)
{
  GDir *confdir;
  struct stat fileinfo, dir;
  const gchar *filename, *mode = 0;
  gchar *filename_full;
  GString *keyfile_string = NULL;
  GKeyFile *settingsfile;
  int ret = 0, test = 0;
#ifdef UDEV
  const gchar *udev = 0;
#endif /* UDEV */

  confdir = g_dir_open(CONFIG_FILE_DIR, 0, NULL);
  if(!confdir)
  {
      log_debug("No configuration. Creating defaults.\n");
      create_conf_file();
      return (ret);
  }

  if (stat(FS_MOUNT_CONFIG_FILE, &fileinfo) == -1) 
  {
	/* conf file not created yet, make default and merge all */
      	create_conf_file();
  }

  /* config file is created, so the dir must exists */
  stat(CONFIG_FILE_DIR, &dir);

  /* st_mtime is changed by file modifications, st_mtime of a directory is changed by the creation or deletion of files in that directory.
  So if the st_mtime of the config file is equal to the directory time we can be sure the config is untouched and we do not need 
  to re-merge the config.
  */
  if(fileinfo.st_mtime == dir.st_mtime)
	return 0;

  log_debug("Merging/creating configuration.\n");
  keyfile_string = g_string_new(NULL);
  /* check each ini file and get contents */
  while((filename = g_dir_read_name(confdir)) != NULL)
  {
	log_debug("filename = %s\n", filename);
	filename_full = g_strconcat(CONFIG_FILE_DIR, "/", filename, NULL);
	if(!strcmp(filename_full, FS_MOUNT_CONFIG_FILE))
	{
		/* store mode info to add it later as we want to keep it */
		mode = get_mode_setting();
#ifdef UDEV
		/* store udev path (especially important for the upgrade path */
		udev = find_udev_path();
#endif /* UDEV */
		break;
	}
	/* load contents of file, if it fails skip to next one */
	settingsfile = g_key_file_new();
 	test = g_key_file_load_from_file(settingsfile, filename_full, G_KEY_FILE_KEEP_COMMENTS, NULL);
	if(!test)
	{
		log_debug("%d failed loading config file %s\n", test, filename_full);
		g_free(filename_full);
		break;
	}
	g_free(filename_full);
        log_debug("file data = %s\n", g_key_file_to_data(settingsfile, NULL, NULL));
	keyfile_string = g_string_append(keyfile_string, g_key_file_to_data(settingsfile, NULL, NULL));
	log_debug("keyfile_string = %s\n", keyfile_string->str);
  	g_key_file_free(settingsfile);
  }

  if(keyfile_string)
  {
	/* write out merged config file */
  	/* g_file_set_contents returns 1 on succes, this function returns 0 */
  	ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile_string->str, -1, NULL);
	g_string_free(keyfile_string, TRUE);
	if(mode)
	{
		set_mode_setting(mode);
	}
#ifdef UDEV
	if(udev)
	{
		set_config_setting(UDEV_PATH_ENTRY, UDEV_PATH_KEY, udev);
	}
#endif /* UDEV */
  }
  else
	ret = 1;
  if(mode)
  	free((void *)mode);
#ifdef UDEV
  if(udev)
	free((void *)udev);
#endif /* UDEV */

  g_dir_close(confdir);
  return(ret);
}