在Linux中写入命名管道的最佳,最安全的方法是什么?

问题描述:

What is the best, secure way to write to a named pipe in Linux? Or, how can I make a named pipe secure?

Here's how I write to the pipe on linux using PHP:

$con = fopen("/tmp/myFIFO", "w");
fwrite($con, "UP
");
fclose($con);

I wish to make it more secure.

This is how I create the pipe in C:

int pc;
char mode[] = "0777";
int i = strtol(mode, 0, 8);
pc = mkfifo(FIFO, 0);

if(pc < 0) {
    printf("Failed in creating a pipe
");
    printf("Exiting...
");
    exit(1);
}
else {
    printf("Success in Creating Pipe
");
    chmod("/tmp/myFIFO", i);
}

You can use umask to control the permissions of the files you create. You also have chmod in php as well.

However, you also have posix_mkfifo available, which exactly fits your purpose:

bool posix_mkfifo ( string $pathname , int $mode )