我正在尝试使用C语言在Linux上学习低级图形编程。我对Linux和C都比较陌生。我正在尝试找出在代码中管理/dev/fb0
权限的最负责任方式。
我的代码:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
int main(){
int fb_fd = 0;
// Open the file for reading and writing
fb_fd = open("/dev/fb0", O_RDWR);
if (fb_fd == -1) {
perror("Error: cannot open framebuffer device");
exit(1);
}
printf("The framebuffer device was opened successfully.\n");
struct fb_fix_screeninfo finfo;
struct fb_var_screeninfo vinfo;
//Get variable screen information
ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo);
//Get fixed screen information
ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo);
printf("vinfo.bits_per-pixel: %d\n",vinfo.bits_per_pixel);
close(fb_fd);
return(0);
}
我的问题是:程序只能使用sudo ./my_program
运行,否则会无法打开帧缓冲区(权限被拒绝)。我不确定在编写旨在绘制到帧缓冲区的软件时如何通常管理/dev/fb0
的权限。如何“授予权限”给我的程序,使其可以由普通用户运行而无需特殊权限?
编辑:按要求提供一些控制台输出:
设置模式和权限
~/learning_c/game_0$ ls -l /dev/fb0
crw-rw---- 1 root video 29, 0 Oct 28 08:01 /dev/fb0
~/learning_c/game_0$ ls -l ./bin/game
-rwxrwxr-x 1 logan logan 15160 Oct 29 21:12 ./bin/game
~/learning_c/game_0$ chgrp video ./bin/game
chgrp: changing group of './bin/game': Operation not permitted
~/learning_c/game_0$ sudo chgrp video ./bin/game
[sudo] password for logan:
~/learning_c/game_0$ ls -l ./bin/game
-rwxrwxr-x 1 logan video 15160 Oct 29 21:12 ./bin/game
~/learning_c/game_0$ sudo chmod -v g+s ./bin/game
mode of './bin/game' changed from 0775 (rwxrwxr-x) to 2775 (rwxrwsr-x)
~/learning_c/game_0$ ls -l ./bin/game
-rwxrwsr-x 1 logan video 15160 Oct 29 21:12 ./bin/game
尝试运行./bin/game。
~/learning_c/game_0$ ./bin/game
Error: cannot open framebuffer device: Permission denied
logan@logan-Aspire-5560:~/learning_c/game_0$ sudo ./bin/game
[sudo] password for logan:
The framebuffer device was opened successfully
chgrp
和chmod
的建议是通过命令行运行,但我似乎没有得到预期的结果。我以详细模式运行它们以验证它们是否更改了组和文件模式位,但程序仍然被拒绝访问而需要使用sudo
。命令行脚本是程序员获取访问帧缓冲区权限的典型方式吗? - Logan Benderls -l
检视文件模式,我看到-rwxr-xr-x
,这表示组拥有xr-
权限(没有w
权限)。该代码正在以O_RDWR
模式打开文件。视频组也需要写入权限吗? - Logan Benderls -l /dev/fb0
和ls -l my_program
的完整输出。 - Ctx