如何设置FIFO权限以便可以创建文件

时间:2018-11-28 10:22:21

标签: c linux

我正在做一个学校项目,其中必须复制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 ));

1 个答案:

答案 0 :(得分:3)

除非您是root用户(例如sudo'd),否则您无权创建任何东西,包括/目录中的fifo建议您选择您确实具有写许可权的目录。不建议更改/的权限。