local/namedstreampipe.c

///////////////////////////////////////////////////////////////////////////////
// Filename: namedstreampipe.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: function that creates a named stream pipe
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/03/27 15:16:31 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////

    // uncomment this if the test code should NOT be compiled
#define TEST


// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */



// System headers /////////////////////////////////////////// System headers //

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>



// Local headers ///////////////////////////////////////////// Local headers //
    /* NONE */



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //

/*
 * namedstreampipe()
 *
 * this function creates a named stream pipe
 *
 * in: name: file name for stream pipe
 *     fd:   file descriptor pair returned here
 *
 * result: 0 if everything ok, -1 for error (with errno set accordingly)
 */


int
namedstreampipe(char *name, int fd[2])
{
    int len;
    struct sockaddr_un unix_addr;

    if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
        return -1;

    unlink(name);   // remove file name if it exists

    memset(&unix_addr, 0, sizeof(unix_addr));
    unix_addr.sun_family = AF_UNIX;
    strcpy(unix_addr.sun_path, name);
    len = strlen(unix_addr.sun_path) + sizeof(unix_addr.sun_family);
    
    if(bind(fd[0], (struct sockaddr *) &unix_addr, len) < 0)
        return -1;

    return 0;
}

// Main /////////////////////////////////////////////////////////////// Main //

#ifdef TEST
// this program tests if the function above works.

#include <iostream.h>
#include "../common.h"

int main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    

    int fd[2];
    int len;

    char buffer[512];

    if( namedstreampipe("testnamed", fd) != 0)
        error.system("can,t open named stream pipe");

    if( write(fd[0], "Hello ", 6) != 6)
        error.system("write error to fd[0]");

    if( write(fd[1], "World!", 6) != 6)
        error.system("write error to fd[1]");

    if( (len=read(fd[0], buffer, 512)) != 6)
        error.system("read error on fd[0]");

    if( strncmp(buffer, "World!", 6) != 0)
        error.panic("write != read!");

    if( (len=read(fd[1], buffer, 512)) != 6)
        error.system("read error on fd[1]");

    if( strncmp(buffer, "Hello ", 6) != 0)
        error.panic("write != read!");

    unlink("testnamed");

    cout << "namedstreampipe() works" << endl;
}
#endif