简体   繁体   English

C 中 open() 函数中的“权限被拒绝”

[英]"Permission denied" in open() function in C

I am new to C programming.我是 C 编程的新手。 I am having problem writing to a file using the open() function in C, here is my code for clarity我在使用 C 中的 open() 函数写入文件时遇到问题,为了清楚起见,这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

void usage(char *prog_name, char *filename){
    printf("Usage: %s <data to add to %s> \n",prog_name, filename);
    exit(0);
}

void fatal(char *);                          
void *errchck_malloc(unsigned int);     

int main(int argc, char *argv[]){
    int fd; // File descriptor
    char *buffer, *datafile;

    buffer = (char *) errchck_malloc(100);
    datafile = (char *) errchck_malloc(20);
    strcpy(datafile, "./simplenote.txt");

    if (argc < 2)
        usage(argv[0], datafile);

    strcpy(buffer, argv[1]);

    printf("[DEBUG] buffer @ %p: \'%s\'\n", buffer, buffer);
    printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);

    strncat(buffer, "\n", 1);

    // Open file 
    fd = open(datafile, O_CREAT|O_RDWR,O_APPEND, S_IRUSR, S_IWUSR);
    if(fd == -1)
        fatal("in main() while opening file");
    printf("[DEBUG] file descriptor is %d\n", fd);

    // Writing data to file
    if(write(fd, buffer, strlen(buffer))==-1)
        fatal("in main() while writing buffer to file");

    // Closing file
    if(close(fd) == -1)
        fatal("in main() while closing file");

    printf("Note saved\n");
    free(buffer);
    free(datafile);
}

// fatal(): Function to display error message then exit
void fatal(char *message){
    char err_msg[100];

    strcpy(err_msg, "[!!] Fatal Error ");
    strncat(err_msg, message, 83);
    perror(err_msg);
    exit(-1);
}

// errchck_malloc(): An error check malloc wrapper function
void *errchck_malloc(unsigned int size){
    void *ptr;
    ptr = malloc(size);
    if(ptr == NULL)
        fatal("in errchck_malloc() on memory allocation");
    return ptr;
}

When I execute the program on the first try, the program runs as expected.当我第一次执行程序时,程序按预期运行。

first run:第一次运行:

user: ./simplenote "Hello, again"
[DEBUG] buffer @ 0x7fafcb4017a0: 'Hello again'
[DEBUG] datafile @ 0x7fafcb401810: './simplenote.txt'
[DEBUG] file descriptor is 3
Note saved

when I try to open the file and view the text, I get a permission denied error.当我尝试打开文件并查看文本时,出现权限被拒绝错误。 when I try to open the file with sudo, it opens and the text is in the file.当我尝试使用 sudo 打开文件时,它会打开并且文本在文件中。 When I run the program a second time, I get an error while opening the file because of permission issues.当我第二次运行该程序时,由于权限问题,在打开文件时出现错误。

second run:第二次运行:

user: ./simplenote "just checking if it is still working"                                                           
[DEBUG] buffer @ 0x7face4c017a0: 'just checking if it is still working'
[DEBUG] datafile @ 0x7face4c01810: './simplenote.txt'
[!!] Fatal Error in main() while opening file: Permission denied

How do I fix the permission issues with the file creation?如何解决文件创建的权限问题?

File created when program run for first time has permissions that does not allows you to append程序第一次运行时创建的文件具有不允许您附加的权限

stat simplenote.txt 
  File: simplenote.txt
  Size: 5           Blocks: 8          IO Block: 4096   regular file
Device: 2fh/47d Inode: 32810078    Links: 1
Access: (2000/------S---)  Uid: ( 1000/ user)   Gid: ( 1000/ user)
Access: 2020-01-05 19:29:34.317872734 +0100
Modify: 2020-01-05 19:29:34.317872734 +0100
Change: 2020-01-05 19:29:34.317872734 +0100

You should combine mode using |您应该使用|组合模式like this:像这样:

fd = open(datafile, O_CREAT|O_RDWR|O_APPEND, S_IRUSR | S_IWUSR);

You can check what arguments you should pass to open using man open , on my system it show something like this (trimmed to important parts):您可以使用man open检查应该传递给open参数,在我的系统上它显示如下内容(修剪为重要部分):

   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);

   int creat(const char *pathname, mode_t mode);

   int openat(int dirfd, const char *pathname, int flags);
   int openat(int dirfd, const char *pathname, int flags, mode_t mode);

The arguments of open() are not correct. open()的参数不正确。 The documentation of open() says: open()文档说:

int open(const char *pathname, int flags, mode_t mode); int open(const char *pathname, int flags, mode_t mode);

O_APPEND is a flag, it should be OR-ed with the other two to produce the flags argument. O_APPEND是一个标志,它应该与其他两个进行 OR 运算以产生flags参数。

S_IRUSR and S_IWUSR are permissions. S_IRUSRS_IWUSR是权限。 They should be OR-ed to produce the mode argument.它们应该进行 OR 运算以产生mode参数。

All in all, the call to open() should be:总而言之,对open()的调用应该是:

fd = open(datafile, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM