c - 如何设置 fifo 权限以便可以创建文件

标签 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 ));

最佳答案

除非你是根用户(例如 sudo 'd),否则你没有创建任何东西的权限,包括/目录中的 fifo 建议你选择一个你有写权限的目录。不建议更改/的权限。

关于c - 如何设置 fifo 权限以便可以创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53517162/

相关文章:

c - 隐式调用时如何使用RTLD_DEEPBIND?

linux - 将命令的输出重定向到现有 tar 中的新文件

linux - linux内核模块的读/写锁

linux - 抑制单个 sudo 命令的日志条目

c - 改进我的 C 风格字符串使用

c - 如何删除未使用的共享内存和信号量?

c - `long` 保证与 `size_t` 一样宽

c++ - c++ 程序核心转储的回溯中的无限中止()

Java 脚本 (JSR223) = 用于模板化的 Bean/Script Shell?

c - 将 XOR 作为宏实现,但未按预期工作