Blog » Blog Entry

/dev/69

Updated: May 13, 2009April 29, 2009

Here's a little something for fun:

It's a /dev/69 for those bored of /dev/zero:

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

int daemon_init( void )
{
    pid_t pid;

    if ( ( pid = fork() ) < 0 )
    {
        return -1;
    }
    else if ( pid != 0 )
    {
        exit(0); /* parent exits */
    }
    
    setsid();

    chdir( "/dev" );
    umask(0);
    
    return 0;

}

void create_fifo( char* filename )
{
    if ( mknod( filename, S_IFIFO | 0600, 0 ) == -1 )
    {
        system( "mkfifo --mode=0600 /dev/69" );
    }
}

void check_fifo( char* filename )
{
    struct stat buf;

    if ( stat( filename, &buf ) == -1 )
    {
        create_fifo(filename);

        if ( stat( filename, &buf ) == -1 )
        {
            exit(EXIT_FAILURE);
        }
    }
    
    if ( !S_ISFIFO(buf.st_mode) )
    {
        if ( unlink(filename) == -1 )
        {
            exit(EXIT_FAILURE);
        }

        create_fifo(filename);
    }
}

void monitor_fifo( char* filename )
{
    while (1)
    {
        int fd;

        check_fifo(filename);
        
        if ( ( fd = open( filename, O_WRONLY ) ) == -1 )
        {
            exit(EXIT_FAILURE);
        }

        while (1)
        {
            write( fd, "69", strlen("69") );
        }
        
        (void) close(fd);
        
        sleep(1);  /* to avoid dup sigs */
    }
}

int main( int argc, char* argv[] )
{
    if ( daemon_init() )
    {
        fprintf( stderr, "%s: Error initializing daemon\n", argv[0] );

        return -1;
    }

    monitor_fifo("/dev/69");

    return 0;
}

Build, install, and use:

# gcc -o 69 69.c

# sudo ./69 

# sudo cat /dev/69

[...]

^C
Tags: c, dev, 69

« Rails .html.erb -> .erb Dear Time Warner and any other retarded ISPs looking to grow a brain.. »

Add a comment:

Title:

Comment:

Name:

Email:

Website: