LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 693|回复: 2

led通知小程序

[复制链接]
发表于 2011-2-19 13:39:01 | 显示全部楼层 |阅读模式
曾在本版上看到过一个蜂鸣器小程序, 用来作为编译结束后的提示音, 不过我不喜欢蜂鸣器的声音, 所以改用caps lock的led灯闪烁, 代码比较简单:
  1. //led.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <sys/ioctl.h>
  9. //grep KDGETLED -R /usr/include/linux/
  10. #define KDSETLED 0x4b32
  11. int main(int argc, char *argv[]) {
  12.     int fd, i, max, current_uid;
  13.     i = 0;
  14.     max = 10;
  15.     if (argc == 2) {
  16.         max = atoi(argv[1]);
  17.     } else if (argc > 2){
  18.         printf("args error\n");
  19.     }
  20.     current_uid = getuid();
  21.     if ((current_uid != 0) && setuid(0)) {
  22.         printf("setuid error\n");
  23.         return 1;
  24.     }
  25.     if(!(fd = open("/dev/tty1", O_NOCTTY))) {
  26.         printf("open error\n");
  27.         return 1;
  28.     }
  29.     if (-1 == ioctl(fd, KDSETLED, 0)) {
  30.         printf("ioctl error\n");
  31.         return 1;
  32.     }
  33.     while (i < max) {
  34.         ioctl(fd, KDSETLED, 4);
  35.         usleep(500000);
  36.         ioctl(fd, KDSETLED, 0);
  37.         usleep(500000);
  38.         i++;
  39.     }
  40.     close(fd);
  41.     return 0;
  42. }
复制代码

编译并赋权限:
  1. gcc -Wall led.c -o led
  2. chown root.root led
  3. chmod +s led
复制代码

用法:
  1. make;led
复制代码
参数可以表示闪烁次数
发表于 2011-2-19 14:06:40 | 显示全部楼层
  1. shm$ ./led 3
  2. setuid error
  3. shm$ ll led
  4. -rwsr-sr-x 1 root root 5419 Feb 19 13:58 led
  5. shm$ id
  6. uid=1000(lfs) gid=100(users) groups=100(users),14(uucp),19(log),91(video),92(audio),93(optical),150(wireshark)
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-2-19 14:33:28 | 显示全部楼层
我的系统是debian, 内核2.6.37.1, 新建用户也是可以用的:
  1. aaaa@debian:/home/stesen$ ls -l a.out
  2. -rwsr-sr-x 1 root root 7969 Feb 19 14:26 a.out
  3. aaaa@debian:/home/stesen$ ./a.out 3
  4. aaaa@debian:/home/stesen$ id
  5. uid=1002(aaaa) gid=1003(aaaa) groups=1003(aaaa)
复制代码
莫非lfs有所不同? 还是你开了selinux?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表