手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux ioctl函数详细说明

[日期:2012-07-31] 来源:Linux社区  作者:yiluohuanghun [字体:大 中 小]

四、 cmd参数如何得出

这里确实要说一说,cmd参数在用户程序端由一些宏根据设备类型、序列号、传送方向、数据尺寸等生成,这个整数通过系统调用传递到内核中的驱动程序,再由驱动程序使用解码宏从这个整数中得到设备的类型、序列号、传送方向、数据尺寸等信息,然后通过switch{case}结构进行相应的操作。要透彻理解,只能是通过阅读源代码,我这篇文章实际上只是一个引子。cmd参数的组织还是比较复杂的,我认为要搞熟它还是得花不少时间的,但是这是值得的,因为驱动程序中最难的是对中断的理解。

五、 小结

ioctl其实没有什么很难的东西需要理解,关键是理解cmd命令码是怎么在用户程序里生成并在驱动程序里解析的,程序员最主要的工作量在switch{case}结构中,因为对设备的I/O控制都是通过这一部分的代码实现的。

下面以几个示例代码来说明问题:

程序1:检测接口的inet_addr, netmask, broad_addr

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <errno.h>  
  5. #include <unistd.h>  
  6. #include <sys/types.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. #include <arpa/inet.h>  
  10. #include <sys/ioctl.h>  
  11. #include <net/if.h>  
  12.  
  13. static void usage()  
  14. {  
  15.    printf("usage : ipconfig interface \n");  
  16.    exit(0);  
  17. }  
  18. int main(int argc,char **argv)  
  19. {  
  20.    struct sockaddr_in *addr;  
  21.    struct ifreq ifr;  
  22.    char *name,*address;  
  23.    int sockfd;  
  24.  
  25.    if(argc != 2)    
  26.          usage();  
  27.    else    
  28.         name = argv[1];  
  29.  
  30.    sockfd = socket(AF_INET,SOCK_DGRAM,0);  
  31.    strncpy(ifr.ifr_name,name,IFNAMSIZ-1);  
  32.  
  33.    if(ioctl(sockfd,SIOCGIFADDR,&ifr) == -1)  
  34.          perror("ioctl error");  
  35.     exit(1);  
  36.  
  37.    addr = (struct sockaddr_in *)&(ifr.ifr_addr);  
  38.    address = inet_ntoa(addr->sin_addr);  
  39.    printf("inet addr: %s ",address);  
  40.  
  41.    if(ioctl(sockfd,SIOCGIFBRDADDR,&ifr) == -1)  
  42.       perror("ioctl error"),exit(1);  
  43.  
  44.    addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;  
  45.    address = inet_ntoa(addr->sin_addr);  
  46.    printf("broad addr: %s ",address);  
  47.  
  48.    if(ioctl(sockfd,SIOCGIFNETMASK,&ifr) == -1)  
  49.       perror("ioctl error"),exit(1);  
  50.    addr = (struct sockaddr_in *)&ifr.ifr_addr;  
  51.    address = inet_ntoa(addr->sin_addr);  
  52.    printf("inet mask: %s ",address);  
  53.  
  54.    printf("\n");  
  55.    exit(0);  
  56. }

程序2:检查接口的物理连接是否正常

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <errno.h>  
  4. #include <fcntl.h>  
  5. #include <getopt.h>  
  6. #include <sys/socket.h>  
  7. #include <sys/ioctl.h>  
  8. #include <net/if.h>  
  9. #include <stdlib.h>  
  10. #include <unistd.h>  
  11. typedef unsigned short u16;  
  12. typedef unsigned int u32;  
  13. typedef unsigned char u8;  
  14. #include <linux/ethtool.h>  
  15. #include <linux/sockios.h>  
  16.  
  17. int detect_mii(int skfd, char *ifname)  
  18. {  
  19.    struct ifreq ifr;  
  20.    u16 *data, mii_val;  
  21.    unsigned phy_id;  
  22.  
  23.    /* Get the vitals from the interface. */ 
  24.    strncpy(ifr.ifr_name, ifname, IFNAMSIZ);  
  25.  
  26.    if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0)  
  27.       {  
  28.          fprintf(stderr, "SIOCGMIIPHY on %s failed: %s\n", ifname, strerror(errno));  
  29.          (void) close(skfd);  
  30.          return 2;  
  31.       }  
  32.  
  33.    data = (u16 *)(&ifr.ifr_data);  
  34.    phy_id = data[0];  
  35.    data[1] = 1;  
  36.  
  37.    if (ioctl(skfd, SIOCGMIIREG, &ifr) < 0)  
  38.      {  
  39.         fprintf(stderr, "SIOCGMIIREG on %s failed: %s\n", ifr.ifr_name, strerror(errno));  
  40.         return 2;  
  41.      }  
  42.  
  43.    mii_val = data[3];  
  44.    return(((mii_val & 0x0016) == 0x0004) ? 0 : 1);  
  45. }  
  46.  
  47. int detect_ethtool(int skfd, char *ifname)  
  48. {  
  49.    struct ifreq ifr;  
  50.    struct ethtool_value edata;  
  51.    memset(&ifr, 0, sizeof(ifr));  
  52.    edata.cmd = ETHTOOL_GLINK;  
  53.  
  54.    strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)-1);  
  55.    ifr.ifr_data = (char *) &edata;  
  56.  
  57.    if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1)  
  58.      {  
  59.         printf("ETHTOOL_GLINK failed: %s\n", strerror(errno));  
  60.         return 2;  
  61.      }  
  62.  
  63.    return (edata.data ? 0 : 1);  
  64. }  
  65.  
  66. int main(int argc, char **argv)  
  67. {  
  68.    int skfd = -1;  
  69.    char *ifname;  
  70.    int retval;  
  71.  
  72.    if( argv[1] )  ifname = argv[1];  
  73.      else  ifname = "eth0";  
  74.  
  75.    /* Open a socket. */ 
  76.    if (( skfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )  
  77.       {  
  78.          printf("socket error\n");  
  79.          exit(-1);  
  80.       }  
  81.  
  82.    retval = detect_ethtool(skfd, ifname);  
  83.    if (retval == 2)  
  84.      retval = detect_mii(skfd, ifname);  
  85.  
  86.    close(skfd);  
  87.    
  88.    if (retval == 2)  
  89.      printf("Could not determine status\n");  
  90.    if (retval == 1)  
  91.      printf("Link down\n");  
  92.    if (retval == 0)  
  93.      printf("Link up\n");  
  94.  
  95.    return retval;  
  96. }  

程序3:测试物理连接

  1. *******************************程序3*****************************************************  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <errno.h>  
  6. #include <net/if.h>  
  7. #include <linux/sockios.h>  
  8. #include <sys/ioctl.h>  
  9.  
  10. #define LINKTEST_GLINK 0x0000000a  
  11.  
  12. struct linktest_value {  
  13.         unsigned int    cmd;  
  14.         unsigned int    data;  
  15. };  
  16.  
  17. static void usage(const char * pname)  
  18. {  
  19.    fprintf(stderr, "usage: %s <device>\n", pname);  
  20.    fprintf(stderr, "returns: \n");  
  21.    fprintf(stderr, "\t 0: link detected\n");  
  22.    fprintf(stderr, "\t%d: %s\n", ENODEV, strerror(ENODEV));  
  23.    fprintf(stderr, "\t%d: %s\n", ENONET, strerror(ENONET));  
  24.    fprintf(stderr, "\t%d: %s\n", EOPNOTSUPP, strerror(EOPNOTSUPP));  
  25.    exit(EXIT_FAILURE);  
  26. }  
  27. static int linktest(const char * devname)  
  28. {  
  29.    struct ifreq ifr;  
  30.    struct linktest_value edata;  
  31.    int fd;  
  32.    /* setup our control structures. */ 
  33.    memset(&ifr, 0, sizeof(ifr));  
  34.    strcpy(ifr.ifr_name, devname);  
  35.    /* open control socket. */ 
  36.    fd=socket(AF_INET, SOCK_DGRAM, 0);  
  37.    if(fd < 0 )   
  38.      return -ECOMM;  
  39.  
  40.    errno = 0;  
  41.    edata.cmd = LINKTEST_GLINK;  
  42.    ifr.ifr_data = (caddr_t)&edata;  
  43.  
  44.    if(!ioctl(fd, SIOCETHTOOL, &ifr))   
  45.       {  
  46.         if(edata.data)   
  47.         {  
  48.             fprintf(stdout, "link detected on %s\n", devname);  
  49.             return 0;  
  50.         }   
  51.         else   
  52.         {  
  53.                errno=ENONET;  
  54.         }  
  55.      }  
  56.    perror("linktest");  
  57.    return errno;  
  58. }  
  59. int main(int argc, char *argv[])  
  60. {  
  61.    if(argc != 2)   
  62.          usage(argv[0]);  
  63.    return linktest(argv[1]);  
  64. } 

程序4:调节音量

  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <fcntl.h>  
  4. #include <sys/ioctl.h>  
  5. #include <sys/soundcard.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <math.h>  
  9. #include <string.h>  
  10. #include <stdlib.h>  
  11. #define  BASE_VALUE 257  
  12.  
  13. int main(int argc,char *argv[])  
  14. {  
  15.    int mixer_fd=0;  
  16.    char *names[SOUND_MIXER_NRDEVICES]=SOUND_DEVICE_LABELS;  
  17.    int value,i;  
  18.  
  19.    printf("\nusage:%s dev_no.[0..24] value[0..100]\n\n",argv[0]);  
  20.    printf("eg. %s 0 100\n",argv[0]);  
  21.    printf("will change the volume to MAX volume.\n\n");  
  22.    printf("The dev_no. are as below:\n");  
  23.  
  24.    for (i=0;i<SOUND_MIXER_NRDEVICES;i++)  
  25.      {  
  26.         if (i%3==0) printf("\n");  
  27.         printf("%s:%d\t\t",names[i],i);  
  28.      }  
  29.  
  30.    printf("\n\n");  
  31.  
  32.    if (argc<3)  exit(1);  
  33.  
  34.    if ((mixer_fd = open("/dev/mixer",O_RDWR)))  
  35.      {  
  36.          printf("Mixer opened successfully,working...\n");  
  37.          value=BASE_VALUE*atoi(argv[2]);  
  38.  
  39.          if (ioctl(mixer_fd,MIXER_WRITE(atoi(argv[1])),&value)==0)  
  40.             printf("successfully.....");  
  41.          else 
  42.             printf("unsuccessfully.....");  
  43.          
  44.          printf("done.\n");  
  45.      }  
  46.     else 
  47.       printf("can't open /dev/mixer error....\n");  
  48.      exit(0);  
  49. } 
linux
相关资讯       ioctl  Linux函数  ioctl函数 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款