我正在做一个学校项目,我必须在其中复制 syslog 守护进程。我正在尝试通过使用命令打开的命名管道为 syslog 守护程序编写
char * fdfifo = "/fifo";
mkfifo(fdfifo, 0666);
但是,当我尝试打开管道时,我从 errno 收到一条错误消息:
Value of errno: 13
Error printed by perror: Permission denied
Error opening file: Permission denied
当我以 sudo 运行应用程序时,文件会按原样创建。只有当我试图在其中存储文件描述符的文件不存在并且我必须创建文件时才会遇到此问题。
这是我在错误点之前的完整代码:
pthread_mutex_t lock2;
char * logName;
int fd[2];
static volatile int keepRunning = 1;
void intHandler(int dummy) {
keepRunning = 0;
}
int openLog(char* logname, pthread_mutex_t lock, pthread_t tid){
signal(SIGINT, intHandler);
lock2=lock;
logName = logname;
// FILE *f;
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
return 0;
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0){
exit(EXIT_SUCCESS);
}
/* Set new file permissions */
umask(0);
char * fdfifo = "/fifo";
mkfifo(fdfifo, 0666);
int errnum;
errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
最佳答案
除非你是根用户(例如 sudo 'd),否则你没有创建任何东西的权限,包括/目录中的 fifo 建议你选择一个你有写权限的目录。不建议更改/的权限。
关于c - 如何设置 fifo 权限以便可以创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53517162/