1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
|
/********************************************************************
*
* L I N U X E J E C T C O M M A N D
*
* by Jeff Tranter (tranter@pobox.com)
*
********************************************************************
*
* Copyright (C) 1994-2005 Jeff Tranter (tranter@pobox.com)
* Copyright (C) 2004, 2005 Frank Lichtenheld (djpig@debian.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
********************************************************************
*
* See the man page for a description of what this program does and what
* the requirements to run it are.
*
*/
#include "i18n.h"
#ifndef DEFAULTDEVICE
#error DEFAULTDEVICE not set, check Makefile
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <dirent.h>
#include <sys/sysmacros.h> /* makedev() */
#ifdef GETOPTLONG
#include <getopt.h>
#endif /* GETOPTLONG */
#include <errno.h>
#include <regex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/mtio.h>
#include <sys/mount.h>
#if defined(__linux__)
#include <linux/types.h>
#include <linux/cdrom.h>
#include <linux/fd.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
#include <sys/time.h>
/* Used by the ToggleTray() function. If ejecting the tray takes this
* time or less, the tray was probably already ejected, so we close it
* again.
*/
#define TRAY_WAS_ALREADY_OPEN_USECS 200000 /* about 0.2 seconds */
#define CLOSE(fd) if (close(fd)==-1) { \
perror(programName); \
exit(1); \
}
#define FCLOSE(fd) if (fclose(fd)==-1) { \
perror(programName); \
exit(1); \
}
#define HAVE_EJECT_SCSI
#define HAVE_EJECT_FLOPPY
#define HAVE_EJECT_TAPE
#elif defined(__FreeBSD_kernel__)
#include <sys/cdio.h>
#endif /* if defined(__linux__) */
#define CLOSE(fd) if (close(fd)==-1) { \
perror(programName); \
exit(1); \
}
#define FCLOSE(fd) if (fclose(fd)==-1) { \
perror(programName); \
exit(1); \
}
/* Global Variables */
static const char *version = VERSION; /* program version */
int a_option = 0; /* command flags and arguments */
int c_option = 0;
int d_option = 0;
int f_option = 0;
int h_option = 0;
int n_option = 0;
int q_option = 0;
int r_option = 0;
int s_option = 0;
int t_option = 0;
int T_option = 0;
int X_option = 0;
int v_option = 0;
int x_option = 0;
int p_option = 0;
int m_option = 0;
int a_arg = 0;
int c_arg = 0;
int x_arg = 0;
static char *programName; /* used in error messages */
/*
* These are the basenames of devices which can have multiple
* partitions per device.
*/
static const char *partitionDevice[] = {
"hd",
"sd",
"xd",
"dos_hd",
"mfm",
"ad",
"ed",
"ftl",
"pd",
0};
/* Display command usage on standard error and exit. */
static void usage()
{
// perror(_("%s: device is `%s'\n"));
fprintf(stderr,_(
"Eject version %s by Jeff Tranter (tranter@pobox.com)\n"
"Usage:\n"
" eject -h -- display command usage and exit\n"
" eject -V -- display program version and exit\n"
" eject [-vnrsfqpm] [<name>] -- eject device\n"
" eject [-vn] -d -- display default device\n"
" eject [-vn] -a on|off|1|0 [<name>] -- turn auto-eject feature on or off\n"
" eject [-vn] -c <slot> [<name>] -- switch discs on a CD-ROM changer\n"
" eject [-vn] -t [<name>] -- close tray\n"
" eject [-vn] -T [<name>] -- toggle tray\n"
" eject [-vn] -x <speed> [<name>] -- set CD-ROM max speed\n"
" eject [-vn] -X [<name>] -- list CD-ROM available speeds\n"
"Options:\n"
" -v\t-- enable verbose output\n"
" -n\t-- don't eject, just show device found\n"
" -r\t-- eject CD-ROM\n"
#ifdef HAVE_EJECT_SCSI
" -s\t-- eject SCSI device\n"
#endif
#ifdef HAVE_EJECT_FLOPPY
" -f\t-- eject floppy\n"
#endif
#ifdef HAVE_EJECT_TAPE
" -q\t-- eject tape\n"
#endif
" -p\t-- use /proc/mounts instead of /etc/mtab\n"
" -m\t-- do not unmount device even if it is mounted\n"
)
, version);
#ifdef GETOPTLONG
fprintf(stderr,_(
"Long options:\n"
" -h --help -v --verbose -d --default\n"
" -a --auto -c --changerslot -t --trayclose -x --cdspeed\n"
" -r --cdrom"
#ifdef HAVE_EJECT_SCSI
" -s --scsi"
#endif
#ifdef HAVE_EJECT_FLOPPY
" -f --floppy"
#endif
" -X --listspeed"
#ifdef HAVE_EJECT_TAPE
" -q --tape"
#endif
"\n"
" -n --noop -V --version\n"
" -p --proc -m --no-unmount -T --traytoggle\n"));
#endif /* GETOPTLONG */
fprintf(stderr,_(
"Parameter <name> can be a device file or a mount point.\n"
"If omitted, name defaults to `%s'.\n"
"By default tries -r, -s, -f, and -q in order until success.\n"),
DEFAULTDEVICE);
exit(1);
}
/* Handle command line options. */
static void parse_args(int argc, char **argv, char **device)
{
const char *flags = "a:c:x:dfhnqrstTXvVpm";
#ifdef GETOPTLONG
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{"default", no_argument, NULL, 'd'},
{"auto", required_argument, NULL, 'a'},
{"changerslot", required_argument, NULL, 'c'},
{"trayclose", no_argument, NULL, 't'},
{"traytoggle", no_argument, NULL, 'T'},
{"cdspeed", required_argument, NULL, 'x'},
{"listspeed", no_argument, NULL, 'X'},
{"noop", no_argument, NULL, 'n'},
{"cdrom", no_argument, NULL, 'r'},
{"scsi", no_argument, NULL, 's'},
{"floppy", no_argument, NULL, 'f'},
{"tape", no_argument, NULL, 'q'},
{"version", no_argument, NULL, 'V'},
{"proc", no_argument, NULL, 'p'},
{"no-unmount", no_argument, NULL, 'm'},
{0, 0, 0, 0}
};
int option_index;
#endif /* GETOPTLONG */
int c;
#ifdef GETOPTLONG
while ((c = getopt_long(argc, argv, flags, long_options, &option_index)) != EOF) {
#else
while ((c = getopt(argc, argv, flags)) != EOF) {
#endif /* GETOPTLONG */
switch (c) {
case 'a':
a_option = 1;
if (!strcmp(optarg, "0"))
a_arg = 0;
else if (!strcmp(optarg, "off"))
a_arg = 0;
else if (!strcmp(optarg, "1"))
a_arg = 1;
else if (!strcmp(optarg, "on"))
a_arg = 1;
else {
fprintf(stderr, _("%s: invalid argument to --auto/-a option\n"), programName);
exit(1);
}
break;
case 'c':
c_option = 1;
/* atoi() returns 0 on error, so "0" must be parsed separately */
if (!strcmp(optarg, "0"))
c_arg = 0;
else {
c_arg = atoi(optarg);
if (c_arg <= 0) {
fprintf(stderr, _("%s: invalid argument to --changerslot/-c option\n"), programName);
exit(1);
}
}
break;
case 'x':
x_option = 1;
if (!strcmp(optarg, "0"))
x_arg = 0;
else {
x_arg = atoi(optarg);
if (x_arg <= 0) {
fprintf(stderr, _("%s: invalid argument to --cdspeed/-x option\n"), programName);
exit(1);
}
}
break;
case 'd':
d_option = 1;
break;
case 'f':
f_option = 1;
break;
case 'h':
usage();
exit(0);
break;
case 'm':
m_option = 1;
break;
case 'n':
n_option = 1;
break;
case 'p':
p_option = 1;
break;
case 'q':
q_option = 1;
break;
case 'r':
r_option = 1;
break;
case 's':
s_option = 1;
break;
case 't':
t_option = 1;
break;
case 'X':
X_option = 1;
break;
case 'T':
T_option = 1;
break;
case 'v':
v_option = 1;
break;
case 'V':
printf(_("eject version %s by Jeff Tranter (tranter@pobox.com)\n"), version);
exit(0);
break;
case '?':
exit(1);
break;
}
}
/* check for a single additional argument */
if ((argc - optind) > 1) {
fprintf(stderr, _("%s: too many arguments\n"), programName);
exit(1);
}
if ((argc - optind) == 1) { /* one argument */
*device = strdup(argv[optind]);
}
}
/* Return 1 if file/device exists, 0 otherwise. */
static int FileExists(const char *name, const int try, int *found)
{
if (!found) return -1;
/*
* access() uses the UID, not the EUID. This way a normal user
* cannot find out if a file (say, /root/fubar) exists or not, even
* if eject is SUID root
*/
if (access (name, F_OK) == 0) {
(*found)++;
if (try <= (*found))
return 1;
else
return 0;
} else {
return 0;
}
}
/*
* Given name, such as foo, see if any of the following exist:
*
* foo (if foo starts with '.' or '/')
* /dev/foo
* /media/foo
* /mnt/foo
* /dev/cdroms/foo
* /dev/cdroms/foo0
* /dev/dsk/foo
* /dev/rdsk/foo
* ./foo
*
* If found, return the full path. If not found, return 0.
* Returns pointer to dynamically allocated string.
*/
static char *FindDevice(const char *name)
{
char *buf;
static int try = 0;
int found = 0;
buf = (char *) malloc(strlen(name)+14); /* to allow for "/dev/cdroms/ + "0" + null */
if (buf==NULL) {
fprintf(stderr, _("%s: could not allocate memory\n"), programName);
exit(1);
}
if (try == INT_MAX) {
fprintf(stderr, _("%s: FindDevice called too often\n"), programName );;
exit(1);
} else
try++;
if ((name[0] == '.') || (name[0] == '/')) {
strcpy(buf, name);
if (FileExists(buf, try, &found))
return buf;
}
strcpy(buf, "/dev/");
strcat(buf, name);
if (FileExists(buf, try, &found))
return buf;
strcpy(buf, "/media/");
strcat(buf, name);
if (FileExists(buf, try, &found))
return buf;
strcpy(buf, "/mnt/");
strcat(buf, name);
if (FileExists(buf, try, &found))
return buf;
/* for devfs under Linux */
strcpy(buf, "/dev/cdroms/");
strcat(buf, name);
if (FileExists(buf, try, &found))
return buf;
strcpy(buf, "/dev/cdroms/");
strcat(buf, name);
strcat(buf, "0");
if (FileExists(buf, try, &found))
return buf;
/* for devfs under Solaris */
strcpy(buf, "/dev/rdsk/");
strcat(buf, name);
if (FileExists(buf, try, &found))
return buf;
strcpy(buf, "/dev/dsk/");
strcat(buf, name);
if (FileExists(buf, try, &found))
return buf;
strcpy(buf, "./");
strcat(buf, name);
if (FileExists(buf, try, &found))
return buf;
free(buf);
buf = 0;
return 0;
}
/* Set or clear auto-eject mode. */
static void AutoEject(int fd, int onOff)
{
int status = -1;
#if defined(CDROM_SET_OPTIONS) && defined(CDROM_CLEAR_OPTIONS)
if (onOff)
status = ioctl(fd, CDROM_SET_OPTIONS, CDO_AUTO_EJECT);
else
status = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_AUTO_EJECT);
#else
errno = ENOSYS;
#endif
if (status < 0) {
fprintf(stderr, _("%s: CD-ROM auto-eject command failed: %s\n"), programName, strerror(errno));
exit(1);
}
}
/*
* Changer select. CDROM_SELECT_DISC is preferred, older kernels used
* CDROMLOADFROMSLOT.
*/
static void ChangerSelect(int fd, int slot)
{
int status;
#ifdef CDROM_SELECT_DISC
status = ioctl(fd, CDROM_SELECT_DISC, slot);
if (status < 0) {
fprintf(stderr, _("%s: CD-ROM select disc command failed: %s\n"), programName, strerror(errno));
exit(1);
}
#elif defined CDROMLOADFROMSLOT
status = ioctl(fd, CDROMLOADFROMSLOT, slot);
if (status != 0) {
fprintf(stderr, _("%s: CD-ROM load from slot command failed: %s\n"), programName, strerror(errno));
exit(1);
}
#else
fprintf(stderr, _("%s: IDE/ATAPI CD-ROM changer not supported by this kernel\n"), programName);
#endif
}
/*
* Close tray. Not supported by older kernels.
*/
static void CloseTray(int fd)
{
int status;
#if defined(CDROMCLOSETRAY) || defined(CDIOCCLOSE)
#if defined(CDROMCLOSETRAY)
status = ioctl(fd, CDROMCLOSETRAY);
#elif defined(CDIOCCLOSE)
status = ioctl(fd, CDIOCCLOSE);
#endif
if (status != 0) {
fprintf(stderr, _("%s: CD-ROM tray close command failed: %s\n"), programName, strerror(errno));
exit(1);
}
#else
fprintf(stderr, _("%s: CD-ROM tray close command not supported by this kernel\n"), programName);
#endif
}
/*
* Toggle tray.
*
* Written by Benjamin Schwenk <benjaminschwenk@yahoo.de> and
* Sybren Stuvel <sybren@thirdtower.com>
*
* Not supported by older kernels because it might use
* CloseTray().
*
*/
static void ToggleTray(int fd)
{
struct timeval time_start, time_stop;
int time_elapsed;
#ifdef CDROMCLOSETRAY
/* Try to open the CDROM tray and measure the time therefor
* needed. In my experience the function needs less than 0.05
* seconds if the tray was already open, and at least 1.5 seconds
* if it was closed. */
gettimeofday(&time_start, NULL);
/* Send the CDROMEJECT command to the device. */
if (ioctl(fd, CDROMEJECT, 0) < 0) {
perror("ioctl");
exit(1);
}
/* Get the second timestamp, to measure the time needed to open
* the tray. */
gettimeofday(&time_stop, NULL);
time_elapsed = (time_stop.tv_sec * 1000000 + time_stop.tv_usec) -
(time_start.tv_sec * 1000000 + time_start.tv_usec);
/* If the tray "opened" too fast, we can be nearly sure, that it
* was already open. In this case, close it now. Else the tray was
* closed before. This would mean that we are done. */
if (time_elapsed < TRAY_WAS_ALREADY_OPEN_USECS)
CloseTray(fd);
#else
fprintf(stderr, _("%s: CD-ROM tray toggle command not supported by this kernel\n"), programName);
#endif
}
/*
* Select Speed of CD-ROM drive.
* Thanks to Roland Krivanek (krivanek@fmph.uniba.sk)
* http://dmpc.dbp.fmph.uniba.sk/~krivanek/cdrom_speed/
*/
static void SelectSpeedCdrom(int fd, int speed)
{
int status;
#ifdef CDROM_SELECT_SPEED
status = ioctl(fd, CDROM_SELECT_SPEED, speed);
if (status != 0) {
fprintf(stderr, _("%s: CD-ROM select speed command failed: %s\n"), programName, strerror(errno));
exit(1);
}
#else
fprintf(stderr, _("%s: CD-ROM select speed command not supported by this kernel\n"), programName);
#endif
}
/*
* Read Speed of CD-ROM drive. From Linux 2.6.13, the current speed is correctly reported
*/
static int ReadSpeedCdrom(const char *shortName)
{
char line[512];
char *str_speed, *str_name;
int drive_number = -1, i;
FILE *f = fopen("/proc/sys/dev/cdrom/info", "r");
if (f == NULL) {
fprintf(stderr, _("%s: unable to read the speed from /proc/sys/dev/cdrom/info\n"), programName);
exit(1);
}
while (!feof(f)) {
fgets(line, sizeof(line), f);
/* find drive number from shortName in line "drive name" */
if (drive_number == -1) {
if (strncmp(line, "drive name:", 11) == 0) {
str_name = strtok(&line[11], "\t ");
drive_number = 0;
while (strncmp(shortName, str_name, strlen(shortName)) != 0) {
drive_number++;
str_name = strtok(NULL, "\t ");
if (str_name == NULL) {
fprintf(stderr, _("%s: error while finding CD-ROM name\n"), programName);
exit(1);
}
}
}
/* find line "drive speed" and read the correct speed */
} else {
if (strncmp(line, "drive speed:", 12) == 0) {
str_speed = strtok(&line[12], "\t ");
for (i = 1; i < drive_number; i++)
str_speed = strtok(NULL, "\t ");
if (str_speed == NULL) {
fprintf(stderr, _("%s: error while reading speed\n"), programName);
exit(1);
}
return atoi(str_speed);
}
}
}
fprintf(stderr, _("%s: error while reading speed\n"), programName);
exit(1);
return -1;
}
/*
* List Speed of CD-ROM drive.
*/
static void ListSpeedCdrom(const char *fullName, int fd)
{
#ifdef CDROM_SELECT_SPEED
int max_speed, curr_speed = 0, prev_speed;
char *shortName = rindex(fullName, '/') + 1;
SelectSpeedCdrom(fd, 0);
max_speed = ReadSpeedCdrom(shortName);
while (curr_speed < max_speed) {
prev_speed = curr_speed;
SelectSpeedCdrom(fd, prev_speed + 1);
curr_speed = ReadSpeedCdrom(shortName);
if (curr_speed > prev_speed)
printf("%d ", curr_speed);
else
curr_speed = prev_speed + 1;
}
printf("\n");
#else
fprintf(stderr, _("%s: CD-ROM select speed command not supported by this kernel\n"), programName);
#endif
}
/*
* Eject using CDROMEJECT ioctl. Return 1 if successful, 0 otherwise.
*/
static int EjectCdrom(int fd)
{
int status = -1;
#if defined(CDROMEJECT)
status = ioctl(fd, CDROMEJECT);
#elif defined(CDIOCEJECT)
status = ioctl(fd, CDIOCEJECT);
#else
/* Some kernels implement cdrom-eject only, but I don't think any kernel in the
world would implement eject only for non-cdrom drives. Let's die. */
# error
#endif
return (status == 0);
}
#ifdef HAVE_EJECT_SCSI
/*
* Eject using SCSI SG_IO commands. Return 1 if successful, 0 otherwise.
*/
static int EjectScsi(int fd)
{
int status, k;
sg_io_hdr_t io_hdr;
unsigned char allowRmBlk[6] = {ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0};
unsigned char startStop1Blk[6] = {START_STOP, 0, 0, 0, 1, 0};
unsigned char startStop2Blk[6] = {START_STOP, 0, 0, 0, 2, 0};
unsigned char inqBuff[2];
unsigned char sense_buffer[32];
if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
printf("not an sg device, or old sg driver\n");
return 0;
}
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = 6;
io_hdr.mx_sb_len = sizeof(sense_buffer);
io_hdr.dxfer_direction = SG_DXFER_NONE;
io_hdr.dxfer_len = 0;
io_hdr.dxferp = inqBuff;
io_hdr.sbp = sense_buffer;
io_hdr.timeout = 2000;
io_hdr.cmdp = allowRmBlk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
return 0;
io_hdr.cmdp = startStop1Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
return 0;
io_hdr.cmdp = startStop2Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
return 0;
/* force kernel to reread partition table when new disc inserted */
status = ioctl(fd, BLKRRPART);
return 1;
}
#endif
#ifdef HAVE_EJECT_FLOPPY
/*
* Eject using FDEJECT ioctl. Return 1 if successful, 0 otherwise.
*/
static int EjectFloppy(int fd)
{
int status;
status = ioctl(fd, FDEJECT);
return (status >= 0);
}
#endif
#ifdef HAVE_EJECT_TAPE
/*
* Eject using tape ioctl. Return 1 if successful, 0 otherwise.
*/
static int EjectTape(int fd)
{
int status;
struct mtop op;
op.mt_op = MTOFFL; /* rewind and eject */
op.mt_count = 0; /* not used */
status = ioctl(fd, MTIOCTOP, &op);
return (status >= 0);
}
#endif
/* Unmount a device. */
static void Unmount(const char *fullName)
{
int status;
switch (fork()) {
case 0: /* child */
setuid(getuid()); /* reduce likelyhood of security holes when running setuid */
if(p_option) {
execlp("pumount", "pumount", fullName, "-n", NULL);
execlp("umount", "umount", fullName, "-n", NULL);
} else {
execlp("pumount", "pumount", fullName, NULL);
execlp("umount", "umount", fullName, NULL);
}
fprintf(stderr, _("%s: unable to exec umount of `%s': %s\n"),
programName, fullName, strerror(errno));
exit(1);
break;
case -1:
fprintf(stderr, _("%s: unable to fork: %s\n"), programName, strerror(errno));
break;
default: /* parent */
wait(&status);
if (WIFEXITED(status) == 0) {
fprintf(stderr, _("%s: unmount of `%s' did not exit normally\n"), programName, fullName);
exit(1);
}
if (WEXITSTATUS(status) != 0) {
fprintf(stderr, _("%s: unmount of `%s' failed\n"), programName, fullName);
exit(1);
}
break;
}
}
/* Open a device file. */
static int OpenDevice(const char *fullName)
{
int fd;
/* apparently the SG_IO ioctls need write access for
non-root users to work */
fd = open(fullName, O_RDWR|O_NONBLOCK);
if (fd == -1) {
fd = open(fullName, O_RDONLY|O_NONBLOCK);
if (fd == -1) {
fprintf(stderr, _("%s: unable to open `%s'\n"), programName, fullName);
exit(1);
}
}
return fd;
}
/*
* Get major and minor device numbers for a device file name, so we
* can check for duplicate devices.
*/
static int GetMajorMinor(const char *name, int *maj, int *min)
{
struct stat sstat;
if (maj) *maj = -1;
if (min) *min = -1;
if (stat(name, &sstat) == -1)
return -1;
if (! S_ISBLK(sstat.st_mode) && ! S_ISCHR(sstat.st_mode))
return -1;
if (maj) *maj = major(sstat.st_rdev);
if (min) *min = minor(sstat.st_rdev);
return 0;
}
/* next three functions copied from mntent.c from util-linux*/
#define isoctal(a) (((a) & ~7) == '0')
static int is_space_or_tab (char c) {
return (c == ' ' || c == '\t');
}
char* skip_nonspaces(char *s) {
while (*s && !is_space_or_tab(*s))
s++;
return s;
}
static char* unmangle(char *s) {
char *ret, *ss, *sp;
ss = skip_nonspaces(s);
ret = sp = malloc(ss-s+1);
/*FIXME: missing error checking*/
while(s != ss) {
if (*s == '\\' && isoctal(s[1]) && isoctal(s[2]) && isoctal(s[3])) {
*sp++ = 64*(s[1] & 7) + 8*(s[2] & 7) + (s[3] & 7);
s += 4;
} else
*sp++ = *s++;
}
*sp = 0;
return ret;
}
/*
* See if device has been mounted by looking in mount table. If so, set
* device name and mount point name, and return 1, otherwise return 0.
*/
static int MountedDevice(const char *name, char **mountName, char **deviceName)
{
FILE *fp;
char line[1024];
char s1[1024];
char s2[1024];
int rc;
int maj;
int min;
GetMajorMinor(name, &maj, &min);
fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab"), "r");
if (fp == NULL)
{
fprintf(stderr, _("unable to open %s: %s\n"), (p_option ? "/proc/mounts" : "/etc/mtab"), strerror(errno));
exit(1);
}
while (fgets(line, sizeof(line), fp) != 0) {
rc = sscanf(line, "%1023s %1023s", s1, s2);
if (rc >= 2) {
int mtabmaj, mtabmin;
GetMajorMinor(s1, &mtabmaj, &mtabmin);
*deviceName = unmangle(s1);
*mountName = unmangle(s2);
if (((strcmp((*deviceName), name) == 0) || (strcmp((*mountName), name) == 0)) ||
((maj != -1) && (maj == mtabmaj) && (min == mtabmin))) {
FCLOSE(fp);
return 1;
}
}
}
*deviceName = 0;
*mountName = 0;
FCLOSE(fp);
return 0;
}
/*
* See if device can be mounted by looking in /etc/fstab.
* If so, set device name and mount point name, and return 1,
* otherwise return 0.
*/
static int MountableDevice(const char *name, char **mountName, char **deviceName)
{
FILE *fp;
char line[1024];
char s1[1024];
char s2[1024];
char *tmpMountName, *tmpDeviceName;
int rc;
fp = fopen("/etc/fstab", "r");
if (fp == NULL) {
/*
* /etc/fstab may be unreadable in some situations due to passwords in the
* file.
*/
/* fprintf(stderr, _("%s: unable to open /etc/fstab: %s\n"), programName, strerror(errno));
exit(1);*/
if (v_option) {
printf( _("%s: unable to open /etc/fstab: %s\n"), programName, strerror(errno));
}
return -1;
}
while (fgets(line, sizeof(line), fp) != 0) {
rc = sscanf(line, "%1023s %1023s", s1, s2);
tmpDeviceName = unmangle(s1);
tmpMountName = unmangle(s2);
if (rc >= 2 && (tmpDeviceName)[0] != '#' && strcmp((tmpMountName), name) == 0) {
*deviceName = tmpDeviceName;
*mountName = tmpMountName;
FCLOSE(fp);
return 1;
}
}
FCLOSE(fp);
return 0;
}
/*
* Find device name with the given major and minor number. Returns NULL if not
* found. The returned pointer points to static area and must not be free()'d.
*/
char* FindDeviceMajorMinor(int major, int minor)
{
DIR* d = opendir("/dev");
struct dirent *entry;
struct stat st;
dev_t dev = makedev(major, minor);
static char path[270];
if (!d)
return NULL;
while ((entry = readdir(d)) != 0) {
snprintf(path, sizeof(path), "/dev/%s", entry->d_name);
if (stat(path, &st) != 0)
continue;
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
return path;
}
return NULL;
}
/*
* Check whether dev is an encrypted device handled by devmapper. If so, return
* the underlying physical device name (pointer to static area, do not free()).
* If not, or an error occurs, return NULL.
* This uses the /usr/lib/eject/dmcrypt-get-device helper.
*/
const char* dmcrypt_script = "/usr/lib/eject/dmcrypt-get-device";
const char* GetDevmapperDevice (const char* dev)
{
char cmd[1024];
char output[100];
int major, minor;
struct stat dummy;
const char* devmapper = "/dev/mapper/";
if (strncmp (dev, devmapper, strlen(devmapper)))
return NULL;
if (strchr (dev, '\''))
return NULL;
if ((stat(dmcrypt_script, &dummy) < 0)
&& (errno == ENOENT)) {
if (v_option)
printf(_("%s: %s doesn't exist, skipping call\n"),
programName, dmcrypt_script);
return NULL;
}
snprintf (cmd, sizeof(cmd), "%s '%s'", dmcrypt_script, dev);
FILE* f = popen (cmd, "r");
fread (output, 1, sizeof(output), f);
if (pclose(f))
return NULL;
if (sscanf(output, "%i:%i", &major, &minor) == 2)
return FindDeviceMajorMinor (major, minor);
else
return NULL;
}
/*
* Step through mount table and unmount all devices that match a regular
* expression.
*/
static void UnmountDevices(const char *pattern)
{
regex_t preg;
FILE *fp;
char s1[1024];
char s2[1024];
char *mountParam;
char line[1024];
int status;
if (regcomp(&preg, pattern, REG_EXTENDED)!=0) {
perror(programName);
exit(1);
}
fp = fopen((p_option ? "/proc/mounts" : "/etc/mtab"), "r");
if (fp == NULL)
{
fprintf(stderr, _("unable to open %s: %s\n"),(p_option ? "/proc/mounts" : "/etc/mtab"), strerror(errno));
exit(1);
}
while (fgets(line, sizeof(line), fp) != 0) {
status = sscanf(line, "%1023s %1023s", s1, s2);
if (status >= 2) {
/* Check if we have a mapped device */
const char* dev = GetDevmapperDevice (s1);
if (dev) {
if (v_option)
printf(_("%s: %s is encrypted on real device %s\n"),
programName, s1, dev);
} else
dev = s1;
status = regexec(&preg, dev, 0, 0, 0);
if (status == 0) {
mountParam = unmangle(s2);
if (v_option)
printf(_("%s: unmounting `%s'\n"), programName, mountParam);
Unmount(mountParam);
free(mountParam);
}
}
}
regfree(&preg);
FCLOSE(fp);
}
/* Check if name is a symbolic link. If so, return what it points to. */
static char *SymLink(const char *name)
{
int status;
char s1[PATH_MAX];
char s2[PATH_MAX];
char s4[PATH_MAX];
char result[PATH_MAX];
char *s3;
memset(s1, 0, sizeof(s1));
memset(s2, 0, sizeof(s2));
memset(s4, 0, sizeof(s4));
memset(result, 0, sizeof(result));
status = readlink(name, s1, sizeof(s1) - 1);
if (status == -1)
return 0;
s1[status] = 0;
if (s1[0] == '/') { /* absolute link */
return strdup(s1);
} else { /* relative link, add base name */
strncpy(s2, name, sizeof(s2)-1);
s3 = strrchr(s2, '/');
if (s3 != 0) {
s3[1] = 0;
snprintf(result, sizeof(result)-1, "%s%s", s2, s1);
}
}
realpath(result, s4);
return strdup(s4);
}
/*
* Given a name, see if it matches a pattern for a device that can have
* multiple partitions. If so, return a regular expression that matches
* partitions for that device, otherwise return 0.
*/
static char *MultiplePartitions(const char *name)
{
int i = 0;
int status;
regex_t preg;
char pattern[256];
char *result = 0;
/* Check if we have a mapped device */
const char* dev = GetDevmapperDevice (name);
if (dev) {
if (v_option)
printf(_("%s: %s is encrypted on real device %s\n"),
programName, name, dev);
name = dev;
}
for (i = 0; partitionDevice[i] != 0; i++) {
/* look for ^/dev/foo[a-z]([0-9]?[0-9])?$, e.g. /dev/hda1 */
strcpy(pattern, "^/dev/");
strcat(pattern, partitionDevice[i]);
strcat(pattern, "[a-z]([0-9]?[0-9])?$");
if (regcomp(&preg, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
perror(programName);
exit(1);
}
status = regexec(&preg, name, 1, 0, 0);
regfree(&preg);
if (status == 0) {
result = (char *) malloc(strlen(name) + 25);
if (result == NULL) {
fprintf(stderr, _("%s: could not allocate memory\n"), programName);
exit(1);
}
strcpy(result, name);
result[strlen(partitionDevice[i]) + 6] = 0;
strcat(result, "([0-9]?[0-9])?$");
if (v_option)
printf(_("%s: `%s' is a multipartition device\n"), programName, name);
return result;
}
}
if (v_option)
printf(_("%s: `%s' is not a multipartition device\n"), programName, name);
return 0;
}
/* handle -x option */
static void HandleXOption(char *deviceName)
{
int fd; /* file descriptor for device */
if (x_option) {
if (v_option)
{
if (x_arg == 0)
printf(_("%s: setting CD-ROM speed to auto\n"), programName);
else
printf(_("%s: setting CD-ROM speed to %dX\n"), programName, x_arg);
}
fd = OpenDevice(deviceName);
SelectSpeedCdrom(fd, x_arg);
exit(0);
}
}
/* main program */
int main(int argc, char **argv)
{
const char *defaultDevice = DEFAULTDEVICE; /* default if no name passed by user */
int worked = 0; /* set to 1 when successfully ejected */
char *device = 0; /* name passed from user */
char *fullName; /* expanded name */
char *fullNameOrig;/* expanded name (links not resolved) */
char *deviceName; /* name of device */
char *linkName; /* name of device's symbolic link */
char *mountName; /* name of device's mount point */
int fd; /* file descriptor for device */
int mounted = 0; /* true if device is mounted */
int mountable = 0; /* true if device is in /etc/fstab */
int result = 0; /* store the result of a operation */
char *pattern; /* regex for device if multiple partitions */
int ld = 6; /* symbolic link max depth */
I18NCODE
/* program name is global variable used by other procedures */
programName = strdup(argv[0]);
/* parse the command line arguments */
parse_args(argc, argv, &device);
/* handle -d option */
if (d_option) {
printf(_("%s: default device: `%s'\n"), programName, defaultDevice);
exit(0);
}
/* if no device, use default */
if (device == 0) {
device = strdup(defaultDevice);
if (v_option)
printf(_("%s: using default device `%s'\n"), programName, device);
}
/* Strip any trailing slash from name in case user used bash/tcsh
style filename completion (e.g. /mnt/cdrom/) */
if (device[strlen(device)-1] == '/')
device[strlen(device)-1] = 0;
if (v_option)
printf(_("%s: device name is `%s'\n"), programName, device);
do {
/* figure out full device or mount point name */
fullName = FindDevice(device);
if (fullName == 0) {
fprintf(stderr, _("%s: unable to find or open device for: `%s'\n"),
programName, device);
exit(1);
}
if (v_option)
printf(_("%s: expanded name is `%s'\n"), programName, fullName);
/* check for a symbolic link */
/* /proc/mounts doesn't resolve symbolic links */
fullNameOrig = strdup(fullName);
linkName = strdup(fullName); /* ensure linkName is initialized */
if (!p_option) {
while ((linkName = SymLink(fullName)) && (ld > 0)) {
if (v_option)
printf(_("%s: `%s' is a link to `%s'\n"), programName,
fullName, linkName);
free(fullName);
fullName = strdup(linkName);
free(linkName);
linkName = 0;
ld--;
}
}
/* handle max depth exceeded option */
if (ld <= 0) {
printf(_("%s: maximum symbolic link depth exceeded: `%s'\n"), programName, fullName);
exit(1);
}
/* if mount point, get device name */
mounted = MountedDevice(fullName, &mountName, &deviceName);
if (v_option) {
if (mounted)
printf(_("%s: `%s' is mounted at `%s'\n"), programName,
deviceName, mountName);
else
printf(_("%s: `%s' is not mounted\n"), programName, fullName);
}
if (!mounted) {
deviceName = strdup(fullName);
}
/* if not currently mounted, see if it is a possible mount point */
if (!mounted) {
mountable = MountableDevice(fullName, &mountName, &deviceName);
/* if return value -1 then fstab could not be read */
if (v_option && mountable >= 0) {
if (mountable)
printf(_("%s: `%s' can be mounted at `%s'\n"), programName, deviceName, mountName);
else
printf(_("%s: `%s' is not a mount point\n"), programName, fullName);
}
}
result = GetMajorMinor(deviceName, NULL, NULL);
if (result == -1) {
fprintf(stderr,
_("%s: tried to use `%s' as device name but it is no block device\n"),
programName, deviceName);
}
} while (result == -1);
/* handle -n option */
if (n_option) {
printf(_("%s: device is `%s'\n"), programName, deviceName);
if (v_option)
printf(_("%s: exiting due to -n/--noop option\n"), programName);
exit(0);
}
/* handle -a option */
if (a_option) {
if (v_option) {
if (a_arg)
printf(_("%s: enabling auto-eject mode for `%s'\n"), programName, deviceName);
else
printf(_("%s: disabling auto-eject mode for `%s'\n"), programName, deviceName);
}
fd = OpenDevice(deviceName);
AutoEject(fd, a_arg);
exit(0);
}
/* handle -t option */
if (t_option) {
if (v_option)
printf(_("%s: closing tray\n"), programName);
fd = OpenDevice(deviceName);
CloseTray(fd);
HandleXOption(deviceName);
exit(0);
}
/* handle -X option */
if (X_option) {
if (v_option)
printf(_("%s: listing CD-ROM speed\n"), programName);
fd = OpenDevice(deviceName);
ListSpeedCdrom(deviceName, fd);
exit(0);
}
/* handle -x option only */
if (!c_option) HandleXOption(deviceName);
/* unmount device if mounted */
if ((m_option != 1) && mounted) {
if (v_option)
printf(_("%s: unmounting device `%s' from `%s'\n"), programName, deviceName, mountName);
Unmount(mountName);
}
/* if it is a multipartition device, unmount any other partitions on
the device */
pattern = MultiplePartitions(deviceName);
if ((m_option != 1) && (pattern != 0))
UnmountDevices(pattern);
/* handle -T option */
if (T_option) {
if (v_option)
printf(_("%s: toggling tray\n"), programName);
fd = OpenDevice(deviceName);
ToggleTray(fd);
HandleXOption(deviceName);
exit(0);
}
/* handle -c option */
if (c_option) {
if (v_option)
printf(_("%s: selecting CD-ROM disc #%d\n"), programName, c_arg);
fd = OpenDevice(deviceName);
ChangerSelect(fd, c_arg);
HandleXOption(deviceName);
exit(0);
}
/* if user did not specify type of eject, try all four methods */
if ((r_option + s_option + f_option + q_option) == 0) {
r_option = s_option = f_option = q_option = 1;
}
/* open device */
fd = OpenDevice(deviceName);
/* try various methods of ejecting until it works */
if (r_option) {
if (v_option)
printf(_("%s: trying to eject `%s' using CD-ROM eject command\n"), programName, deviceName);
worked = EjectCdrom(fd);
if (v_option) {
if (worked)
printf(_("%s: CD-ROM eject command succeeded\n"), programName);
else
printf(_("%s: CD-ROM eject command failed\n"), programName);
}
}
#ifdef HAVE_EJECT_SCSI
if (s_option && !worked) {
if (v_option)
printf(_("%s: trying to eject `%s' using SCSI commands\n"), programName, deviceName);
worked = EjectScsi(fd);
if (v_option) {
if (worked)
printf(_("%s: SCSI eject succeeded\n"), programName);
else
printf(_("%s: SCSI eject failed\n"), programName);
}
}
#endif
#ifdef HAVE_EJECT_FLOPPY
if (f_option && !worked) {
if (v_option)
printf(_("%s: trying to eject `%s' using floppy eject command\n"), programName, deviceName);
worked = EjectFloppy(fd);
if (v_option) {
if (worked)
printf(_("%s: floppy eject command succeeded\n"), programName);
else
printf(_("%s: floppy eject command failed\n"), programName);
}
}
#endif
#ifdef HAVE_EJECT_TAPE
if (q_option && !worked) {
if (v_option)
printf(_("%s: trying to eject `%s' using tape offline command\n"), programName, deviceName);
worked = EjectTape(fd);
if (v_option) {
if (worked)
printf(_("%s: tape offline command succeeded\n"), programName);
else
printf(_("%s: tape offline command failed\n"), programName);
}
}
#endif
if (!worked) {
fprintf(stderr, _("%s: unable to eject, last error: %s\n"), programName, strerror(errno));
exit(1);
}
/* cleanup */
CLOSE(fd);
free(device);
free(deviceName);
free(fullName);
free(fullNameOrig);
free(linkName);
free(mountName);
free(pattern);
exit(0);
}
|