diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-27 10:23:34 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-27 10:23:34 +0000 |
commit | 892536f019f72925513aa4d1decfe530bb65bbdc (patch) | |
tree | d8b4c8202c81bc7e9bda8be83da94b8c1b8b7d4f | |
parent | 4daad9004d8f07991516970a1cbd77756fae7041 (diff) | |
download | busybox-w32-892536f019f72925513aa4d1decfe530bb65bbdc.tar.gz busybox-w32-892536f019f72925513aa4d1decfe530bb65bbdc.tar.bz2 busybox-w32-892536f019f72925513aa4d1decfe530bb65bbdc.zip |
hdparm: use fixed fd #3 for device being interrogated
(surprisingly, on i386 almost no code size change)
-rw-r--r-- | miscutils/hdparm.c | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c index 396328f72..39e4990f5 100644 --- a/miscutils/hdparm.c +++ b/miscutils/hdparm.c | |||
@@ -441,25 +441,28 @@ static const char *const secu_str[] = { | |||
441 | 441 | ||
442 | #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */ | 442 | #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */ |
443 | 443 | ||
444 | |||
445 | enum { fd = 3 }; | ||
446 | |||
444 | /* Busybox messages and functions */ | 447 | /* Busybox messages and functions */ |
445 | #if ENABLE_IOCTL_HEX2STR_ERROR | 448 | #if ENABLE_IOCTL_HEX2STR_ERROR |
446 | static int ioctl_alt_func(int fd, int cmd, unsigned char *args, int alt, const char *string) | 449 | static int ioctl_alt_func(/*int fd,*/ int cmd, unsigned char *args, int alt, const char *string) |
447 | { | 450 | { |
448 | if (!ioctl(fd, cmd, args)) | 451 | if (!ioctl(fd, cmd, args)) |
449 | return 0; | 452 | return 0; |
450 | args[0] = alt; | 453 | args[0] = alt; |
451 | return bb_ioctl_or_warn(fd, cmd, args, string); | 454 | return bb_ioctl_or_warn(fd, cmd, args, string); |
452 | } | 455 | } |
453 | #define ioctl_alt_or_warn(fd,cmd,args,alt) ioctl_alt_func(fd,cmd,args,alt,#cmd) | 456 | #define ioctl_alt_or_warn(cmd,args,alt) ioctl_alt_func(cmd,args,alt,#cmd) |
454 | #else | 457 | #else |
455 | static int ioctl_alt_func(int fd, int cmd, unsigned char *args, int alt) | 458 | static int ioctl_alt_func(/*int fd,*/ int cmd, unsigned char *args, int alt) |
456 | { | 459 | { |
457 | if (!ioctl(fd, cmd, args)) | 460 | if (!ioctl(fd, cmd, args)) |
458 | return 0; | 461 | return 0; |
459 | args[0] = alt; | 462 | args[0] = alt; |
460 | return bb_ioctl_or_warn(fd, cmd, args); | 463 | return bb_ioctl_or_warn(fd, cmd, args); |
461 | } | 464 | } |
462 | #define ioctl_alt_or_warn(fd,cmd,args,alt) ioctl_alt_func(fd,cmd,args,alt) | 465 | #define ioctl_alt_or_warn(cmd,args,alt) ioctl_alt_func(cmd,args,alt) |
463 | #endif | 466 | #endif |
464 | 467 | ||
465 | static void on_off(int value) | 468 | static void on_off(int value) |
@@ -1233,7 +1236,7 @@ static void dump_identity(const struct hd_driveid *id) | |||
1233 | } | 1236 | } |
1234 | #endif | 1237 | #endif |
1235 | 1238 | ||
1236 | static void flush_buffer_cache(int fd) | 1239 | static void flush_buffer_cache(/*int fd*/ void) |
1237 | { | 1240 | { |
1238 | fsync(fd); /* flush buffers */ | 1241 | fsync(fd); /* flush buffers */ |
1239 | ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */ | 1242 | ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */ |
@@ -1248,14 +1251,14 @@ static void flush_buffer_cache(int fd) | |||
1248 | #endif | 1251 | #endif |
1249 | } | 1252 | } |
1250 | 1253 | ||
1251 | static int seek_to_zero(int fd) | 1254 | static int seek_to_zero(/*int fd*/ void) |
1252 | { | 1255 | { |
1253 | if (lseek(fd, (off_t) 0, SEEK_SET)) | 1256 | if (lseek(fd, (off_t) 0, SEEK_SET)) |
1254 | return 1; | 1257 | return 1; |
1255 | return 0; | 1258 | return 0; |
1256 | } | 1259 | } |
1257 | 1260 | ||
1258 | static int read_big_block(int fd, char *buf) | 1261 | static int read_big_block(/*int fd,*/ char *buf) |
1259 | { | 1262 | { |
1260 | int i; | 1263 | int i; |
1261 | 1264 | ||
@@ -1270,7 +1273,7 @@ static int read_big_block(int fd, char *buf) | |||
1270 | return 0; | 1273 | return 0; |
1271 | } | 1274 | } |
1272 | 1275 | ||
1273 | static int do_blkgetsize(int fd, unsigned long long *blksize64) | 1276 | static int do_blkgetsize(/*int fd,*/ unsigned long long *blksize64) |
1274 | { | 1277 | { |
1275 | int rc; | 1278 | int rc; |
1276 | unsigned blksize32 = 0; | 1279 | unsigned blksize32 = 0; |
@@ -1292,7 +1295,7 @@ static void print_timing(unsigned t, double e) | |||
1292 | printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e * 1024, 'k'); | 1295 | printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e * 1024, 'k'); |
1293 | } | 1296 | } |
1294 | 1297 | ||
1295 | static void do_time(int flag, int fd) | 1298 | static void do_time(int flag /*,int fd*/) |
1296 | /* flag = 0 time_cache, 1 time_device */ | 1299 | /* flag = 0 time_cache, 1 time_device */ |
1297 | { | 1300 | { |
1298 | static const struct itimerval thousand = {{1000, 0}, {1000, 0}}; | 1301 | static const struct itimerval thousand = {{1000, 0}, {1000, 0}}; |
@@ -1309,7 +1312,7 @@ static void do_time(int flag, int fd) | |||
1309 | } | 1312 | } |
1310 | 1313 | ||
1311 | max_iterations = 1024; | 1314 | max_iterations = 1024; |
1312 | if (0 == do_blkgetsize(fd, &blksize)) { | 1315 | if (0 == do_blkgetsize(&blksize)) { |
1313 | max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB; | 1316 | max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB; |
1314 | } | 1317 | } |
1315 | 1318 | ||
@@ -1317,9 +1320,9 @@ static void do_time(int flag, int fd) | |||
1317 | sync(); | 1320 | sync(); |
1318 | sleep(2); | 1321 | sleep(2); |
1319 | if (flag == 0) { /* Time cache */ | 1322 | if (flag == 0) { /* Time cache */ |
1320 | if (seek_to_zero(fd)) | 1323 | if (seek_to_zero()) |
1321 | goto quit; | 1324 | goto quit; |
1322 | if (read_big_block(fd, buf)) | 1325 | if (read_big_block(buf)) |
1323 | goto quit; | 1326 | goto quit; |
1324 | printf(" Timing buffer-cache reads: "); | 1327 | printf(" Timing buffer-cache reads: "); |
1325 | } else { /* Time device */ | 1328 | } else { /* Time device */ |
@@ -1335,9 +1338,9 @@ static void do_time(int flag, int fd) | |||
1335 | /* Now do the timing */ | 1338 | /* Now do the timing */ |
1336 | do { | 1339 | do { |
1337 | ++iterations; | 1340 | ++iterations; |
1338 | if ((flag == 0) && seek_to_zero(fd)) | 1341 | if ((flag == 0) && seek_to_zero()) |
1339 | goto quit; | 1342 | goto quit; |
1340 | if (read_big_block(fd, buf)) | 1343 | if (read_big_block(buf)) |
1341 | goto quit; | 1344 | goto quit; |
1342 | getitimer(ITIMER_REAL, &itv); | 1345 | getitimer(ITIMER_REAL, &itv); |
1343 | elapsed = (1000 - itv.it_value.tv_sec) * 1000000 | 1346 | elapsed = (1000 - itv.it_value.tv_sec) * 1000000 |
@@ -1348,7 +1351,7 @@ static void do_time(int flag, int fd) | |||
1348 | /* Now remove the lseek() and getitimer() overheads from the elapsed time */ | 1351 | /* Now remove the lseek() and getitimer() overheads from the elapsed time */ |
1349 | setitimer(ITIMER_REAL, &thousand, NULL); | 1352 | setitimer(ITIMER_REAL, &thousand, NULL); |
1350 | do { | 1353 | do { |
1351 | if (seek_to_zero(fd)) | 1354 | if (seek_to_zero()) |
1352 | goto quit; | 1355 | goto quit; |
1353 | getitimer(ITIMER_REAL, &itv); | 1356 | getitimer(ITIMER_REAL, &itv); |
1354 | elapsed2 = (1000 - itv.it_value.tv_sec) * 1000000 | 1357 | elapsed2 = (1000 - itv.it_value.tv_sec) * 1000000 |
@@ -1356,7 +1359,7 @@ static void do_time(int flag, int fd) | |||
1356 | } while (--iterations); | 1359 | } while (--iterations); |
1357 | elapsed -= elapsed2; | 1360 | elapsed -= elapsed2; |
1358 | total_MB *= BUFCACHE_FACTOR; | 1361 | total_MB *= BUFCACHE_FACTOR; |
1359 | flush_buffer_cache(fd); | 1362 | flush_buffer_cache(); |
1360 | } | 1363 | } |
1361 | print_timing(total_MB, elapsed / 1000000.0); | 1364 | print_timing(total_MB, elapsed / 1000000.0); |
1362 | quit: | 1365 | quit: |
@@ -1465,7 +1468,7 @@ static void print_flag(int flag, const char *s, unsigned long value) | |||
1465 | 1468 | ||
1466 | static void process_dev(char *devname) | 1469 | static void process_dev(char *devname) |
1467 | { | 1470 | { |
1468 | int fd; | 1471 | /*int fd;*/ |
1469 | long parm, multcount; | 1472 | long parm, multcount; |
1470 | #ifndef HDIO_DRIVE_CMD | 1473 | #ifndef HDIO_DRIVE_CMD |
1471 | int force_operation = 0; | 1474 | int force_operation = 0; |
@@ -1475,7 +1478,8 @@ static void process_dev(char *devname) | |||
1475 | unsigned char args[4] = { WIN_SETFEATURES, 0, 0, 0 }; | 1478 | unsigned char args[4] = { WIN_SETFEATURES, 0, 0, 0 }; |
1476 | const char *fmt = " %s\t= %2ld"; | 1479 | const char *fmt = " %s\t= %2ld"; |
1477 | 1480 | ||
1478 | fd = xopen(devname, O_RDONLY|O_NONBLOCK); | 1481 | /*fd = xopen(devname, O_RDONLY | O_NONBLOCK);*/ |
1482 | xmove_fd(xopen(devname, O_RDONLY | O_NONBLOCK), fd); | ||
1479 | printf("\n%s:\n", devname); | 1483 | printf("\n%s:\n", devname); |
1480 | 1484 | ||
1481 | if (set_readahead) { | 1485 | if (set_readahead) { |
@@ -1633,7 +1637,7 @@ static void process_dev(char *devname) | |||
1633 | #endif | 1637 | #endif |
1634 | if (get_standbynow) printf(" issuing standby command\n"); | 1638 | if (get_standbynow) printf(" issuing standby command\n"); |
1635 | args[0] = WIN_STANDBYNOW1; | 1639 | args[0] = WIN_STANDBYNOW1; |
1636 | ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2); | 1640 | ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2); |
1637 | } | 1641 | } |
1638 | if (set_sleepnow) { | 1642 | if (set_sleepnow) { |
1639 | #ifndef WIN_SLEEPNOW1 | 1643 | #ifndef WIN_SLEEPNOW1 |
@@ -1644,7 +1648,7 @@ static void process_dev(char *devname) | |||
1644 | #endif | 1648 | #endif |
1645 | if (get_sleepnow) printf(" issuing sleep command\n"); | 1649 | if (get_sleepnow) printf(" issuing sleep command\n"); |
1646 | args[0] = WIN_SLEEPNOW1; | 1650 | args[0] = WIN_SLEEPNOW1; |
1647 | ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2); | 1651 | ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2); |
1648 | } | 1652 | } |
1649 | if (set_seagate) { | 1653 | if (set_seagate) { |
1650 | args[0] = 0xfb; | 1654 | args[0] = 0xfb; |
@@ -1664,7 +1668,7 @@ static void process_dev(char *devname) | |||
1664 | #else /* HDIO_DRIVE_CMD */ | 1668 | #else /* HDIO_DRIVE_CMD */ |
1665 | if (force_operation) { | 1669 | if (force_operation) { |
1666 | char buf[512]; | 1670 | char buf[512]; |
1667 | flush_buffer_cache(fd); | 1671 | flush_buffer_cache(); |
1668 | if (-1 == read(fd, buf, sizeof(buf))) | 1672 | if (-1 == read(fd, buf, sizeof(buf))) |
1669 | bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1); | 1673 | bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1); |
1670 | } | 1674 | } |
@@ -1757,7 +1761,7 @@ static void process_dev(char *devname) | |||
1757 | const char *state; | 1761 | const char *state; |
1758 | 1762 | ||
1759 | args[0] = WIN_CHECKPOWERMODE1; | 1763 | args[0] = WIN_CHECKPOWERMODE1; |
1760 | if (ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2)) { | 1764 | if (ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2)) { |
1761 | if (errno != EIO || args[0] != 0 || args[1] != 0) | 1765 | if (errno != EIO || args[0] != 0 || args[1] != 0) |
1762 | state = "unknown"; | 1766 | state = "unknown"; |
1763 | else | 1767 | else |
@@ -1806,7 +1810,7 @@ static void process_dev(char *devname) | |||
1806 | memset(args1, 0, sizeof(args1)); | 1810 | memset(args1, 0, sizeof(args1)); |
1807 | args1[0] = WIN_IDENTIFY; | 1811 | args1[0] = WIN_IDENTIFY; |
1808 | args1[3] = 1; | 1812 | args1[3] = 1; |
1809 | if (!ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY)) | 1813 | if (!ioctl_alt_or_warn(HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY)) |
1810 | identify((void *)(args1 + 4)); | 1814 | identify((void *)(args1 + 4)); |
1811 | } | 1815 | } |
1812 | #endif | 1816 | #endif |
@@ -1829,11 +1833,11 @@ static void process_dev(char *devname) | |||
1829 | ioctl_or_warn(fd, BLKRRPART, NULL); | 1833 | ioctl_or_warn(fd, BLKRRPART, NULL); |
1830 | 1834 | ||
1831 | if (do_ctimings) | 1835 | if (do_ctimings) |
1832 | do_time(0, fd); /* time cache */ | 1836 | do_time(0 /*,fd*/); /* time cache */ |
1833 | if (do_timings) | 1837 | if (do_timings) |
1834 | do_time(1, fd); /* time device */ | 1838 | do_time(1 /*,fd*/); /* time device */ |
1835 | if (do_flush) | 1839 | if (do_flush) |
1836 | flush_buffer_cache(fd); | 1840 | flush_buffer_cache(); |
1837 | close(fd); | 1841 | close(fd); |
1838 | } | 1842 | } |
1839 | 1843 | ||