static int get_uint64_prop(LibHalContext *ctxt, const char *udi,
                           const char *prop, unsigned long long *val_p)
{
    DBusError err;
    unsigned long long val;
    int rv;

    dbus_error_init(&err);
    val = libhal_device_get_property_uint64(ctxt, udi, prop, &err);
    rv = dbus_error_is_set(&err);
    dbus_error_free(&err);
    if (rv == 0)
        *val_p = val;

    return rv;
}
Exemple #2
0
int 
main (int argc, char *argv[])
{
	int fd, rfd;
	int ret;
	char *udi;
	char *device_file, *raw_device_file;
	char *devpath, *rdevpath;
	boolean_t is_dos;
	int dos_num;
	LibHalContext *ctx = NULL;
	DBusError error;
	DBusConnection *conn;
	char *parent_udi;
	char *storage_device;
	char *is_disc_str;
	int fdc;
	dbus_bool_t is_disc = FALSE;
	dbus_bool_t is_floppy = FALSE;
	unsigned int block_size;
	dbus_uint64_t vol_size;
	dbus_bool_t has_data = TRUE;	/* probe for fs by default */
	dbus_bool_t has_audio = FALSE;
	char *partition_scheme = NULL;
	dbus_uint64_t partition_start = 0;
	int partition_number = 0;
	struct vtoc vtoc;
	dk_gpt_t *gpt;
	struct dk_minfo mi;
	int i, dos_cnt;
	fstyp_handle_t fstyp_handle;
	int systid, relsect, numsect;
	off_t probe_offset = 0;
	int num_volumes;
	char **volumes;
	dbus_uint64_t v_start;
	const char *fstype;
	nvlist_t *fsattr;

	fd = rfd = -1;

	ret = 1;

	if ((udi = getenv ("UDI")) == NULL) {
		goto out;
	}
	if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL) {
		goto out;
	}
	if ((raw_device_file = getenv ("HAL_PROP_BLOCK_SOLARIS_RAW_DEVICE")) == NULL) {
		goto out;
	}
	if (!dos_to_dev(device_file, &rdevpath, &dos_num)) {
		rdevpath = raw_device_file;
	}
	if (!(is_dos = dos_to_dev(device_file, &devpath, &dos_num))) {
		devpath = device_file;
	}
	if ((parent_udi = getenv ("HAL_PROP_INFO_PARENT")) == NULL) {
		goto out;
	}
	if ((storage_device = getenv ("HAL_PROP_BLOCK_STORAGE_DEVICE")) == NULL) {
		goto out;
	}

	is_disc_str = getenv ("HAL_PROP_VOLUME_IS_DISC");
	if (is_disc_str != NULL && strcmp (is_disc_str, "true") == 0) {
		is_disc = TRUE;
	} else {
		is_disc = FALSE;
	}

	drop_privileges ();

	setup_logger ();

	dbus_error_init (&error);
	if ((ctx = libhal_ctx_init_direct (&error)) == NULL)
		goto out;

	HAL_DEBUG (("Doing probe-volume for %s\n", device_file));

	fd = open (devpath, O_RDONLY | O_NONBLOCK);
	if (fd < 0) {
		goto out;
	}
	rfd = open (rdevpath, O_RDONLY | O_NONBLOCK);
	if (rfd < 0) {
		goto out;
	}

	/* if it's a floppy with no media, bail out */
	if (ioctl(rfd, FDGETCHANGE, &fdc) == 0) {
		is_floppy = TRUE;
		if (fdc & FDGC_CURRENT) {
			goto out;
		}
	}

	/* block size and total size */
	if (ioctl(rfd, DKIOCGMEDIAINFO, &mi) != -1) {
		block_size = mi.dki_lbsize;
		vol_size = mi.dki_capacity * block_size;
	} else if (errno == ENXIO) {
		/* driver supports ioctl, but media is not available */
		goto out;
	} else {
		/* driver does not support ioctl, e.g. lofi */
		block_size = 512;
		vol_size = 0;
	}
	libhal_device_set_property_int (ctx, udi, "volume.block_size", block_size, &error);
	my_dbus_error_free (&error);
	libhal_device_set_property_uint64 (ctx, udi, "volume.size", vol_size, &error);
	my_dbus_error_free (&error);

	if (is_disc) {
		if (!probe_disc (rfd, ctx, udi, &has_data, &has_audio)) {
			HAL_DEBUG (("probe_disc failed, skipping fstyp"));
			goto out;
		}
		/* with audio present, create volume even if fs probing fails */
		if (has_audio) {
			ret = 0;
		}
	}

	if (!has_data) {
		goto skip_fs;
	}

	/* don't support partitioned floppy */
	if (is_floppy) {
		goto skip_part;
	}

	/*
	 * first get partitioning info
	 */
	if (is_dos) {
		/* for a dos drive find partition offset */
		if (!find_dos_drive(fd, dos_num, &relsect, &numsect, &systid)) {
			goto out;
		}
		partition_scheme = "mbr";
		partition_start = (dbus_uint64_t)relsect * 512;
		partition_number = dos_num;
		probe_offset = (off_t)relsect * 512;
	} else {
		if ((partition_number = read_vtoc(rfd, &vtoc)) >= 0) {
			if (!vtoc_one_slice_entire_disk(&vtoc)) {
				partition_scheme = "smi";
				if (partition_number < vtoc.v_nparts) {
					if (vtoc.v_part[partition_number].p_size == 0) {
						HAL_DEBUG (("zero size partition"));
					}
					partition_start = vtoc.v_part[partition_number].p_start * block_size;
				}
			}
		} else if ((partition_number = efi_alloc_and_read(rfd, &gpt)) >= 0) {
			partition_scheme = "gpt";
			if (partition_number < gpt->efi_nparts) {
				if (gpt->efi_parts[partition_number].p_size == 0) {
					HAL_DEBUG (("zero size partition"));
				}
				partition_start = gpt->efi_parts[partition_number].p_start * block_size;
			}
			efi_free(gpt);
		}
		probe_offset = 0;
	}

	if (partition_scheme != NULL) {
		libhal_device_set_property_string (ctx, udi, "volume.partition.scheme", partition_scheme, &error);
		my_dbus_error_free (&error);
		libhal_device_set_property_int (ctx, udi, "volume.partition.number", partition_number, &error);
		my_dbus_error_free (&error);
		libhal_device_set_property_uint64 (ctx, udi, "volume.partition.start", partition_start, &error);
		my_dbus_error_free (&error);
		libhal_device_set_property_bool (ctx, udi, "volume.is_partition", TRUE, &error);
		my_dbus_error_free (&error);
	} else {
		libhal_device_set_property_bool (ctx, udi, "volume.is_partition", FALSE, &error);
		my_dbus_error_free (&error);
	}

	/*
	 * ignore duplicate partitions
	 */
	if ((volumes = libhal_manager_find_device_string_match (
	    ctx, "block.storage_device", storage_device, &num_volumes, &error)) != NULL) {
		my_dbus_error_free (&error);
		for (i = 0; i < num_volumes; i++) {
			if (strcmp (udi, volumes[i]) == 0) {
				continue; /* skip self */
			}
			v_start = libhal_device_get_property_uint64 (ctx, volumes[i], "volume.partition.start", &error);
			if (dbus_error_is_set(&error)) {
				dbus_error_free(&error);
				continue;
			}
			if (v_start == partition_start) {
				HAL_DEBUG (("duplicate partition"));
				goto out;
			}
		}
		libhal_free_string_array (volumes);
	}

skip_part:

	/*
	 * now determine fs type
	 *
	 * XXX We could get better performance from block device,
	 * but for now we use raw device because:
	 *
	 * - fstyp_udfs has a bug that it only works on raw
	 *
	 * - sd has a bug that causes extremely slow reads
	 *   and incorrect probing of hybrid audio/data media
	 */
	if (fstyp_init(rfd, probe_offset, NULL, &fstyp_handle) != 0) {
		HAL_DEBUG (("fstyp_init failed"));
		goto out;
	}
	if ((fstyp_ident(fstyp_handle, NULL, &fstype) != 0) ||
	    (fstyp_get_attr(fstyp_handle, &fsattr) != 0)) {
		HAL_DEBUG (("fstyp ident or get_attr failed"));
		fstyp_fini(fstyp_handle);
		goto out;
	}
	set_fstyp_properties (ctx, udi, fstype, fsattr);

	if (strcmp (fstype, "hsfs") == 0) {
		hsfs_contents (fd, probe_offset, ctx, udi);
	}

	fstyp_fini(fstyp_handle);

skip_fs:

	ret = 0;

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

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

	return ret;

}
Exemple #3
0
/** Entry point
 *
 *  @param  argc                Number of arguments given to program
 *  @param  argv                Arguments given to program
 *  @return                     Return code
 */
int
main (int argc, char *argv[])
{
	char *udi = NULL;
	char *key = NULL;
	int type;
	dbus_bool_t is_hex = FALSE;
	dbus_bool_t is_verbose = FALSE;
	dbus_bool_t is_version = FALSE;
	char *str;
	DBusError error;

	if (argc <= 1) {
		usage (argc, argv);
		return 1;
	}

	while (1) {
		int c;
		int option_index = 0;
		const char *opt;
		static struct option long_options[] = {
			{"udi", 1, NULL, 0},
			{"key", 1, NULL, 0},
			{"hex", 0, NULL, 0},
			{"verbose", 0, NULL, 0},
			{"version", 0, NULL, 0},
			{"help", 0, NULL, 0},
			{NULL, 0, NULL, 0}
		};

		c = getopt_long (argc, argv, "",
				 long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 0:
			opt = long_options[option_index].name;

			if (strcmp (opt, "help") == 0) {
				usage (argc, argv);
				return 0;
			} else if (strcmp (opt, "hex") == 0) {
				is_hex = TRUE;
			} else if (strcmp (opt, "verbose") == 0) {
				is_verbose = TRUE;
			} else if (strcmp (opt, "version") == 0) {
				is_version = TRUE;
			} else if (strcmp (opt, "key") == 0) {
				key = strdup (optarg);
			} else if (strcmp (opt, "udi") == 0) {
				udi = strdup (optarg);
			}
			break;

		default:
			usage (argc, argv);
			return 1;
			break;
		}
	}

	if (is_version) {
		printf ("hal-get-property " PACKAGE_VERSION "\n");
		return 0;
	}

	if (udi == NULL || key == NULL) {
		usage (argc, argv);
		return 1;
	}

	dbus_error_init (&error);	
	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
		fprintf (stderr, "error: libhal_ctx_new\n");
		return 1;
	}
	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
		LIBHAL_FREE_DBUS_ERROR (&error);
		return 1;
	}
	if (!libhal_ctx_init (hal_ctx, &error)) {
		if (dbus_error_is_set(&error)) {
			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
			LIBHAL_FREE_DBUS_ERROR (&error);
		}
		fprintf (stderr, "Could not initialise connection to hald.\n"
				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
		return 1;
	}

	type = libhal_device_get_property_type (hal_ctx, udi, key, &error);
	if (type == LIBHAL_PROPERTY_TYPE_INVALID) {
		fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message);
		LIBHAL_FREE_DBUS_ERROR (&error);
		return 1;
	}
	/* emit the value to stdout */
	switch (type) {
	case LIBHAL_PROPERTY_TYPE_STRING:
		str = libhal_device_get_property_string (hal_ctx, udi, key, &error);
		if (is_verbose)
			printf ("Type is string\n");
		printf ("%s\n", str);
		libhal_free_string (str);
		break;
	case LIBHAL_PROPERTY_TYPE_INT32:
		if (is_verbose)
			printf ("Type is integer (shown in %s)\n",
				(is_hex ? "hexadecimal" : "decimal"));
		printf ((is_hex ? "%x\n" : "%d\n"),
			libhal_device_get_property_int (hal_ctx, udi, key, &error));
		break;
	case LIBHAL_PROPERTY_TYPE_UINT64:
		if (is_verbose)
			printf ("Type is uint64 (shown in %s)\n",
				(is_hex ? "hexadecimal" : "decimal"));
		printf ((is_hex ? "%llx\n" : "%llu\n"),
			(long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error));
		break;
	case LIBHAL_PROPERTY_TYPE_DOUBLE:
		if (is_verbose)
			printf ("Type is double\n");
		printf ("%f\n",
			libhal_device_get_property_double (hal_ctx, udi, key, &error));
		break;
	case LIBHAL_PROPERTY_TYPE_BOOLEAN:
		if (is_verbose)
			printf ("Type is boolean\n");
		printf ("%s\n",
			libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false");
		break;

	case LIBHAL_PROPERTY_TYPE_STRLIST:
	{
		unsigned int i;
		char **strlist;
		
		if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) {
			
			for (i = 0; strlist[i] != 0; i++) {
				printf ("%s", strlist[i]);
				if (strlist[i+1] != NULL)
					printf (" ");
			}
		} 
		break;
	}
	default:
		printf ("Unknown type %d='%c'\n", type, type);
		return 1;
		break;
	}

	if (dbus_error_is_set (&error)) {
		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
		dbus_error_free (&error);
		return 1;
	}


	return 0;
}
Exemple #4
0
gboolean
check_libhal (const char *server_addr)
{
	pid_t child_pid;

	child_pid = fork ();
	if (child_pid == -1) {
		printf ("Cannot fork\n");
		exit (1);
	} else if (child_pid == 0) {
		DBusError error;
		DBusConnection *conn;
		LibHalContext *ctx;
		dbus_bool_t passed;

		printf ("server address='%s'\n", server_addr);

		dbus_error_init (&error);
		if ((conn = dbus_connection_open (server_addr, &error)) == NULL) {
			printf ("Error connecting to server: %s\n", error.message);
			goto fail;
		}

		dbus_connection_setup_with_g_main (conn, NULL);

		if ((ctx = libhal_ctx_new ()) == NULL) {
			printf ("Error getting libhal context\n");
			goto fail;
		}

		
		libhal_ctx_set_dbus_connection (ctx, conn);

		libhal_ctx_init (ctx, &error);

		if (dbus_error_is_set (&error)) {
			printf ("FAILED98: %s\n", error.message);			
			goto fail;
		}
		printf ("SUCCESS98\n");

		libhal_device_print (ctx, "/org/freedesktop/Hal/devices/testobj1", &error);


		if (dbus_error_is_set (&error)) {
			printf ("FAILED99: %s\n", error.message);			
			goto fail;
		}
		printf ("SUCCESS99\n");

		passed = FALSE;

		{
			char *val;
			
			val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string", &error);

			if (val == NULL || strcmp (val, "fooooobar22") != 0 || dbus_error_is_set (&error)) {
				libhal_free_string (val);
				printf ("FAILED100\n");			
				goto fail;
			}
			printf ("SUCCESS100\n");
			libhal_free_string (val);
		}

		{
			char *val;
			val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string2", &error);
			if (val == NULL || strcmp (val, "fooøةמ") != 0 || dbus_error_is_set (&error)) {
				libhal_free_string (val);
				printf ("FAILED101: %s\n", error.message);			
				goto fail;
			}
			libhal_free_string (val);
			printf ("SUCCESS101\n");
		}

		{
			dbus_bool_t b;
			b = libhal_device_get_property_bool (
			    ctx, "/org/freedesktop/Hal/devices/testobj1", 
			    "test.bool", &error);
			    
			if (!b || dbus_error_is_set (&error)) {
				printf ("FAILED102: %s, %i\n", error.message, b);			
				goto fail;
			}
			printf ("SUCCESS102\n");
		}

		{
			double val;
			double expected_val = 0.53434343;

			val = libhal_device_get_property_double (ctx, "/org/freedesktop/Hal/devices/testobj1", 
								 "test.double", &error);
			if ( memcmp (&val, &expected_val, sizeof (double)) != 0 || dbus_error_is_set (&error)) {
				printf ("FAILED103\n");
				goto fail;
			}
			printf ("SUCCESS103\n");
		}

		if (libhal_device_get_property_uint64 (
			    ctx, "/org/freedesktop/Hal/devices/testobj1", "test.uint64", &error) != 
		    ((((dbus_uint64_t)1)<<35) + 5) ||
		    dbus_error_is_set (&error)) {
			printf ("FAILED104: %s\n", error.message);			
			goto fail;
		}
		printf ("SUCCESS104\n");
		
		{
			char **val;
			val = libhal_device_get_property_strlist (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.strlist", &error);
			if (val == NULL ||  dbus_error_is_set (&error)) {
				libhal_free_string_array (val);
				printf ("FAILED105: %s\n", error.message);			
				goto fail;
			}
			printf ("SUCCESS105\n");
			
			if (libhal_string_array_length (val) != 2) {
				libhal_free_string_array (val);
				printf ("FAILED106\n");			
				goto fail;
			}
			printf ("SUCCESS106\n");

			if (strcmp (val[0], "foostrlist2") != 0 ||
			    strcmp (val[1], "foostrlist3") != 0) {
				libhal_free_string_array (val);
				printf ("FAILED107\n");			
				goto fail;
			}
			printf ("SUCCESS107\n");

			libhal_free_string_array (val);
		}

		if (libhal_device_get_property_int (
			    ctx, "/org/freedesktop/Hal/devices/testobj1", "test.int", &error) != 42 ||
		    dbus_error_is_set (&error)) {
			printf ("FAILED108\n");			
			goto fail;
		}
		printf ("SUCCESS108\n");
	
		/* tests for libhal_psi */
		{
			int type;
			char *key;
			LibHalPropertySet *pset;
			LibHalPropertySetIterator it;
			unsigned int psi_num_passed;
			unsigned int psi_num_elems;

			if ((pset = libhal_device_get_all_properties (ctx, "/org/freedesktop/Hal/devices/testobj1", 
								      &error)) == NULL)
				return FALSE;
			printf ("SUCCESS110\n");

			psi_num_passed = 0;
			psi_num_elems = libhal_property_set_get_num_elems (pset);

			for (libhal_psi_init (&it, pset); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
				type = libhal_psi_get_type (&it);
				key = libhal_psi_get_key (&it);

				switch (type) {
				case LIBHAL_PROPERTY_TYPE_STRING:
					if (strcmp (key, "info.udi") == 0) {
						if (strcmp (libhal_psi_get_string (&it), 
							    "/org/freedesktop/Hal/devices/testobj1") == 0) {
							psi_num_passed++;
						} else {
							printf ("fail on %s\n", key);
						}
					} else if (strcmp (key, "test.string") == 0) {
						if (strcmp (libhal_psi_get_string (&it), 
							    "fooooobar22") == 0) {
							psi_num_passed++;
						} else {
							printf ("fail on %s\n", key);
						}
					} else if (strcmp (key, "test.string2") == 0) {
						if (strcmp (libhal_psi_get_string (&it), 
							    "fooøةמ") == 0) {
							psi_num_passed++;
						} else {
							printf ("fail on %s\n", key);
						}
					}
					break;

				case LIBHAL_PROPERTY_TYPE_INT32:
					if (strcmp (key, "test.int") == 0) {
						if (libhal_psi_get_int (&it) == 42)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_UINT64:
					if (strcmp (key, "test.uint64") == 0) {
						if (libhal_psi_get_uint64 (&it) == ((((dbus_uint64_t)1)<<35) + 5))
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_BOOLEAN:
					if (strcmp (key, "test.bool") == 0) {
						if (libhal_psi_get_bool (&it) == TRUE)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_DOUBLE:
					if (strcmp (key, "test.double") == 0) {
						double val = 0.53434343;
						double val2;

						val2 = libhal_psi_get_double (&it);
						if (memcmp (&val, &val2, sizeof (double)) == 0)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				case LIBHAL_PROPERTY_TYPE_STRLIST:
					if (strcmp (key, "test.strlist") == 0) {
						char **val;
					
						val = libhal_psi_get_strlist (&it);
						if (libhal_string_array_length (val) == 2 &&
						    strcmp (val[0], "foostrlist2") == 0 &&
						    strcmp (val[1], "foostrlist3") == 0 &&
						    val[2] == NULL)
							psi_num_passed++;
						else
							printf ("fail on %s\n", key);
					}
					break;

				default:
					printf ("    *** unknown type for key %s\n", key);
					break;
				}
			}
			libhal_free_property_set (pset);

			if (psi_num_passed != psi_num_elems) {
				printf ("FAILED111\n");			
				goto fail;
			}
			printf ("SUCCESS111\n");			


		} /* end libhal_test_psi */

	
		printf ("Passed all libhal tests\n");
		passed = TRUE;
		
	fail:
		if (dbus_error_is_set (&error))
			dbus_error_free (&error);
			
		send_tests_done (conn, passed);
		exit (1);

	} else {
		printf ("child pid=%d\n", child_pid);
	}
	return TRUE;
}