1 /*************************************************************************** 2 * 3 * probe-volume.c : probe volumes 4 * 5 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 6 * Use is subject to license terms. 7 * 8 * Licensed under the Academic Free License version 2.1 9 * 10 **************************************************************************/ 11 12 #pragma ident "%Z%%M% %I% %E% SMI" 13 14 #ifdef HAVE_CONFIG_H 15 # include <config.h> 16 #endif 17 18 #include <errno.h> 19 #include <string.h> 20 #include <stdlib.h> 21 #include <stdio.h> 22 #include <sys/ioctl.h> 23 #include <sys/types.h> 24 #include <sys/stat.h> 25 #include <fcntl.h> 26 #include <unistd.h> 27 #include <ctype.h> 28 #include <time.h> 29 #include <sys/time.h> 30 #include <sys/dkio.h> 31 #include <sys/cdio.h> 32 #include <sys/fdio.h> 33 #include <libnvpair.h> 34 #include <libfstyp.h> 35 #include <sys/vtoc.h> 36 #include <sys/efi_partition.h> 37 #include <sys/fs/hsfs_spec.h> 38 #include <sys/fs/hsfs_isospec.h> 39 #include <priv.h> 40 41 #include <libhal.h> 42 #include <cdutils.h> 43 #include <fsutils.h> 44 #include <logger.h> 45 46 static void 47 my_dbus_error_free(DBusError *error) 48 { 49 if (dbus_error_is_set(error)) { 50 dbus_error_free(error); 51 } 52 } 53 54 /* 55 * Return a copy of a string without trailing spaces. If 'len' is non-zero, 56 * it specifies max length, otherwise the string must be null-terminated. 57 */ 58 static char * 59 rtrim_copy(char *src, int len) 60 { 61 char *dst, *p; 62 63 if (len == 0) { 64 len = strlen(src); 65 } 66 if ((dst = calloc(1, len + 1)) != NULL) { 67 strncpy(dst, src, len); 68 p = dst + len - 1; 69 while ((p >= dst) && (isspace(*p))) { 70 *p-- = '\0'; 71 } 72 } 73 return (dst); 74 } 75 76 static void 77 set_fstyp_properties (LibHalContext *ctx, const char *udi, const char *fstype, nvlist_t *fsattr) 78 { 79 char buf[256]; 80 DBusError error; 81 char *uuid = NULL; 82 char *label_orig = NULL; 83 char *label = NULL; 84 LibHalChangeSet *cs; 85 86 dbus_error_init (&error); 87 88 if ((cs = libhal_device_new_changeset (udi)) == NULL) { 89 return; 90 } 91 92 libhal_changeset_set_property_string (cs, "volume.fsusage", "filesystem"); 93 libhal_changeset_set_property_string (cs, "volume.fstype", fstype); 94 95 /* label */ 96 (void) nvlist_lookup_string(fsattr, "gen_volume_label", &label_orig); 97 if (label_orig != NULL) { 98 label = rtrim_copy(label_orig, 0); 99 } 100 if ((label != NULL) && (label[0] != '\0')) { 101 libhal_changeset_set_property_string (cs, "volume.label", label); 102 libhal_changeset_set_property_string (cs, "info.product", label); 103 } else { 104 libhal_changeset_set_property_string (cs, "volume.label", ""); 105 snprintf (buf, sizeof (buf), "Volume (%s)", fstype); 106 libhal_changeset_set_property_string (cs, "info.product", buf); 107 } 108 free(label); 109 110 /* uuid */ 111 if (nvlist_lookup_string(fsattr, "gen_uuid", &uuid) == 0) { 112 libhal_changeset_set_property_string (cs, "volume.uuid", uuid); 113 } else { 114 libhal_changeset_set_property_string (cs, "volume.uuid", ""); 115 } 116 117 libhal_device_commit_changeset (ctx, cs, &error); 118 libhal_device_free_changeset (cs); 119 120 my_dbus_error_free (&error); 121 } 122 123 /* 124 * hsfs/iso9660 contents detection: Video DVD, Video CD, etc. 125 */ 126 static void 127 hsfs_contents(int fd, off_t probe_offset, LibHalContext *ctx, const char *udi) 128 { 129 size_t secsz = ISO_SECTOR_SIZE; 130 uchar_t buf[ISO_SECTOR_SIZE]; 131 int ptbl_lbn, ptbl_size; 132 int off, reloff, readoff; 133 uchar_t *p; 134 char *name; 135 int name_len; 136 int ipe_len; 137 DBusError error; 138 139 /* 140 * find 1st Primary Volume Descriptor 141 */ 142 readoff = probe_offset + ISO_VOLDESC_SEC * secsz; 143 if (pread (fd, buf, secsz, readoff) != secsz) { 144 return; 145 } 146 while (ISO_DESC_TYPE (buf) != ISO_VD_PVD) { 147 if (ISO_DESC_TYPE (buf) == ISO_VD_EOV) { 148 return; 149 } 150 readoff += secsz; 151 if (pread (fd, buf, secsz, readoff) != secsz) { 152 return; 153 } 154 } 155 156 /* 157 * PVD contains size and offset of the LSB/MSB path table 158 */ 159 ptbl_size = ISO_PTBL_SIZE (buf); 160 #if defined(_LITTLE_ENDIAN) 161 ptbl_lbn = ISO_PTBL_MAN_LS (buf); 162 #else 163 ptbl_lbn = ISO_PTBL_MAN_MS (buf); 164 #endif 165 166 /* 167 * Look through path table entries 168 */ 169 readoff = probe_offset + ptbl_lbn * secsz; 170 if (pread (fd, buf, secsz, readoff) != secsz) { 171 return; 172 } 173 dbus_error_init (&error); 174 175 for (off = reloff = 0; 176 off < ptbl_size; 177 off += ipe_len, reloff += ipe_len) { 178 179 /* load sectors on demand */ 180 if (reloff >= secsz) { 181 readoff += secsz; 182 if (pread (fd, buf, secsz, readoff) != secsz) { 183 break; 184 } 185 reloff -= secsz; 186 } 187 188 p = buf + reloff; 189 name_len = IPE_NAME_LEN(p); 190 ipe_len = IPE_FPESIZE + name_len + (name_len % 2); 191 192 /* only interested in root directories */ 193 if (IPE_PARENT_NO (p) != 1) { 194 continue; 195 } 196 if ((name_len < 2) || (name_len > IDE_MAX_NAME_LEN)) { 197 continue; 198 } 199 200 name = (char *)IPE_NAME (p); 201 if (strncasecmp (name, "VIDEO_TS", min (8, name_len)) == 0) { 202 libhal_device_set_property_bool (ctx, udi, 203 "volume.disc.is_videodvd", TRUE, &error); 204 } else if (strncasecmp (name, "VCD", min (3, name_len)) == 0) { 205 libhal_device_set_property_bool (ctx, udi, 206 "volume.disc.is_vcd", TRUE, &error); 207 } else if (strncasecmp (name, "SVCD", min (4, name_len)) == 0) { 208 libhal_device_set_property_bool (ctx, udi, 209 "volume.disc.is_svcd", TRUE, &error); 210 } 211 } 212 213 my_dbus_error_free (&error); 214 } 215 216 static dbus_bool_t 217 probe_disc (int fd, LibHalContext *ctx, const char *udi, dbus_bool_t *has_data, 218 dbus_bool_t *has_audio) 219 { 220 DBusError error; 221 disc_info_t di; 222 int profile; 223 dbus_bool_t is_blank, is_appendable, is_rewritable; 224 char *disc_type = "cd_rom"; 225 uint64_t capacity = 0; 226 int i; 227 LibHalChangeSet *cs; 228 229 dbus_error_init (&error); 230 231 if (get_disc_info (fd, &di)) { 232 is_blank = (di.disc_status == 0); 233 is_appendable = (di.disc_status == 1); 234 is_rewritable = (di.erasable != 0); 235 } else { 236 is_blank = is_appendable = is_rewritable = FALSE; 237 } 238 239 if (get_current_profile (fd, &profile)) { 240 switch (profile) { 241 case 0x08: /* CD-ROM */ 242 disc_type = "cd_rom"; 243 break; 244 case 0x09: /* CD-R */ 245 disc_type = "cd_r"; 246 break; 247 case 0x0A: /* CD-RW */ 248 disc_type = "cd_rw"; 249 is_rewritable = TRUE; 250 break; 251 case 0x10: /* DVD-ROM */ 252 disc_type = "dvd_rom"; 253 break; 254 case 0x11: /* DVD-R Sequential */ 255 disc_type = "dvd_r"; 256 break; 257 case 0x12: /* DVD-RAM */ 258 disc_type = "dvd_ram"; 259 is_rewritable = TRUE; 260 break; 261 case 0x13: /* DVD-RW Restricted Overwrite */ 262 disc_type = "dvd_rw"; 263 is_rewritable = TRUE; 264 break; 265 case 0x14: /* DVD-RW Sequential */ 266 disc_type = "dvd_rw"; 267 is_rewritable = TRUE; 268 break; 269 case 0x1A: /* DVD+RW */ 270 disc_type = "dvd_plus_rw"; 271 is_rewritable = TRUE; 272 break; 273 case 0x1B: /* DVD+R */ 274 disc_type = "dvd_plus_r"; 275 break; 276 case 0x2B: /* DVD+R Double Layer */ 277 disc_type = "dvd_plus_r_dl"; 278 break; 279 case 0x40: /* BD-ROM */ 280 disc_type = "bd_rom"; 281 break; 282 case 0x41: /* BD-R Sequential */ 283 disc_type = "bd_r"; 284 break; 285 case 0x42: /* BD-R Random */ 286 disc_type = "bd_r"; 287 break; 288 case 0x43: /* BD-RE */ 289 disc_type = "bd_re"; 290 is_rewritable = TRUE; 291 break; 292 case 0x50: /* HD DVD-ROM */ 293 disc_type = "hddvd_rom"; 294 break; 295 case 0x51: /* HD DVD-R */ 296 disc_type = "hddvd_r"; 297 break; 298 case 0x52: /* HD DVD-Rewritable */ 299 disc_type = "hddvd_rw"; 300 is_rewritable = TRUE; 301 break; 302 } 303 304 (void) get_disc_capacity_for_profile(fd, profile, &capacity); 305 } 306 307 *has_audio = *has_data = FALSE; 308 if (!is_blank) { 309 uchar_t smalltoc[12]; 310 size_t toc_size; 311 uchar_t *toc, *p; 312 313 /* 314 * XXX for some reason CDROMREADTOCENTRY fails on video DVDs, 315 * but extracting the toc directly works okay. 316 */ 317 if (!read_toc(fd, 0, 1, 4, smalltoc)) { 318 HAL_DEBUG(("read_toc failed")); 319 *has_data = B_TRUE; /* probe for fs anyway */ 320 } else { 321 toc_size = smalltoc[0] * 256 + smalltoc[1] + 2; 322 toc = (uchar_t *)calloc(1, toc_size); 323 if (toc == NULL || !read_toc(fd, 0, 1, toc_size, toc)) { 324 HAL_DEBUG (("read_toc again failed")); 325 } else { 326 for (p = &toc[4]; p < (toc + toc_size); p += 8) { 327 /* skip leadout */ 328 if (p[2] == 0xAA) { 329 continue; 330 } 331 if (p[1] & 4) { 332 *has_data = B_TRUE; 333 } else { 334 *has_audio = B_TRUE; 335 } 336 } 337 } 338 free(toc); 339 } 340 } 341 342 if ((cs = libhal_device_new_changeset (udi)) == NULL) { 343 return (FALSE); 344 } 345 libhal_changeset_set_property_string (cs, "volume.disc.type", disc_type); 346 libhal_changeset_set_property_bool (cs, "volume.disc.is_blank", is_blank); 347 libhal_changeset_set_property_bool (cs, "volume.disc.has_audio", *has_audio); 348 libhal_changeset_set_property_bool (cs, "volume.disc.has_data", *has_data); 349 libhal_changeset_set_property_bool (cs, "volume.disc.is_appendable", is_appendable); 350 libhal_changeset_set_property_bool (cs, "volume.disc.is_rewritable", is_rewritable); 351 libhal_changeset_set_property_uint64 (cs, "volume.disc.capacity", capacity); 352 353 libhal_changeset_set_property_bool (cs, "volume.disc.is_videodvd", FALSE); 354 libhal_changeset_set_property_bool (cs, "volume.disc.is_vcd", FALSE); 355 libhal_changeset_set_property_bool (cs, "volume.disc.is_svcd", FALSE); 356 357 libhal_device_commit_changeset (ctx, cs, &error); 358 libhal_device_free_changeset (cs); 359 360 out: 361 my_dbus_error_free (&error); 362 363 return (TRUE); 364 } 365 366 static void 367 drop_privileges () 368 { 369 priv_set_t *pPrivSet = NULL; 370 priv_set_t *lPrivSet = NULL; 371 372 /* 373 * Start with the 'basic' privilege set and then remove any 374 * of the 'basic' privileges that will not be needed. 375 */ 376 if ((pPrivSet = priv_str_to_set("basic", ",", NULL)) == NULL) { 377 return; 378 } 379 380 /* Clear privileges we will not need from the 'basic' set */ 381 (void) priv_delset(pPrivSet, PRIV_FILE_LINK_ANY); 382 (void) priv_delset(pPrivSet, PRIV_PROC_INFO); 383 (void) priv_delset(pPrivSet, PRIV_PROC_SESSION); 384 (void) priv_delset(pPrivSet, PRIV_PROC_EXEC); 385 (void) priv_delset(pPrivSet, PRIV_PROC_FORK); 386 387 /* for uscsi */ 388 (void) priv_addset(pPrivSet, PRIV_SYS_DEVICES); 389 390 391 /* to open logindevperm'd devices */ 392 (void) priv_addset(pPrivSet, PRIV_FILE_DAC_READ); 393 394 /* Set the permitted privilege set. */ 395 if (setppriv(PRIV_SET, PRIV_PERMITTED, pPrivSet) != 0) { 396 return; 397 } 398 399 /* Clear the limit set. */ 400 if ((lPrivSet = priv_allocset()) == NULL) { 401 return; 402 } 403 404 priv_emptyset(lPrivSet); 405 406 if (setppriv(PRIV_SET, PRIV_LIMIT, lPrivSet) != 0) { 407 return; 408 } 409 410 priv_freeset(lPrivSet); 411 } 412 413 int 414 main (int argc, char *argv[]) 415 { 416 int fd, rfd; 417 int ret; 418 char *udi; 419 char *device_file, *raw_device_file; 420 char *devpath, *rdevpath; 421 boolean_t is_dos; 422 int dos_num; 423 LibHalContext *ctx = NULL; 424 DBusError error; 425 DBusConnection *conn; 426 char *parent_udi; 427 char *storage_device; 428 char *is_disc_str; 429 int fdc; 430 dbus_bool_t is_disc = FALSE; 431 dbus_bool_t is_floppy = FALSE; 432 unsigned int block_size; 433 dbus_uint64_t vol_size; 434 dbus_bool_t has_data = TRUE; /* probe for fs by default */ 435 dbus_bool_t has_audio = FALSE; 436 char *partition_scheme = NULL; 437 dbus_uint64_t partition_start = 0; 438 int partition_number = 0; 439 struct vtoc vtoc; 440 dk_gpt_t *gpt; 441 struct dk_minfo mi; 442 int i, dos_cnt; 443 fstyp_handle_t fstyp_handle; 444 int systid, relsect, numsect; 445 off_t probe_offset = 0; 446 int num_volumes; 447 char **volumes; 448 dbus_uint64_t v_start; 449 const char *fstype; 450 nvlist_t *fsattr; 451 452 fd = rfd = -1; 453 454 ret = 1; 455 456 if ((udi = getenv ("UDI")) == NULL) { 457 goto out; 458 } 459 if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL) { 460 goto out; 461 } 462 if ((raw_device_file = getenv ("HAL_PROP_BLOCK_SOLARIS_RAW_DEVICE")) == NULL) { 463 goto out; 464 } 465 if (!dos_to_dev(device_file, &rdevpath, &dos_num)) { 466 rdevpath = raw_device_file; 467 } 468 if (!(is_dos = dos_to_dev(device_file, &devpath, &dos_num))) { 469 devpath = device_file; 470 } 471 if ((parent_udi = getenv ("HAL_PROP_INFO_PARENT")) == NULL) { 472 goto out; 473 } 474 if ((storage_device = getenv ("HAL_PROP_BLOCK_STORAGE_DEVICE")) == NULL) { 475 goto out; 476 } 477 478 is_disc_str = getenv ("HAL_PROP_VOLUME_IS_DISC"); 479 if (is_disc_str != NULL && strcmp (is_disc_str, "true") == 0) { 480 is_disc = TRUE; 481 } else { 482 is_disc = FALSE; 483 } 484 485 drop_privileges (); 486 487 setup_logger (); 488 489 dbus_error_init (&error); 490 if ((ctx = libhal_ctx_init_direct (&error)) == NULL) 491 goto out; 492 493 HAL_DEBUG (("Doing probe-volume for %s\n", device_file)); 494 495 fd = open (devpath, O_RDONLY | O_NONBLOCK); 496 if (fd < 0) { 497 goto out; 498 } 499 rfd = open (rdevpath, O_RDONLY | O_NONBLOCK); 500 if (rfd < 0) { 501 goto out; 502 } 503 504 /* if it's a floppy with no media, bail out */ 505 if (ioctl(rfd, FDGETCHANGE, &fdc) == 0) { 506 is_floppy = TRUE; 507 if (fdc & FDGC_CURRENT) { 508 goto out; 509 } 510 } 511 512 /* block size and total size */ 513 if (ioctl(rfd, DKIOCGMEDIAINFO, &mi) != -1) { 514 block_size = mi.dki_lbsize; 515 vol_size = mi.dki_capacity * block_size; 516 } else if (errno == ENXIO) { 517 /* driver supports ioctl, but media is not available */ 518 goto out; 519 } else { 520 /* driver does not support ioctl, e.g. lofi */ 521 block_size = 512; 522 vol_size = 0; 523 } 524 libhal_device_set_property_int (ctx, udi, "volume.block_size", block_size, &error); 525 my_dbus_error_free (&error); 526 libhal_device_set_property_uint64 (ctx, udi, "volume.size", vol_size, &error); 527 my_dbus_error_free (&error); 528 529 if (is_disc) { 530 if (!probe_disc (rfd, ctx, udi, &has_data, &has_audio)) { 531 HAL_DEBUG (("probe_disc failed, skipping fstyp")); 532 goto out; 533 } 534 /* with audio present, create volume even if fs probing fails */ 535 if (has_audio) { 536 ret = 0; 537 } 538 } 539 540 if (!has_data) { 541 goto skip_fs; 542 } 543 544 /* don't support partitioned floppy */ 545 if (is_floppy) { 546 goto skip_part; 547 } 548 549 /* 550 * first get partitioning info 551 */ 552 if (is_dos) { 553 /* for a dos drive find partition offset */ 554 if (!find_dos_drive(fd, dos_num, &relsect, &numsect, &systid)) { 555 goto out; 556 } 557 partition_scheme = "mbr"; 558 partition_start = (dbus_uint64_t)relsect * 512; 559 partition_number = dos_num; 560 probe_offset = (off_t)relsect * 512; 561 } else { 562 if ((partition_number = read_vtoc(rfd, &vtoc)) >= 0) { 563 if (!vtoc_one_slice_entire_disk(&vtoc)) { 564 partition_scheme = "smi"; 565 if (partition_number < vtoc.v_nparts) { 566 if (vtoc.v_part[partition_number].p_size == 0) { 567 HAL_DEBUG (("zero size partition")); 568 } 569 partition_start = vtoc.v_part[partition_number].p_start * block_size; 570 } 571 } 572 } else if ((partition_number = efi_alloc_and_read(rfd, &gpt)) >= 0) { 573 partition_scheme = "gpt"; 574 if (partition_number < gpt->efi_nparts) { 575 if (gpt->efi_parts[partition_number].p_size == 0) { 576 HAL_DEBUG (("zero size partition")); 577 } 578 partition_start = gpt->efi_parts[partition_number].p_start * block_size; 579 } 580 efi_free(gpt); 581 } 582 probe_offset = 0; 583 } 584 585 if (partition_scheme != NULL) { 586 libhal_device_set_property_string (ctx, udi, "volume.partition.scheme", partition_scheme, &error); 587 my_dbus_error_free (&error); 588 libhal_device_set_property_int (ctx, udi, "volume.partition.number", partition_number, &error); 589 my_dbus_error_free (&error); 590 libhal_device_set_property_uint64 (ctx, udi, "volume.partition.start", partition_start, &error); 591 my_dbus_error_free (&error); 592 libhal_device_set_property_bool (ctx, udi, "volume.is_partition", TRUE, &error); 593 my_dbus_error_free (&error); 594 } else { 595 libhal_device_set_property_bool (ctx, udi, "volume.is_partition", FALSE, &error); 596 my_dbus_error_free (&error); 597 } 598 599 /* 600 * ignore duplicate partitions 601 */ 602 if ((volumes = libhal_manager_find_device_string_match ( 603 ctx, "block.storage_device", storage_device, &num_volumes, &error)) != NULL) { 604 my_dbus_error_free (&error); 605 for (i = 0; i < num_volumes; i++) { 606 if (strcmp (udi, volumes[i]) == 0) { 607 continue; /* skip self */ 608 } 609 v_start = libhal_device_get_property_uint64 (ctx, volumes[i], "volume.partition.start", &error); 610 if (dbus_error_is_set(&error)) { 611 dbus_error_free(&error); 612 continue; 613 } 614 if (v_start == partition_start) { 615 HAL_DEBUG (("duplicate partition")); 616 goto out; 617 } 618 } 619 libhal_free_string_array (volumes); 620 } 621 622 skip_part: 623 624 /* 625 * now determine fs type 626 * 627 * XXX We could get better performance from block device, 628 * but for now we use raw device because: 629 * 630 * - fstyp_udfs has a bug that it only works on raw 631 * 632 * - sd has a bug that causes extremely slow reads 633 * and incorrect probing of hybrid audio/data media 634 */ 635 if (fstyp_init(rfd, probe_offset, NULL, &fstyp_handle) != 0) { 636 HAL_DEBUG (("fstyp_init failed")); 637 goto out; 638 } 639 if ((fstyp_ident(fstyp_handle, NULL, &fstype) != 0) || 640 (fstyp_get_attr(fstyp_handle, &fsattr) != 0)) { 641 HAL_DEBUG (("fstyp ident or get_attr failed")); 642 fstyp_fini(fstyp_handle); 643 goto out; 644 } 645 set_fstyp_properties (ctx, udi, fstype, fsattr); 646 647 if (strcmp (fstype, "hsfs") == 0) { 648 hsfs_contents (fd, probe_offset, ctx, udi); 649 } 650 651 fstyp_fini(fstyp_handle); 652 653 skip_fs: 654 655 ret = 0; 656 657 out: 658 if (fd >= 0) 659 close (fd); 660 if (rfd >= 0) 661 close (rfd); 662 663 if (ctx != NULL) { 664 my_dbus_error_free (&error); 665 libhal_ctx_shutdown (ctx, &error); 666 libhal_ctx_free (ctx); 667 } 668 669 return ret; 670 671 } 672