免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7050 | 回复: 5
打印 上一主题 下一主题

[Linux] ioctl: Operation not supported [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-11 22:00 |只看该作者 |倒序浏览
程序如下:
/*
* setext2.c - Set ext2 special flags
*/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/ext2_fs.h>
#include <sys/ioctl.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    int fd;
    long flags;

    /* Usage nag */
    if (argc != 2) {
        puts("USAGE:setext2 {filename}");
        exit(EXIT_FAILURE);
    }

    if ((fd = open(argv[1], O_RDONLY)) < 0) {
        perror("open");
        exit(EXIT_FAILURE);
    }
    /* These are the flags we'll set on the file */
    flags = EXT2_SYNC_FL | EXT2_NODUMP_FL;
    if (ioctl(fd, EXT2_IOC_SETFLAGS, &flags)) {
        perror("ioctl");
        close(fd);
        exit(EXIT_FAILURE);
    }

    if (flags & EXT2_SYNC_FL)
        puts("SYNC flag set");
    if (flags & EXT2_NODUMP_FL)
        puts("NODUMP flag set");

    close(fd);
    exit(EXIT_SUCCESS);
}


为什么编译的时候报错ioctl: Operation not supported

论坛徽章:
0
2 [报告]
发表于 2012-12-12 09:38 |只看该作者
本帖最后由 seaquester 于 2012-12-12 11:10 编辑

你说的这个错误信息“ioctl: Operation not supported” 不像是编译是的错误信息,反倒像执行是的错误信息。

我在CentOS 6.3下编译是下面的错误:
$ gcc -Wall -o setext2 setext2.c
In file included from setext2.c:10:
/usr/include/linux/ext2_fs.h: In function ‘ext2_mask_flags’:
/usr/include/linux/ext2_fs.h:182: error: ‘FS_DIRSYNC_FL’ undeclared (first use in this function)
/usr/include/linux/ext2_fs.h:182: error: (Each undeclared identifier is reported only once
/usr/include/linux/ext2_fs.h:182: error: for each function it appears in.)
/usr/include/linux/ext2_fs.h:182: error: ‘FS_TOPDIR_FL’ undeclared (first use in this function)
/usr/include/linux/ext2_fs.h:184: error: ‘FS_NODUMP_FL’ undeclared (first use in this function)
/usr/include/linux/ext2_fs.h:184: error: ‘FS_NOATIME_FL’ undeclared (first use in this function)
setext2.c: In function ‘main’:
setext2.c:30: error: ‘FS_SYNC_FL’ undeclared (first use in this function)
setext2.c:30: error: ‘FS_NODUMP_FL’ undeclared (first use in this function)
setext2.c:31: error: ‘FS_IOC_SETFLAGS’ undeclared (first use in this function)

在 #include <linux/ext2_fs.h>之前,include 另外一个头文件 fs.h 之后就可以编译了。
#include <linux/fs.h>

论坛徽章:
0
3 [报告]
发表于 2012-12-12 11:10 |只看该作者
回复 1# xiaoruoax

经过尝试发现,解决方法很简单。
只需要,先通过EXT2_IOC_GETFLAGS获取 ext2 flags,然后,将要设置的flag通过 “|” 操作附加上去,然后通过EXT2_IOC_SETFLAGS设置就可以成功,在CentOS 6.3 (x86_64)上面测试通过。设置完成后,通过lsattr可以查看设置的结果(chattr可以设置flags)。

示例代码如下:

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <linux/fs.h>
  7. #include <linux/ext2_fs.h>
  8. #include <sys/ioctl.h>
  9. #include <errno.h>


  10. int main(int argc, char *argv[])
  11. {
  12.     int fd;
  13.     long flags;

  14.     if (argc != 2) {
  15.         printf("Usage: %s <filename>\n", argv[0]);
  16.         exit(EXIT_FAILURE);
  17.     }

  18.     if ((fd = open(argv[1], O_RDONLY)) < 0) {
  19.         perror("open");
  20.         exit(EXIT_FAILURE);
  21.     }

  22.     /* Get attributes from the file */
  23.     if (ioctl(fd, EXT2_IOC_GETFLAGS, &flags)) {
  24.         perror("ioctl");
  25.         exit(EXIT_FAILURE);
  26.     }

  27.     /* Set attributes to the file */
  28.     flags |= (EXT2_SYNC_FL | EXT2_NODUMP_FL);
  29.     if (ioctl(fd, EXT2_IOC_SETFLAGS, &flags)) {
  30.         perror("ioctl");
  31.         close(fd);
  32.         exit(EXIT_FAILURE);
  33.     }

  34.     if (flags & EXT2_SYNC_FL) {
  35.         puts("SYNC flag set");
  36.     }
  37.     if (flags & EXT2_NODUMP_FL) {
  38.         puts("NODUMP flag set");
  39.     }

  40.     close(fd);
  41.     exit(EXIT_SUCCESS);
  42. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-12-12 16:55 |只看该作者
不好意思,修改一下,是执行的时候的报错

论坛徽章:
0
5 [报告]
发表于 2012-12-14 09:07 |只看该作者
回复 4# xiaoruoax


    试试我修改的那份代码,在我这边可以执行,没错误。

论坛徽章:
0
6 [报告]
发表于 2012-12-15 21:45 |只看该作者
回复 5# seaquester


    果然好了!谢谢O(∩_∩)O
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP