|
曾在本版上看到过一个蜂鸣器小程序, 用来作为编译结束后的提示音, 不过我不喜欢蜂鸣器的声音, 所以改用caps lock的led灯闪烁, 代码比较简单:- //led.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- //grep KDGETLED -R /usr/include/linux/
- #define KDSETLED 0x4b32
- int main(int argc, char *argv[]) {
- int fd, i, max, current_uid;
- i = 0;
- max = 10;
- if (argc == 2) {
- max = atoi(argv[1]);
- } else if (argc > 2){
- printf("args error\n");
- }
- current_uid = getuid();
- if ((current_uid != 0) && setuid(0)) {
- printf("setuid error\n");
- return 1;
- }
- if(!(fd = open("/dev/tty1", O_NOCTTY))) {
- printf("open error\n");
- return 1;
- }
- if (-1 == ioctl(fd, KDSETLED, 0)) {
- printf("ioctl error\n");
- return 1;
- }
- while (i < max) {
- ioctl(fd, KDSETLED, 4);
- usleep(500000);
- ioctl(fd, KDSETLED, 0);
- usleep(500000);
- i++;
- }
- close(fd);
- return 0;
- }
复制代码
编译并赋权限:- gcc -Wall led.c -o led
- chown root.root led
- chmod +s led
复制代码
用法:参数可以表示闪烁次数 |
|