简体   繁体   中英

How to set fifo permissions so that file can be created

I am doing a school project in which I have to buplicate the syslog daemon. I am trying to write for the syslog daemon via a named pipe which is opened with command

char * fdfifo = "/fifo";
mkfifo(fdfifo, 0666);

However when I try to open the pipe, i get an error message from errno:

Value of errno: 13
Error printed by perror: Permission denied
Error opening file: Permission denied

when I run the application as sudo, the file is created as it should be. And the problem is only encountered when the file, in which I am trying to store the file descriptor doesn't exist and I have to create the file.

Here is my full code until the point of the error:

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

Unless you are root (eg sudo 'd) you do not have permissions to create anything, including a fifo in the / directory suggest you choose a dir you do have write permission to. Changing the permissions on / is not recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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