簡體   English   中英

dup3的工作

[英]Working of dup3

在閱讀dup3系統調用時,我知道它將僅更改重復文件描述符的O_CLOEXEC標志。 但是,當我在下面的程序中為所有3個輸出編寫程序時,打印的標志是否存在,並且該標志基於打開時是否在原始文件描述符中指定了O_CLOEXEC。 怎么了 ? 我真的不明白

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h> 
#include <errno.h>
#include <fcntl.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

void PrintUsage();
void PrintVersion();
void PrintStatusCloexec( int );
void dup3_code( const char * );

void main(int argc, char *argv[])
{
    int hflag = FALSE, vflag = FALSE, nflag = FALSE, src_flag = FALSE;
    char *src_file_name = NULL; 
    int src_len = 0;
    char c;
    int option_index;

    struct option long_options[] = { 
            { "version", no_argument, 0, 0},
            { "help", no_argument, 0, 0}, 
            { "src_file", required_argument, 0, 0},
            {0,0,0,0}
        };      

    while( ( c = getopt_long(argc, argv, "hvs:", long_options, &option_index) ) != -1 )
    {
        switch( c )
        {
            case 'h':
                hflag = TRUE;
                break;
            case 'v':
                vflag = TRUE;
                break;
            case 's':
                src_flag = TRUE;
                src_len = strlen( optarg );
                src_file_name = (char *)calloc(1, src_len + 1);
                strncpy(src_file_name, optarg, src_len);
                break;
            case 0:
                switch(option_index)
                {
                    case 0: vflag = TRUE;
                        break; 
                    case 1: hflag = TRUE;
                        break;
                    case 2: src_flag = TRUE;
                        src_file_name = (char *) calloc(1, strlen(optarg) + 1);
                        strncpy(src_file_name, optarg, src_len);
                        break;
                    default: printf(" Invalid argument index : %d", option_index );
                        exit(EXIT_FAILURE); 
                }
                break;

            case '?':
                break;
            default:
                printf( " Invalid option %c", c);
                exit(EXIT_FAILURE);
        }
    }

    if( vflag == FALSE && src_flag == FALSE ) 
    {
        PrintUsage();
    }

    if(hflag == TRUE)
    {
        PrintUsage();
    }

    if(vflag == TRUE )
    {
        PrintVersion();
    }

    if(src_flag == TRUE)
    {
        printf(" src file name : %s\n", src_file_name);
        dup3_code( src_file_name );
    }

    exit(EXIT_SUCCESS);
}

void PrintUsage()
{
    printf(" Usage : %s [-h] [-v] [-s|src_file file_name]\n" , __FILE__ );
}

void PrintVersion()
{
    printf(" Version : 1.0\n");
}

void dup3_code( const char *file_name )
{
    int fd = open( file_name, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
    if( fd == -1 )
    {
        printf(" Error opening file %s : %s\n", file_name, strerror(errno));
        exit(EXIT_FAILURE);
    }
    printf( "After file is opened:\n");
    PrintStatusCloexec( fd );

    int new_fd = dup( fd );
    printf( "After dup:\n" );
    PrintStatusCloexec( new_fd );
    close( new_fd );

    new_fd = dup3( fd, new_fd, O_CLOEXEC );
    printf( "After dup3:\n" );
    PrintStatusCloexec( new_fd );
    close( new_fd );

    close( fd );
}

void PrintStatusCloexec( int fd )
{
    int flags = fcntl( fd, F_GETFL );
    if( flags & O_CLOEXEC )
    {
        printf( " close on exec flag for file id %d is set\n", fd );
    }
    else
    {
        printf( " close on exec flag for file id %d is not set\n", fd );
    }

}

也是我的GNU C庫版本:

GNU C Library (Ubuntu EGLIBC 2.15-0ubuntu10) stable release version 2.15.

要獲取標志的當前狀態,您需要使用F_GETFD並查看FD_CLOEXEC位。 F_GETFL僅返回屬於打開文件描述的標志(即,在重復的描述符和分支的進程之間共享的標志)。 O_CLOEXEC結果中是否存在F_GETFL沒有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM