/ Published in: C++
Expand |
Embed | Plain Text
# build: `gcc -o 69 69.c` # insert as root: `./69` # access: `cat /dev/69` #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; }
You need to login to post a comment.
