Esempio n. 1
0
/** 
 * userspace_init:
 * @iface:	allocated struct with the userspace interface
 * @cpus:	list of CPU cores
 *
 * Returns:     TRUE/FALSE
 *
 * Inits one userspace interface with the given cores list. iface has to
 * be allocated before passing it to that fucntion 
 */
gboolean userspace_init(struct userspace_interface *iface, GSList *cpus)
{
	if (iface == NULL)
		return FALSE;

	iface->demotion		= g_array_new(TRUE, TRUE, sizeof(unsigned));
	iface->speeds_kHz	= g_array_new(TRUE, TRUE, sizeof(unsigned));
	iface->last_step	= -1;
	iface->current_speed	= 0;
	iface->cpus		= cpus;
	iface->prev_cpu_load	= 50;
	iface->base_cpu		= GPOINTER_TO_INT(cpus->data);
	
	if (!write_governor(USERSPACE_STRING, GPOINTER_TO_INT(cpus->data))) {
		HAL_WARNING(("Could not set userspace governor."));
		return FALSE;
	}

	if (!read_frequencies(iface)) {
		HAL_WARNING(("Could not read available frequencies"));
		return FALSE;
	}

	return TRUE;
}
Esempio n. 2
0
File: ids.c Progetto: bbidulock/hal
/**  
 *  usb_ids_load:
 *  @path:               Path of the usb.ids file, e.g. /usr/share/hwdata/usb.ids
 *  Returns:             #TRUE if the file was succesfully loaded
 *
 *  Load the USB database used for mapping vendor, product, subsys_vendor
 *  and subsys_product numbers into names.
 */
static dbus_bool_t
usb_ids_load (const char *path)
{
	int fd;
	struct stat statbuf;
	gboolean ret;

	ret = FALSE;

	if (stat (path, &statbuf) != 0) {
		HAL_WARNING (("Couldn't stat usb.ids file '%s', errno=%d: %s", path, errno, strerror (errno)));
		goto out;
	}
	usb_ids_len = statbuf.st_size;

	fd = open (path, O_RDONLY);
	if (fd < 0) {
		HAL_WARNING (("Couldn't open usb.ids file '%s', errno=%d: %s", path, errno, strerror (errno)));
		goto out;
	}

	usb_ids = mmap (NULL, usb_ids_len, PROT_READ, MAP_SHARED, fd, 0);
	if (usb_ids == MAP_FAILED) {
		HAL_WARNING (("Couldn't mmap usb.ids file '%s', errno=%d: %s", path, errno, strerror (errno)));
		close (fd);
		goto out;
	}

	ret = TRUE;

	close (fd);
out:
	return ret;
}
Esempio n. 3
0
int
main (int argc, char *argv[])
{
	DBusConnection *dbus_connection;
	DBusError error;
	const char *commandline;

	hal_set_proc_title_init (argc, argv);

	setup_logger ();

	dbus_error_init (&error);
	if ((ctx = libhal_ctx_init_direct (&error)) == NULL) {
		HAL_WARNING (("Unable to init libhal context"));
		goto out;
	}

	if ((dbus_connection = libhal_ctx_get_dbus_connection(ctx)) == NULL) {
		HAL_WARNING (("Cannot get DBus connection"));
		goto out;
	}

	if ((commandline = getenv ("SINGLETON_COMMAND_LINE")) == NULL) {
		HAL_WARNING (("SINGLETON_COMMAND_LINE not set"));
		goto out;
	}

	libhal_ctx_set_singleton_device_added (ctx, add_device);
	libhal_ctx_set_singleton_device_removed (ctx, remove_device);

	dbus_connection_setup_with_g_main (dbus_connection, NULL);
	dbus_connection_set_exit_on_disconnect (dbus_connection, 0);


	if (!libhal_device_singleton_addon_is_ready (ctx, commandline, &error)) {
		goto out;
	}

	leds = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

	gmain = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (gmain);

	return 0;

out:
	HAL_DEBUG (("An error occured, exiting cleanly"));
	
	LIBHAL_FREE_DBUS_ERROR (&error);

	if (ctx != NULL) {
		libhal_ctx_shutdown (ctx, &error);
		LIBHAL_FREE_DBUS_ERROR (&error);
		libhal_ctx_free (ctx);
	}

	return 0;
}
Esempio n. 4
0
HalProperty *
hal_property_new_string (const char *key, const char *value)
{
	HalProperty *prop;
	char *endchar;
	gboolean validated = TRUE;

	prop = g_new0 (HalProperty, 1);

	prop->type = HAL_PROPERTY_TYPE_STRING;
	prop->key = g_strdup (key);
	prop->v.str_value = g_strdup (value != NULL ? value : "");

	while (!g_utf8_validate (prop->v.str_value, -1,
				 (const char **) &endchar)) {
		validated = FALSE;
		*endchar = '?';
	}

	if (!validated) {
		HAL_WARNING (("Key '%s' has invalid UTF-8 string '%s'",
			      key, prop->v.str_value));
	}

	return prop;
}
Esempio n. 5
0
void
hal_property_set_string (HalProperty *prop, const char *value)
{
	char *endchar;
	gboolean validated = TRUE;

	g_return_if_fail (prop != NULL);
	g_return_if_fail (prop->type == HAL_PROPERTY_TYPE_STRING ||
			  prop->type == HAL_PROPERTY_TYPE_INVALID);

	prop->type = HAL_PROPERTY_TYPE_STRING;
	if (prop->v.str_value != NULL)
		g_free (prop->v.str_value);
	prop->v.str_value = g_strdup (value);

	while (!g_utf8_validate (prop->v.str_value, -1,
				 (const char **) &endchar)) {
		validated = FALSE;
		*endchar = '?';
	}

	if (!validated) {
		HAL_WARNING (("Key '%s' has invalid UTF-8 string '%s'",
			      prop->key, value));
	}
}
Esempio n. 6
0
int
drvctl_list(const gchar *name, struct devlistargs *laa)
{
	size_t children;

	/* HAL_INFO (("drvctl_list: %s", name)); */

	memset (laa, 0, sizeof (*laa));
	strlcpy (laa->l_devname, name, sizeof (laa->l_devname));
	if (ioctl (drvctl_fd, DRVLISTDEV, laa) == -1) {
		HAL_INFO (("DRVLISTDEV/1 failed: %s", strerror(errno)));
		return -1;
	}
	children = laa->l_children;
	/* HAL_INFO (("%s: found %d children", name, children)); */
	if (children == 0)
		return -1;
	laa->l_childname = malloc (children * sizeof (laa->l_childname[0]));
	if (laa->l_childname == NULL) {
		HAL_INFO (("drvctl_list couldn't allocate %d children: %s\n", children, strerror(errno)));
		return -1;
	}
	if (ioctl (drvctl_fd, DRVLISTDEV, laa) == -1) {
		HAL_INFO (("DRVLISTDEV/2 failed: %s", strerror(errno)));
		return -1;
	}
	if (children != laa->l_children)
		HAL_WARNING (("DRVLISTDEV/3 expected %d children, got %d", children, laa->l_childname));
	return 0;
}
Esempio n. 7
0
/* Setting current brightness of the led */
static int
set_leds_brightness (const char *udi, int level)
{
	int fd, l, ret;
	char path[256];
	char buf[5];	/* Assume we don't need more */
	char *sysfs_path;
	DBusError error;

	ret = -1;

	if (!g_hash_table_lookup_extended (leds, udi, NULL, (gpointer) &sysfs_path)) {
		return -1;
	}

	dbus_error_init (&error);

	if (level > libhal_device_get_property_int (ctx, udi, "leds.num_levels", &error) -1) {
		HAL_DEBUG (("Requested level is higher than max level (check leds.num_levels)."));
		return -1;
	}

	snprintf (path, sizeof (path), "%s/brightness", sysfs_path);

	fd = open (path, O_WRONLY);
	if (fd < 0) {
		HAL_WARNING(("Could not open '%s' for '%s', error='%s'", sysfs_path, udi, strerror(errno)));
		goto out;
	}

	if ((l = snprintf (buf, 4, "%d", level)) < 4) {
		if (write (fd, buf, l) < 0) {
			HAL_WARNING(("Could not write '%s' to '%s'", buf , sysfs_path));
		} else {
			/* everything okay */
			ret = level;
		}
	}

out:
	if (fd >= 0)
		close (fd);

	return ret;
}
Esempio n. 8
0
static int
nds_claim_interface(LibHalContext *ctx, char *udi, DBusError *error)
{
	DBusConnection *connection;
	char *interface_xml =
		"<method name=\"EnablePrinterScanningViaSNMP\">\n"
		"  <arg name=\"interval\" direction=\"in\" type=\"i\"/>\n"
		"  <arg name=\"community\" direction=\"in\" type=\"s\"/>\n"
		"  <arg name=\"network\" direction=\"in\" type=\"s\"/>\n"
		"  <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
		"</method>\n"
		"<method name=\"DisablePrinterScanningViaSNMP\">\n"
		"  <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
		"</method>\n"
		"<method name=\"ScanForPrintersViaSNMP\">\n"
		"  <arg name=\"community\" direction=\"in\" type=\"s\"/>\n"
		"  <arg name=\"network\" direction=\"in\" type=\"s\"/>\n"
		"  <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
		"</method>\n";

	HAL_DEBUG(("nds_claim_interface(0x%8.8x, %s, 0x%8.8x): %s",
			ctx, udi, error, DBUS_INTERFACE));

	if ((connection = libhal_ctx_get_dbus_connection(ctx)) == NULL) {
		HAL_WARNING(("Could not get DBus connection"));
		return (-1);
	}

	if (libhal_device_claim_interface(ctx, udi,
			DBUS_INTERFACE, interface_xml, error) == 0) {
		HAL_WARNING(("Could not claim interface: %s", error->message));
		return (-1);
	}

	dbus_connection_setup_with_g_main(connection, NULL);
	dbus_connection_add_filter(connection, nds_filter_function, ctx, NULL);
	dbus_connection_set_exit_on_disconnect(connection, 0);

	return (0);
}
Esempio n. 9
0
static void
hf_volume_get_mounts (struct statfs **mounts, int *n_mounts)
{
  g_return_if_fail(mounts != NULL);
  g_return_if_fail(n_mounts != NULL);

  *n_mounts = getmntinfo(mounts, MNT_NOWAIT);
  if (*n_mounts == 0)
    {
      HAL_WARNING(("unable to get list of mounted filesystems: %s", g_strerror(errno)));
      *mounts = NULL;
    }
}
Esempio n. 10
0
static gboolean
drvctl_iochannel_data (GIOChannel *source,
                    GIOCondition condition,
                    gpointer user_data)
{
	prop_dictionary_t ev;
	const char *event, *device;
	int res;

	HAL_INFO (("drvctl_iochannel_data"));

	res = prop_dictionary_recv_ioctl (drvctl_fd, DRVGETEVENT, &ev);
	if (res) {
		HAL_WARNING (("DRVGETEVENT failed: %s", strerror(errno)));
		return FALSE;
	}

	if (!prop_dictionary_get_cstring_nocopy (ev, "event", &event)) {
		HAL_WARNING (("DRVGETEVENT missing \"event\" parameter"));
		goto done;
	}
	if (!prop_dictionary_get_cstring_nocopy (ev, "device", &device)) {
		HAL_WARNING (("DRVGETEVENT missing \"device\" parameter"));
		goto done;
	}

	HAL_INFO (("DRVGETEVENT event=%s device=%s", event, device));

	if (strcmp (event, "device-attach") == 0) {
		drvctl_dev_add (device);
	} else {
		drvctl_dev_remove (device);
	}

done:
	prop_object_release(ev);
	return TRUE;
}
Esempio n. 11
0
void
devinfo_callouts_remove_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
        void *end_token = (void *) userdata1;

        HAL_INFO (("Remove callouts completed udi=%s", hal_device_get_udi (d)));

        if (!hal_device_store_remove (hald_get_gdl (), d)) {
                HAL_WARNING (("Error removing device"));
        }
        g_object_unref (d);

        hotplug_event_end (end_token);
}
Esempio n. 12
0
/********************* userspace interface *********************/
static gboolean write_speed(unsigned kHz, int cpu_id)
{
	char		*speed_file	= NULL;

	if (!cpu_online(cpu_id))
		return FALSE;

	speed_file = g_strdup_printf(SYSFS_SCALING_SETSPEED_FILE, cpu_id); 
        if(!write_line(speed_file, "%u", kHz)){
                HAL_WARNING(("Could not set speed to: %u kHz; %s", kHz, strerror(errno)));
		g_free(speed_file);
                return FALSE;
        }
	g_free(speed_file);
	HAL_DEBUG(("Speed set to: %uKHz  for CPU %d", kHz, cpu_id));

	return TRUE;
}
Esempio n. 13
0
/** 
 * get_cpu_load:
 * @cpu_id:	The ID of the CPU
 * 
 * Returns:     returns current cpuload which has been caluclated before 
 *
 * To get the current CPU load for a given CPU.
 */
static int get_cpu_load(const int cpu_id)
{
	if (cpu_id < -1) {
		errno = EINVAL;
		return -10;
	}

	if (cpuload.load == NULL) {
		HAL_WARNING(("cpuload.load uninitialized"));
		errno = EFAULT;
		return -40;
	}

	if (cpu_id >= cpuload.num_cpus) {
		errno = ENODEV;
		return -30;
	}

	return cpuload.load[cpu_id + 1];
}
Esempio n. 14
0
/* Getting current brightness */
static int
get_leds_brightness (const char *udi)
{
	FILE *f;
	char buf[64];
	char path[256];
	char *sysfs_path;
	int brightness;
	
	f = NULL;

	if (!g_hash_table_lookup_extended (leds, udi, NULL, (gpointer) &sysfs_path)) {
		return -1;
	}

	snprintf (path, sizeof (path), "%s/brightness", sysfs_path);

        if ((f = fopen (path, "rb")) == NULL) {
		HAL_WARNING(("Could not read brightness from '%s' for device '%s'", path, udi));
		return -1;
	}

	if (fgets (buf, sizeof (buf), f) == NULL) {
		HAL_ERROR (("Cannot read from '%s' for device '%s'", path, udi));
                goto out;
        }

	errno = 0;
	brightness = strtol (buf, NULL, 10);
	if (errno != 0) { 
		brightness = -1;
	} 

out:
        if (f != NULL)
                fclose (f);

        return brightness;
}
Esempio n. 15
0
static void
drvctl_dev_add(gchar *name)
{
	HalDevice *parent, *d;
	gchar pdevnode[512];

	if (drvctl_find_parent (name, pdevnode) == FALSE) {
		HAL_INFO (("dev_add: name=%s orphan", name));
		parent = NULL;
	} else {
		parent = hal_device_store_match_key_value_string (
		    hald_get_gdl(), "netbsd.device", pdevnode);
		if (parent == NULL)
			HAL_INFO (("dev_add: name=%s but netbsd.device=%s not found",name, pdevnode));
	}

	d = devinfo_add_node (parent, name);
	if (d == NULL)
		HAL_WARNING (("dev_add: couldn't add %s node (parent=%p)", name, parent));

	hotplug_event_process_queue ();
}
Esempio n. 16
0
static DBusHandlerResult
nds_filter_function(DBusConnection *connection, DBusMessage *message,
		void *user_data)
{
	LibHalContext *ctx = user_data;
	DBusMessage *reply;
	DBusError error;
	const char *member = dbus_message_get_member(message);
	const char *path = dbus_message_get_path(message);
	int rc = -1;

	dbus_error_init(&error);

	HAL_DEBUG(("DBus message: %s, %s ", member, path));

	if (dbus_message_is_method_call(message,
				DBUS_INTERFACE, "EnablePrinterScanningViaSNMP")) {
		int interval = -1;
		char *udi = getenv("UDI");
		char *community = "public";
		char *network = "0.0.0.0";

		dbus_message_get_args(message, &error,
				DBUS_TYPE_INT32, &interval,
				DBUS_TYPE_STRING, &community,
				DBUS_TYPE_STRING, &network,
				DBUS_TYPE_INVALID);

		if (strcmp(network, "0.0.0.0") == 0)
			network = NULL;

		rc = nds_EnablePrinterScanningViaSNMP(ctx, udi, interval,
				community, network);
	} else if (dbus_message_is_method_call(message,
				DBUS_INTERFACE, "ScanForPrintersViaSNMP")) {
		int interval = -1;
		char *udi = getenv("UDI");
		char *community = "public";
		char *network = "0.0.0.0";

		dbus_message_get_args(message, &error,
				DBUS_TYPE_STRING, &community,
				DBUS_TYPE_STRING, &network,
				DBUS_TYPE_INVALID);

		if (strcmp(network, "0.0.0.0") == 0)
			network = NULL;

		rc = nds_ScanForPrintersViaSNMP(ctx, udi, community, network);
	} else if (dbus_message_is_method_call(message,
				DBUS_INTERFACE, "DisablePrinterScanningViaSNMP")) {
		rc = nds_DisablePrinterScanningViaSNMP(ctx);
	} else
		HAL_WARNING(("Unknown DBus message: %s, %s ", member, path));

	if (dbus_error_is_set(&error))
		dbus_error_free(&error);

	if ((reply = dbus_message_new_method_return(message)) == NULL) {
		HAL_WARNING(("Could not allocate memory for the DBus reply"));
		return (FALSE);
	}

	dbus_message_append_args(reply, DBUS_TYPE_INT32, &rc,
			DBUS_TYPE_INVALID);

	if (!dbus_connection_send(connection, reply, NULL)) {
		HAL_WARNING(("Could not sent reply"));
	}
	dbus_connection_flush(connection);
	dbus_message_unref(reply);

	return (DBUS_HANDLER_RESULT_HANDLED);
}
Esempio n. 17
0
HalDevice *
devinfo_mass_disklabel_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
	HalDevice *d = NULL;
	struct disklabel label;
	struct partition *part;
	struct stat st;
	const char *driver;
	char *devpath, *rdevpath, *partname;
	char *childnode;
	char unit;
	struct volume_id *vid;
	uint64_t psize, msize;
	int i, fd;

	partname = devnode;
	unit = partname[strlen (partname) - 1] - 'a';

	if (unit >= MAXPARTITIONS)
		return NULL;

	devpath = g_strdup_printf ("/dev/%s", partname);
	rdevpath = g_strdup_printf ("/dev/r%s", partname);
	fd = open (rdevpath, O_RDONLY);
	if (fd < 0) {
		HAL_WARNING (("couldn't open %s: %s", rdevpath, strerror (errno)));
		g_free (rdevpath);
		return NULL;
	}

	if (ioctl (fd, DIOCGDINFO, &label) == -1) {
		HAL_WARNING (("DIOCGDINFO failed on %s: %s", rdevpath, strerror (errno)));
		g_free (rdevpath);
		close (fd);
		return NULL;
	}
	part = &label.d_partitions[unit];

	d = hal_device_new ();

	devinfo_set_default_properties (d, parent, devnode, devfs_path);

	hal_device_add_capability (d, "block");
	hal_device_property_set_string (d, "info.subsystem", "block");
	hal_device_property_set_string (d, "info.category", "volume");
	hal_device_property_set_string (d, "block.device", devpath);
	hal_device_property_set_string (d, "block.storage_device",
	    hal_device_property_get_string (parent, "info.udi"));

	if (stat (devpath, &st) == 0) {
		hal_device_property_set_int (d, "block.major", major (st.st_rdev));
		hal_device_property_set_int (d, "block.minor", minor (st.st_rdev));
	}

	hal_device_property_set_bool (d, "block.is_volume", TRUE);
	hal_device_property_set_bool (d, "block.no_partitions", FALSE);
	hal_device_property_set_bool (d, "block.have_scanned", TRUE);

	hal_device_add_capability (d, "volume");
	hal_device_property_set_bool (d, "volume.ignore", FALSE);
	hal_device_property_set_bool (d, "volume.is_mounted", FALSE);
	hal_device_property_set_bool (d, "volume.is_mounted_read_only", FALSE);
	hal_device_property_set_string (d, "volume.mount_point", "");
	hal_device_property_set_string (d, "volume.fsusage", "filesystem");
	hal_device_property_set_string (d, "volume.fstype", devinfo_mass_get_fstype (part->p_fstype));
	hal_device_property_set_bool (d, "volume.is_disc", FALSE);

	hal_device_property_set_string (d, "volume.label", "");
	hal_device_property_set_string (d, "volume.partition.label", "");
	hal_device_property_set_string (d, "volume.uuid", "");
	hal_device_property_set_string (d, "volume.partition.uuid", "");

	psize = (uint64_t)part->p_size * (uint64_t)label.d_secsize;
	msize = (uint64_t)label.d_secsize * (uint64_t)label.d_secperunit;

	hal_device_property_set_uint64 (d, "volume.size", psize);
	hal_device_property_set_int (d, "volume.block_size", label.d_secsize);
	hal_device_property_set_uint64 (d, "volume.num_blocks", part->p_size);
	hal_device_property_set_uint64 (d, "volume.partition.media_size", msize);

	hal_device_property_set_bool (d, "volume.is_partition", TRUE);
	hal_device_property_set_int (d, "volume.partition.number", unit);
	hal_device_property_set_string (d, "volume.partition.scheme", "mbr");

	vid = volume_id_open_fd (fd);
	if (vid) {
		if (volume_id_probe_all (vid, 0, psize) == 0) {
			const char *type,*fstype;

			hal_device_property_set_string (d, "volume.label", vid->label);
			hal_device_property_set_string (d, "volume.partition.label", vid->label);
			hal_device_property_set_string (d, "volume.uuid", vid->uuid);
			hal_device_property_set_string (d, "volume.partition.uuid", vid->uuid);

			if ( type && volume_id_get_type (vid, &type)) {
				fstype=devinfo_mass_get_fstype (part->p_fstype);
				if (strcmp (type, fstype)) {
					HAL_INFO (("%s disklabel reports [%s] but libvolume_id says it is "
					    "[%s], assuming disklabel is incorrect",
					    devpath, fstype, type));
					hal_device_property_set_string (d, "volume.fstype", type);
				}
			}
		}
		volume_id_close (vid);
	}

	devinfo_add_enqueue (d, devfs_path, &devinfo_mass_disklabel_handler);

	close (fd);

	g_free (rdevpath);
	g_free (devpath);

	return d;
}
Esempio n. 18
0
static gboolean
ck_tracker_init_get_sessions_for_seat (CKTracker *tracker, CKSeat *seat)
{
	gboolean ret;
	DBusError error;
	DBusMessage *message;
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter iter_array;

	ret = FALSE;

	message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit", 
						seat->seat_objpath,
						"org.freedesktop.ConsoleKit.Seat",
						"GetSessions");
	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (tracker->dbus_connection, message, -1, &error);
	if (reply == NULL || dbus_error_is_set (&error)) {
		HAL_ERROR (("Error doing GetSeats on ConsoleKit: %s: %s", error.name, error.message));
		dbus_message_unref (message);
		if (reply != NULL)
			dbus_message_unref (reply);
		goto error;
	}

	dbus_message_iter_init (reply, &iter);
	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) {
		HAL_WARNING (("Expecting an array from GetSessions on ConsoleKit."));
		dbus_message_unref (message);
		dbus_message_unref (reply);
		goto error;
	}
	dbus_message_iter_recurse (&iter, &iter_array);
	while (dbus_message_iter_get_arg_type (&iter_array) == DBUS_TYPE_OBJECT_PATH) {
		const char *session_objpath;
		CKSession *session;

		dbus_message_iter_get_basic (&iter_array, &session_objpath);
		HAL_INFO (("got session '%s' for seat '%s'", session_objpath, seat->seat_objpath));

		session = ck_session_new (session_objpath);

		/* get information: is_active etc. */
		if (!ck_session_get_info (tracker, session)) {
			HAL_ERROR (("Could not get information for session '%s'", session_objpath));
			dbus_message_unref (message);
			dbus_message_unref (reply);
			goto error;
		}

		ck_seat_attach_session (seat, session);

		tracker->sessions = g_slist_prepend (tracker->sessions, session);

		dbus_message_iter_next (&iter_array);
	}
	dbus_message_unref (message);
	dbus_message_unref (reply);

	HAL_INFO (("Got all sessions on seat '%s'", seat->seat_objpath));

	ret = TRUE;

error:
	if (dbus_error_is_set (&error))
		dbus_error_free (&error);

	return ret;
}
Esempio n. 19
0
CICallerInfo *
ci_tracker_get_info (CITracker *cit, const char *system_bus_unique_name)
{
	CICallerInfo *ci;
	DBusError error;
#ifdef HAVE_CONKIT
	DBusMessage *message;
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter sub_iter;
	char *dbus_session_name;
        char *str;
        int num_elems;
#endif /* HAVE_CONKIT */
	
	ci = NULL;
	
	if (system_bus_unique_name == NULL)
		goto error;

        if (!validate_bus_name (system_bus_unique_name))
                goto error;

	/*HAL_INFO (("========================="));
	  HAL_INFO (("Looking up CICallerInfo for system_bus_unique_name = %s", system_bus_unique_name));*/

	ci = g_hash_table_lookup (cit->connection_name_to_caller_info,
				  system_bus_unique_name);
	if (ci != NULL) {
		/*HAL_INFO (("(using cached information)"));*/
		goto got_caller_info;
	}
	/*HAL_INFO (("(retrieving info from system bus and ConsoleKit)"));*/
	
	ci = caller_info_new ();
	ci->system_bus_unique_name = g_strdup (system_bus_unique_name);

	dbus_error_init (&error);
	ci->uid = dbus_bus_get_unix_user (cit->dbus_connection, system_bus_unique_name, &error);
	if (ci->uid == ((unsigned long) -1) || dbus_error_is_set (&error)) {
		HAL_WARNING (("Could not get uid for connection: %s %s", error.name, error.message));
		dbus_error_free (&error);
		goto error;
	}

#ifdef HAVE_CONKIT
	message = dbus_message_new_method_call ("org.freedesktop.DBus", 
						"/org/freedesktop/DBus/Bus",
						"org.freedesktop.DBus",
						"GetConnectionUnixProcessID");
	dbus_message_iter_init_append (message, &iter);
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &system_bus_unique_name);
	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (cit->dbus_connection, message, -1, &error);
	if (reply == NULL || dbus_error_is_set (&error)) {
		HAL_WARNING (("Error doing GetConnectionUnixProcessID on Bus: %s: %s", error.name, error.message));
		dbus_message_unref (message);
		if (reply != NULL)
			dbus_message_unref (reply);
		dbus_error_free (&error);
		goto error;
	}
	dbus_message_iter_init (reply, &iter);
	dbus_message_iter_get_basic (&iter, &ci->pid);
	dbus_message_unref (message);
	dbus_message_unref (reply);

	message = dbus_message_new_method_call ("org.freedesktop.DBus", 
						"/org/freedesktop/DBus/Bus",
						"org.freedesktop.DBus",
						"GetConnectionSELinuxSecurityContext");
	dbus_message_iter_init_append (message, &iter);
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &system_bus_unique_name);
	reply = dbus_connection_send_with_reply_and_block (cit->dbus_connection, message, -1, &error);
        /* SELinux might not be enabled */
        if (dbus_error_is_set (&error) && 
            strcmp (error.name, "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown") == 0) {
                dbus_message_unref (message);
		if (reply != NULL)
			dbus_message_unref (reply);
                dbus_error_init (&error);
        } else if (reply == NULL || dbus_error_is_set (&error)) {
                g_warning ("Error doing GetConnectionSELinuxSecurityContext on Bus: %s: %s", error.name, error.message);
                dbus_message_unref (message);
                if (reply != NULL)
                        dbus_message_unref (reply);
		dbus_error_free (&error);
                goto error;
        } else {
                /* TODO: verify signature */
                dbus_message_iter_init (reply, &iter);
                dbus_message_iter_recurse (&iter, &sub_iter);
                dbus_message_iter_get_fixed_array (&sub_iter, (void *) &str, &num_elems);
                if (str != NULL && num_elems > 0)
                        ci->selinux_context = g_strndup (str, num_elems);
                dbus_message_unref (message);
                dbus_message_unref (reply);
        }


	message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit", 
						"/org/freedesktop/ConsoleKit/Manager",
						"org.freedesktop.ConsoleKit.Manager",
						"GetSessionForUnixProcess");
	dbus_message_iter_init_append (message, &iter);
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &ci->pid);
	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (cit->dbus_connection, message, -1, &error);
	if (reply == NULL || dbus_error_is_set (&error)) {
		HAL_WARNING (("Error doing GetSessionForUnixProcess on ConsoleKit: %s: %s", error.name, error.message));
		dbus_message_unref (message);
		if (reply != NULL)
			dbus_message_unref (reply);
		/* OK, this is not a catastrophe; just means the caller is not a member of any session.. */
		dbus_error_free (&error);
		goto store_caller_info;
	}
	dbus_message_iter_init (reply, &iter);
	dbus_message_iter_get_basic (&iter, &dbus_session_name);
	ci->session_objpath = g_strdup (dbus_session_name);
	dbus_message_unref (message);
	dbus_message_unref (reply);

	message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit", 
						ci->session_objpath,
						"org.freedesktop.ConsoleKit.Session",
						"IsActive");
	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (cit->dbus_connection, message, -1, &error);
	if (reply == NULL || dbus_error_is_set (&error)) {
		HAL_WARNING (("Error doing IsActive on ConsoleKit: %s: %s", error.name, error.message));
		dbus_message_unref (message);
		if (reply != NULL)
			dbus_message_unref (reply);
		dbus_error_free (&error);
		goto error;
	}
	dbus_message_iter_init (reply, &iter);
	dbus_message_iter_get_basic (&iter, &ci->in_active_session);
	dbus_message_unref (message);
	dbus_message_unref (reply);


	message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit", 
						ci->session_objpath,
						"org.freedesktop.ConsoleKit.Session",
						"IsLocal");
	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (cit->dbus_connection, message, -1, &error);
	if (reply == NULL || dbus_error_is_set (&error)) {
		HAL_WARNING (("Error doing IsLocal on ConsoleKit: %s: %s", error.name, error.message));
		dbus_message_unref (message);
		if (reply != NULL)
			dbus_message_unref (reply);
		dbus_error_free (&error);
		goto error;
	}
	dbus_message_iter_init (reply, &iter);
	dbus_message_iter_get_basic (&iter, &ci->is_local);
	dbus_message_unref (message);
	dbus_message_unref (reply);
	
store_caller_info:
#endif /* HAVE_CONKIT */

	g_hash_table_insert (cit->connection_name_to_caller_info,
			     ci->system_bus_unique_name,
			     ci);

got_caller_info:
	/*HAL_INFO (("system_bus_unique_name = %s", ci->system_bus_unique_name));
	HAL_INFO (("uid                    = %d", ci->uid));
	HAL_INFO (("pid                    = %d", ci->pid));
	if (ci->session_objpath != NULL) {
		HAL_INFO (("session_objpath        = %s", ci->session_objpath));
		HAL_INFO (("in_active_session      = %d", ci->in_active_session));
	} else {
		HAL_INFO (("  (Caller is not in any session)"));
	}
	HAL_INFO (("  (keeping CICallerInfo for %d connections)", 
                   g_hash_table_size (cit->connection_name_to_caller_info)));
	HAL_INFO (("========================="));*/
	return ci;

error:
	/*HAL_INFO (("========================="));*/
	if (ci != NULL)
		caller_info_free (ci);
	return NULL;
}
Esempio n. 20
0
HalDevice *
devinfo_audio_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
	HalDevice *d = NULL, *mixer, *dsp;
	prop_dictionary_t dict;
	const char *driver, *parent_udi;
	char *childnode;
	int16_t unit;
	char *audioctl;
	struct audio_device audiodev;
	int fd;

	if (drvctl_find_device (devnode, &dict) == FALSE || dict == NULL)
		return NULL;

	if (prop_dictionary_get_int16 (dict, "device-unit", &unit) == false ||
	    prop_dictionary_get_cstring_nocopy (dict, "device-driver", &driver) == false) {
		prop_object_release (dict);
		return NULL;
	}

	if (strcmp (driver, "audio") != 0) {
		prop_object_release (dict);
		return NULL;
	}

	audioctl = g_strdup_printf (_PATH_AUDIOCTL "%d", unit);
	fd = open (audioctl, O_RDONLY);
	if (fd < 0) {
		HAL_WARNING (("couldn't open %s: %s", audioctl, strerror(errno)));
		goto done;
	}

	if (ioctl (fd, AUDIO_GETDEV, &audiodev) == -1) {
		HAL_WARNING (("couldn't query %s: %s", audioctl, strerror(errno)));
		goto done;
	}

	d = hal_device_new ();

	devinfo_set_default_properties (d, parent, devnode, devfs_path);
	hal_device_add_capability (d, "sound");
	hal_device_property_set_string (d, "info.category", "sound");
	hal_device_property_set_string (d, "info.subsystem", "sound");

	hal_device_property_set_int (d, "sound.card", unit);
	hal_device_property_set_string (d, "sound.card_id", audiodev.name);
	hal_device_property_set_string (d, "netbsd.sound.hardware", audiodev.config);

	parent_udi = hal_device_property_get_string (parent, "info.udi");
	if (parent_udi)
		hal_device_property_set_string (d, "sound.originating_device", parent_udi);

	devinfo_add_enqueue (d, devfs_path, &devinfo_audio_handler);

	childnode = g_strdup_printf ("oss_mixer_%d", unit);
	devinfo_add_node (d, childnode);
	g_free (childnode);

	childnode = g_strdup_printf ("oss_dsp_%d", unit);
	devinfo_add_node (d, childnode);
	g_free (childnode);

done:
	if (dict)
		prop_object_release (dict);
	if (audioctl)
		g_free (audioctl);
	if (fd >= 0)
		close (fd);

	return d;
}
Esempio n. 21
0
HalDevice *
devinfo_mass_add(HalDevice *parent, const char *devnode, char *devfs_path, char *device_type)
{
	HalDevice *d = NULL, *gparent = NULL;
	prop_dictionary_t dict;
	struct disklabel label;
	struct stat st;
	const char *driver;
	char *rdevpath, *devpath;
	char *childnode;
	char *parent_devnode, *gparent_devnode = NULL;
	char *gparent_udi;
	int16_t unit;
	int i, fd;
	struct scsipi_inquiry_data inqbuf;
	struct scsipi_inquiry cmd;
	bool scsiinq_status;
	char *storage_model = NULL, *storage_vendor = NULL;

	if (drvctl_find_device (devnode, &dict) == FALSE || dict == NULL)
		return NULL;

	if (prop_dictionary_get_int16 (dict, "device-unit", &unit) == false ||
	    prop_dictionary_get_cstring_nocopy (dict, "device-driver", &driver) == false) {
		prop_object_release (dict);
		return NULL;
	}

	if (strcmp (driver, "wd") != 0 && strcmp (driver, "sd") != 0 &&
	    strcmp (driver, "ld") != 0) {
		prop_object_release (dict);
		return NULL;
	}

	sleep (1);

	devpath = g_strdup_printf ("/dev/%s%c", devnode, RAW_PART + 'a');
	rdevpath = g_strdup_printf ("/dev/r%s%c", devnode, RAW_PART + 'a');
	HAL_INFO (("  going to open %s", rdevpath));
	fd = open (rdevpath, O_RDONLY);
	if (fd < 0) {
		HAL_WARNING (("couldn't open %s: %s", rdevpath, strerror (errno)));
		g_free (rdevpath);
		g_free (devpath);
		return NULL;
	}

	HAL_INFO (("  going to DIOCGDINFO %s", rdevpath));
	if (ioctl (fd, DIOCGDINFO, &label) == -1) {
		HAL_WARNING (("DIOCGDINFO failed on %s: %s", rdevpath, strerror (errno)));
		g_free (rdevpath);
		g_free (devpath);
		close (fd);
		return NULL;
	}

	if (strcmp (driver, "sd") == 0) {
		memset(&cmd, 0, sizeof (cmd));
		memset(&inqbuf, 0, sizeof (inqbuf));
		cmd.opcode = INQUIRY;
		cmd.length = sizeof (inqbuf);

		scsiinq_status = scsi_command (fd, &cmd, sizeof (cmd), &inqbuf, sizeof (inqbuf), 10000, SCCMD_READ);
	} else
		scsiinq_status = false;

	close (fd);

	d = hal_device_new ();

	devinfo_set_default_properties (d, parent, devnode, devfs_path);

	hal_device_add_capability (d, "block");
	hal_device_property_set_string (d, "info.subsystem", "block");
	hal_device_property_set_string (d, "block.device", devpath);
	if (stat (devpath, &st) == 0) {
		hal_device_property_set_int (d, "block.major", major (st.st_rdev));
		hal_device_property_set_int (d, "block.minor", minor (st.st_rdev));
	}
	hal_device_property_set_bool (d, "block.is_volume", FALSE);
	hal_device_property_set_bool (d, "block.no_partitions", FALSE);
	hal_device_property_set_bool (d, "block.have_scanned", TRUE);

	hal_device_add_capability (d, "storage");
	hal_device_property_set_string (d, "info.category", "storage");
	parent_devnode = hal_device_property_get_string (parent, "netbsd.device");
	gparent_udi = hal_device_property_get_string (parent, "info.parent");
	if (gparent_udi) {
		gparent = hal_device_store_find (hald_get_gdl (), gparent_udi);
		if (gparent)
			gparent_devnode = hal_device_property_get_string (gparent, "netbsd.device");
	}
	if (gparent_devnode && strstr (gparent_devnode, "umass") == gparent_devnode)
		hal_device_property_set_string (d, "storage.bus", "usb");
	else if (parent_devnode && strstr (parent_devnode, "atabus") == parent_devnode)
		hal_device_property_set_string (d, "storage.bus", "ide");
	else
		hal_device_property_set_string (d, "storage.bus", "scsi");
	hal_device_property_set_string (d, "storage.device_type", "disk");
	hal_device_property_set_bool (d, "storage.removable", label.d_flags & D_REMOVABLE ? TRUE : FALSE);
	if (label.d_flags & D_REMOVABLE) {
		hal_device_property_set_bool (d, "storage.removable.media_available", TRUE);
		hal_device_property_set_uint64 (d, "storage.removable.media_size",
		    (uint64_t)label.d_secsize * (uint64_t)label.d_secperunit);
		hal_device_property_set_uint64 (d, "storage.size", 0);
		hal_device_property_set_bool (d, "storage.hotpluggable", TRUE);
		hal_device_property_set_bool (d, "storage.automount_enabled_hint", TRUE);
	} else {
		hal_device_property_set_bool (d, "storage.removable.media_available", FALSE);
		hal_device_property_set_uint64 (d, "storage.removable.media_size", 0);
		hal_device_property_set_uint64 (d, "storage.size",
		    (uint64_t)label.d_secsize * (uint64_t)label.d_secperunit);
		hal_device_property_set_bool (d, "storage.hotpluggable", FALSE);
		hal_device_property_set_bool (d, "storage.automount_enabled_hint", FALSE);
	}
	hal_device_property_set_bool (d, "storage.no_partitions_hint", FALSE);
	hal_device_property_set_bool (d, "storage.requires_eject", FALSE);
	hal_device_property_set_bool (d, "storage.removable.support_async_notification", FALSE);
	hal_device_property_set_string (d, "storage.partitioning_scheme", "mbr");
	hal_device_property_set_string (d, "storage.originating_device",
	    hal_device_property_get_string (d, "info.udi"));

	if (scsiinq_status == true) {
		storage_model = rtrim_copy(inqbuf.product, sizeof (inqbuf.product));
		storage_vendor = rtrim_copy(inqbuf.vendor, sizeof (inqbuf.vendor));
	}

	if (storage_model) {
		hal_device_property_set_string (d, "storage.model", storage_model);
		free (storage_model);
	} else
		hal_device_property_set_string (d, "storage.model", label.d_packname);

	if (storage_vendor) {
		hal_device_property_set_string (d, "storage.vendor", storage_vendor);
		free (storage_vendor);
	} else
		hal_device_property_set_string (d, "storage.vendor", label.d_typename);

	devinfo_add_enqueue (d, devfs_path, &devinfo_mass_handler);

	for (i = 0; i < MAXPARTITIONS; i++) {
		const char *fstype;

		fstype = devinfo_mass_get_fstype(label.d_partitions[i].p_fstype);
		if (fstype == NULL)
			continue;

		childnode = g_strdup_printf ("%s%c", devnode, 'a' + i);
		HAL_INFO (("  adding %s on %s", childnode, rdevpath));
		devinfo_mass_disklabel_add (d, childnode, childnode, childnode);
		g_free (childnode);
	}

	HAL_INFO (("  returning"));
	g_free (rdevpath);
	g_free (devpath);

done:
	prop_object_release (dict);

	return d;
}
Esempio n. 22
0
static gboolean
ck_tracker_init_get_seats_and_sessions (CKTracker *tracker)
{
	gboolean ret;
	DBusError error;
	DBusMessage *message;
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter iter_array;

	ret = FALSE;

	/* first build array of existing seats and sessions */

	message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit", 
						"/org/freedesktop/ConsoleKit/Manager",
						"org.freedesktop.ConsoleKit.Manager",
						"GetSeats");
	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (tracker->dbus_connection, message, -1, &error);
	if (reply == NULL || dbus_error_is_set (&error)) {
		HAL_ERROR (("Error doing GetSeats on ConsoleKit: %s: %s", error.name, error.message));
		dbus_message_unref (message);
		if (reply != NULL)
			dbus_message_unref (reply);
		goto error;
	}

	dbus_message_iter_init (reply, &iter);
	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) {
		HAL_WARNING (("Expecting an array from GetSeats on ConsoleKit."));
		dbus_message_unref (message);
		dbus_message_unref (reply);
		goto error;
	}
	dbus_message_iter_recurse (&iter, &iter_array);
	while (dbus_message_iter_get_arg_type (&iter_array) == DBUS_TYPE_OBJECT_PATH) {
		const char *seat_objpath;
		CKSeat *seat;

		dbus_message_iter_get_basic (&iter_array, &seat_objpath);
		HAL_INFO (("got seat '%s'", seat_objpath));

		seat = ck_seat_new (seat_objpath);

		/* get information */
		if (!ck_seat_get_info (tracker, seat)) {
			HAL_ERROR (("Could not get information for seat '%s'", seat_objpath));
			dbus_message_unref (message);
			dbus_message_unref (reply);
			goto error;
		}

		/* for each seat, get the sessions */
		if (!ck_tracker_init_get_sessions_for_seat (tracker, seat)) {
			HAL_ERROR (("Could not get sessions for seat '%s'", seat_objpath));
			dbus_message_unref (message);
			dbus_message_unref (reply);
			goto error;
		}

		tracker->seats = g_slist_prepend (tracker->seats, seat);

		dbus_message_iter_next (&iter_array);
	}
	dbus_message_unref (message);
	dbus_message_unref (reply);

	HAL_INFO (("Got seats"));

	ret = TRUE;
error:
	if (dbus_error_is_set (&error))
		dbus_error_free (&error);

	return ret;
}
Esempio n. 23
0
int
add_network_printer(LibHalContext *ctx, char *base, char *hostaddr,
		char *device, char *community)
{
	DBusError error;
	int rc = -1;
	char udi[128];
	char *tmp_udi = NULL;
	static char *parent = NULL;
	char *manufacturer = NULL, *model = NULL, *description = NULL,
	     *uri = NULL, *sn, *serial;

	sn = serial = pseudo_serialno_from_addr(hostaddr);

	if (parent == NULL)
		parent = getenv("UDI");

	dbus_error_init(&error);

	network_device_name_to_udi(udi, sizeof (udi), base, serial, NULL);

	if (libhal_device_exists(ctx, udi, &error) == TRUE)
		goto out;

	if ((tmp_udi = libhal_new_device(ctx, &error)) == NULL)
		goto out;

	snmp_printer_info(hostaddr, community, &manufacturer, &model,
			&description, &serial, NULL, &uri);

	libhal_device_set_property_string(ctx, tmp_udi,
			"info.parent", parent, &error);

	libhal_device_set_property_string(ctx, tmp_udi,
			"info.category", "printer", &error);

	libhal_device_property_strlist_append(ctx, tmp_udi,
				"info.capabilities", "printer", &error);
	libhal_device_property_strlist_append(ctx, tmp_udi,
				"info.capabilities", "network_device", &error);

	libhal_device_set_property_string(ctx, tmp_udi,
			"network_device.address", hostaddr, &error);

	if ((community != NULL) && (strcasecmp(community, "public") != 0))
		libhal_device_set_property_string(ctx, tmp_udi,
			"network_device.snmp_community", community, &error);

	if ((uri != NULL) || (device != NULL))
		libhal_device_set_property_string(ctx, tmp_udi,
			"printer.device", (uri ? uri : device), &error);

	if (serial != NULL)
		libhal_device_set_property_string(ctx, tmp_udi,
			"printer.serial", serial, &error);

	if (manufacturer != NULL)
		libhal_device_set_property_string(ctx, tmp_udi,
			"printer.vendor", manufacturer, &error);

	if (model != NULL)
		libhal_device_set_property_string(ctx, tmp_udi,
			"printer.product", model, &error);

	if (description != NULL)
		libhal_device_set_property_string(ctx, tmp_udi,
			"printer.description", description, &error);

	/* commit the changes to the new UDI */
	rc = libhal_device_commit_to_gdl(ctx, tmp_udi, udi, &error);

out:
	HAL_DEBUG(("result: %s (%s): %s, %s, %s, %s, %s", hostaddr, udi,
		NP(manufacturer), NP(model), NP(description), NP(serial),
		NP(uri)));

	if (tmp_udi != NULL)
		free(tmp_udi);
	if (manufacturer != NULL)
		free(manufacturer);
	if (model != NULL)
		free(model);
	if (description != NULL)
		free(description);
	if (uri != NULL)
		free(uri);
	if (sn != NULL)
		free(sn);

	if (dbus_error_is_set(&error)) {
		HAL_WARNING(("%s: %s", error.name, error.message));
		dbus_error_free(&error);
	}

	HAL_DEBUG(("add: %s (%s)", hostaddr, udi));

	return (rc);
}
Esempio n. 24
0
/** 
 * calc_cpu_load:
 * @consider_nice:
 * 
 * Returns:
 * 
 * calculates current cpu load and stores it in cpuload_data object 
 */
static int calc_cpu_load(const int consider_nice)
{
	unsigned long	total_elapsed, working_elapsed;
	char		what[32];
	unsigned long	user_time, nice_time, system_time, idle_time;
	unsigned long	total_time, iowait_time;
	unsigned	scan_ret;
	char		line[256];
	char		cpu_string[7];
	FILE		*fp;
	int		new_num_cpus, i;

	new_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
	if (new_num_cpus == -1 || new_num_cpus != cpuload.num_cpus) {
		free_cpu_load_data();
		cpuload.num_cpus = new_num_cpus;
		if (cpuload.num_cpus <= 0) {
			errno = ENODEV;
			return -20;
		}

		cpuload.last_total_time = (unsigned long *)calloc(cpuload.num_cpus + 1,
								  sizeof(unsigned long));
		cpuload.last_working_time = (unsigned long *)calloc(cpuload.num_cpus + 1,
								    sizeof(unsigned long));
		cpuload.load = (int *)calloc(cpuload.num_cpus + 1, sizeof(int));
	}

	if ((fp = fopen(PROC_STAT_FILE, "r")) == NULL) {
		HAL_DEBUG(("Could not open %s: %s", PROC_STAT_FILE, strerror(errno)));
		return -1;
	}

	/* start with the first line, "overall" cpu load */
	/* if cpuload.num_cpus == 1, we do not need to evaluate "overall" and "per-cpu" load */
	sprintf(cpu_string, "cpu ");

	for (i = 0; i <= cpuload.num_cpus - (cpuload.num_cpus == 1); i++) {
		unsigned long working_time;
		
		if (fgets(line,255,fp) == NULL) {
			HAL_WARNING(("%s too short (%s)", PROC_STAT_FILE, cpu_string));
			fclose(fp);
			return -1;
		}
		if (memcmp(line, cpu_string, strlen(cpu_string))) {
			HAL_WARNING(("no '%s' string in %s line %d",
				     cpu_string, PROC_STAT_FILE, i));
			fclose(fp);
			return -1;
		}
		/* initialized, since it is simply not there in 2.4 */
		iowait_time = 0;
		scan_ret = sscanf(line, "%s %lu %lu %lu %lu %lu", what, &user_time, &nice_time,
			  &system_time, &idle_time, &iowait_time);
		if (scan_ret < 5) {
			HAL_WARNING(("only %d values in %s. Please report.",
				     scan_ret, PROC_STAT_FILE));
			fclose(fp);
			return -1;
		}

		if (consider_nice) {
			working_time = user_time + system_time + nice_time;
			idle_time += iowait_time;
		} else {
			working_time = user_time + system_time;
			idle_time += (nice_time + iowait_time);
		}
		total_time = working_time + idle_time;
		total_elapsed = total_time - cpuload.last_total_time[i];
		working_elapsed = working_time - cpuload.last_working_time[i];
		cpuload.last_working_time[i] = working_time;
		cpuload.last_total_time[i] = total_time;
		
		if (!total_elapsed) {
			/* not once per CPU, only once per check. */
			if (!i)
				HAL_DEBUG(("%s not updated yet, poll slower.", PROC_STAT_FILE));
		} else
			cpuload.load[i] = working_elapsed * 100 / total_elapsed;

		sprintf(cpu_string, "cpu%d ", i);
	}
	/* shortcut for UP systems */
	if (cpuload.num_cpus == 1)
		cpuload.load[1] = cpuload.load[0];

	fclose(fp);
	
	return 0;
}
Esempio n. 25
0
static void
add_device (LibHalContext *ctx,
	    const char *udi,
	    const LibHalPropertySet *properties)
{
	DBusError err;
	DBusConnection *dbus_connection;
	const char* sysfs_path;
	const char* function;
	static gboolean initialized = FALSE;

	if ((sysfs_path = libhal_ps_get_string (properties, "linux.sysfs_path")) == NULL) {
		HAL_ERROR(("%s has no property linux.sysfs_path", udi));
		return;
	}
	if ((function = libhal_ps_get_string (properties, "leds.function")) == NULL) {
		HAL_ERROR(("%s has no property leds.function", udi));
		return;
	}

	/* claim the interface */
	
        if ((dbus_connection = libhal_ctx_get_dbus_connection(ctx)) == NULL) {
                HAL_WARNING (("Cannot get DBus connection"));
                return;
        }

	if (!initialized) {
		dbus_connection_add_filter (dbus_connection, filter_function, NULL, NULL);
		initialized = TRUE;
	}

	dbus_error_init (&err);

	if (function != NULL && strcmp(function, "kbd_backlight") == 0) {
		HAL_DEBUG (("Found a led which is a keyboard backlight."));
		
		if (!libhal_device_claim_interface (ctx,
						    udi,
						    "org.freedesktop.Hal.Device.KeyboardBacklight",
						    "    <method name=\"SetBrightness\">\n"
						    "      <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
						    "      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
						    "    </method>\n"
						    "    <method name=\"GetBrightness\">\n"
						    "      <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
						    "    </method>\n",
						    &err)) {
			HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.KeyboardBacklight'"));
			LIBHAL_FREE_DBUS_ERROR (&err);
			return;
		}
	} else if (!libhal_device_claim_interface (ctx,
					    udi,
					    "org.freedesktop.Hal.Device.Leds",
					    "    <method name=\"SetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
					    "      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n"
					    "    <method name=\"GetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n",
					    &err)) {
		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.Leds'"));
		LIBHAL_FREE_DBUS_ERROR (&err);
		return;
	}
	
	g_hash_table_insert (leds, g_strdup(udi), g_strdup(sysfs_path));
}