linux系統ioctl使用示例

2021-06-03 19:52:55 字數 2636 閱讀 7204

linux系統ioctl使用示例

these were writed and collected by kf701,

you can use and modify them but no warranty.

contact with me : [email protected]

程式1:檢測介面的 inet_addr,netmask,broad_addr

程式2:檢查介面的物理連線是否正常

程式3:更簡單一點測試物理連線

程式4:調節音量

注:在網上搜尋了部分例子,發現在建立socket之後,完成操作之後都不呼叫close函式,個人嘗試了下,linux下反覆呼叫socket不關閉會導致資源消耗殆盡。

程式1:

#include #include #include #include #include #include #include #include #include #include #include static void usage()

int main(int argc,char **argv)

程式2:

#include #include #include #include #include #include #include #include #include #include typedef unsigned short u16;

typedef unsigned int u32;

typedef unsigned char u8;

#include #include int detect_mii(int skfd, char *ifname)

data = (u16 *)(&ifr.ifr_data);

phy_id = data[0];

data[1] = 1;

if (ioctl(skfd, siocgmiireg, &ifr) < 0)

mii_val = data[3];

return(((mii_val & 0x0016) == 0x0004) ? 0 : 1);

}int detect_ethtool(int skfd, char *ifname)

return (edata.data ? 0 : 1);

}int main(int argc, char **argv)

retval = detect_ethtool(skfd, ifname);

if (retval == 2)

retval = detect_mii(skfd, ifname);

close(skfd);

if (retval == 2)

printf("could not determine status\n");

if (retval == 1)

printf("link down\n");

if (retval == 0)

printf("link up\n");

return retval;

}

程式3:

#include #include #include #include #include #include #include #define linktest_glink 0x0000000a

struct linktest_value ;

static

void

usage(const char * pname)

static

intlinktest(const char * devname)

errno=0;

edata.cmd = linktest_glink;

ifr.ifr_data = (caddr_t)&edata;

if(!ioctl(fd, siocethtool, &ifr)) else

}perror("linktest");

return errno;

}int

main(int argc, char *argv)

return linktest(argv[1]);

}

程式4:

#include #include #include #include #include #include #include #include #include #include #define base_value 257

int main(int argc,char *argv)

{ int mixer_fd=0;

char *names[sound_mixer_nrdevices]=sound_device_labels;

int value,i;

printf("\nusage:%s dev_no.[0..24] value[0..100]\n\n",argv[0]);

printf("eg. %s 0 100\n",argv[0]);

printf(" will change the volume to max volume.\n\n");

printf("the dev_no. are as below:\n");

for (i=0;i

參考:

linux系統ioctl使用示例

程式4 include include include include include include include include include include define base value 257 int main int argc,char argv int mixer fd 0 c...

Linux系統呼叫 ioctl

ioctl 是裝置驅動程式中裝置控制介面函式,通過指定的命令來實現對應的操作。驅動程式long unlocked ioctl struct file unsigned int,unsigned long long compat ioctl struct file unsigned int,unsig...

Linux系統中ioctl 用法

一 什麼是ioctl。ioctl是裝置驅動程式中對裝置的i o通道進行管理的函式。所謂對i o通道進行管理,就 是對裝置的一些特性進行控制,例如串列埠的傳輸波特率 馬達的轉速等等。它的呼叫個數 如下 int ioctl int fd,ind cmd,其中fd就是使用者程式開啟裝置時使用open函式返...