home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / fdformat.000 < prev    next >
Encoding:
Internet Message Format  |  1996-11-16  |  4.5 KB

  1. From samba!concert!gatech!darwin.sura.net!Sirius.dfn.de!chx400!bernina!almesber Mon Sep  7 17:16:12 EDT 1992
  2. Article: 9854 of comp.os.linux
  3. Newsgroups: comp.os.linux
  4. Path: samba!concert!gatech!darwin.sura.net!Sirius.dfn.de!chx400!bernina!almesber
  5. From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
  6. Subject: Re: suid for fdformat?
  7. Message-ID: <1992Sep6.130207.11486@bernina.ethz.ch>
  8. Sender: news@bernina.ethz.ch (USENET News System)
  9. Organization: Swiss Federal Institute of Technology (ETH), Zurich, CH
  10. References: <1992Aug30.062444.346@athena.mit.edu>
  11. Date: Sun, 6 Sep 1992 13:02:07 GMT
  12. Lines: 136
  13.  
  14. In article <1992Aug30.062444.346@athena.mit.edu> hammond@kwhpc.caseng.com (Kevin W. Hammond) writes:
  15. > Should fdformat be installed as a suid program so regular users can format
  16. > floppies, or is it more typically found installed so only root can format
  17. > floppies?
  18.  
  19. It depends. Formatting is inherently more timing-critical than regular
  20. floppy accesses because some errors can only be detected in the
  21. verification pass. Therefore, it's considered an operation that needs
  22. some awareness on the user's part.
  23.  
  24. If you can live with users creating bad media on a loaded system,
  25. running fdformat suid isn't a problem. However, you should use the
  26. slightly modified version I've appended to this posting. It assures
  27. that only floppy devices can be formatted (the ioctls may have some
  28. random effect on other devices) and only if the user has write access
  29. to them.
  30.  
  31. - Werner
  32.  
  33. ---------------------------------- cut here -----------------------------------
  34.  
  35. /* fdformat.c  -  Low-level formats a floppy disk. */
  36.  
  37. #include <unistd.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <fcntl.h>
  41. #include <errno.h>
  42. #include <sys/stat.h>
  43. #include <linux/fd.h>
  44.  
  45.  
  46. static int ctrl;
  47. struct floppy_struct param;
  48.  
  49.  
  50. #define FLOPPY_MAJOR 2
  51. #define SECTOR_SIZE 512
  52. #define PERROR(msg) { perror(msg); exit(1); }
  53.  
  54.  
  55. static void format_disk(char *name)
  56. {
  57.     struct format_descr descr;
  58.     int track;
  59.     char dummy;
  60.  
  61.     printf("Formatting ... ");
  62.     fflush(stdout);
  63.     if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)");
  64.     for (track = 0; track < param.track; track++) {
  65.     descr.track = track;
  66.     descr.head = 0;
  67.     if (ioctl(ctrl,FDFMTTRK,(int) &descr) < 0) PERROR("\nioctl(FDFMTTRK)");
  68.     printf("%3d\b\b\b",track);
  69.     fflush(stdout);
  70.     if (param.head == 2) {
  71.         descr.head = 1;
  72.         if (ioctl(ctrl,FDFMTTRK,(int) &descr) < 0)
  73.         PERROR("\nioctl(FDFMTTRK)");
  74.     }
  75.     }
  76.     if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)");
  77.     printf("done\n");
  78. }
  79.  
  80.  
  81. static void verify_disk(char *name)
  82. {
  83.     unsigned char *data;
  84.     int fd,cyl_size,cyl,count;
  85.  
  86.     cyl_size = param.sect*param.head*512;
  87.     if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc");
  88.     printf("Verifying ... ");
  89.     fflush(stdout);
  90.     if ((fd = open(name,O_RDONLY)) < 0) PERROR(name);
  91.     for (cyl = 0; cyl < param.track; cyl++) {
  92.     printf("%3d\b\b\b",cyl);
  93.     fflush(stdout);
  94.     if (read(fd,data,cyl_size) != cyl_size) PERROR("read");
  95.     for (count = 0; count < cyl_size; count++)
  96.         if (data[count] != FD_FILL_BYTE) {
  97.         printf("bad data in cyl %d\nContinuing ... ",cyl);
  98.         fflush(stdout);
  99.         break;
  100.         }
  101.     }
  102.     printf("done\n");
  103.     if (close(fd) < 0) PERROR("close");
  104. }
  105.  
  106.  
  107. static void usage(char *name)
  108. {
  109.     char *this;
  110.  
  111.     if (this = strrchr(name,'/')) name = this+1;
  112.     fprintf(stderr,"usage: %s [ -n ] device\n",name);
  113.     exit(1);
  114. }
  115.  
  116.  
  117. main(int argc,char **argv)
  118. {
  119.     int verify;
  120.     char *name;
  121.     struct stat st;
  122.  
  123.     name = argv[0];
  124.     verify = 1;
  125.     if (argc > 1 && argv[1][0] == '-') {
  126.     if (argv[1][1] != 'n') usage(name);
  127.     verify = 0;
  128.     argc--;
  129.     argv++;
  130.     }
  131.     if (argc != 2) usage(name);
  132.     if (lstat(argv[1],&st) < 0) PERROR(argv[1]);
  133.     if (!S_ISBLK(st.st_mode) || st.st_rdev >> 8 != FLOPPY_MAJOR) {
  134.     fprintf(stderr,"%s: not a floppy device\n",argv[1]);
  135.     exit(1);
  136.     }
  137.     if (access(argv[1],W_OK) < 0) PERROR(argv[1]);
  138.     if ((ctrl = open(argv[1],3)) < 0) PERROR(argv[1]);
  139.     if (ioctl(ctrl,FDGETPRM,(int) ¶m) < 0) PERROR("ioctl(FDGETPRM)");
  140.     printf("%sle-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
  141.       param.head ? "Doub" : "Sing",param.track,param.sect,param.size >> 1);
  142.     format_disk(argv[1]);
  143.     if (verify) verify_disk(argv[1]);
  144. }
  145. -- 
  146.    _________________________________________________________________________
  147.   / Werner Almesberger, ETH Zuerich, CH      almesber@nessie.cs.id.ethz.ch /
  148.  / IFW A44  Tel. +41 1 254 7213                 almesberger@rzvax.ethz.ch /
  149. /_BITNET:_ALMESBER@CZHETH5A__HEPNET/CHADNET:_[20579::]57414::ALMESBERGER_/
  150.  
  151.  
  152.