/* -------------------------------------------------------------------------- launch-proxy-slashem.c, Launch slashem Written By Martin McCann Version 0.0.1a, 2007/01/26 address@hidden -------------------------------------------------------------------------- A Proof-of-concept program which will launch an instance of slashem, and send and recieve a few messages. ----------------------------------------------------------------------- */ #include #include #include #include int pid; int to_parent[2], to_child[2]; /* --- Forks the process, and lanches slashem in the child process, returning true to the parent process --- */ int launchSlashem() { if(pipe(to_child)) // Failed to open pipe return 0; if(pipe(to_parent)) { // failed to open pipe close(to_child[0]); close(to_child[1]); return 0; } if((pid = fork()) < 0) { // Failed to fork close(to_child[0]); close(to_child[1]); close(to_parent[0]); close(to_parent[1]); return 0; } if(!pid) { // Child process close(to_child[1]); close(to_parent[0]); dup2(to_child[1], 0); // Set to startard input dup2(to_parent[0], 1); // Set to standard output execl("/usr/local/bin/slashem", "slashem", "", NULL); // Launch _exit(127); // Should never reach here } close(to_child[0]); close(to_parent[0]); return 1; } int killchild() { /*** Is this right? ***/ int status; if (waitpid(pid,&status,WNOHANG) == pid) return !WIFEXITED(status); } void rpc_error_handler(int class, const char *error) { int i; fputs("NhProxy RPC error: ", stderr); if (!killchild()) { fprintf(stderr, "Error trying to kill child.\n"); exit(1); } exit(126); } int debug_read(void *handle, void *buf, unsigned int len) { int retval; retval = read((int)handle, buf, len); /* --- What should it do here? --- */ return retval; } int debug_write(void *handle, void *buf, unsigned int len) { int retval; retval = write((int)handle, buf, len); /* --- What should it do here? --- */ return retval; } int main(int argc, char *argv[]) { NhProxyIO *rd, *wr; printf("%s.\n", argv[0]); if(!launchSlashem()) { fprintf(stderr, "Error launching slashem\n"); exit(-1); } fprintf(stderr, "Slashem Launched"); if(!(rd = nhproxy_io_open(debug_read, (void *)to_parent[0], NHPROXY_IO_RDONLY)) || !(wr = nhproxy_io_open(debug_write, (void *)to_child[1], NHPROXY_IO_WRONLY))) { fprintf(stderr,"Error opening IO streams.\n"); exit(-1); } nhproxy_rpc_set_errhandler(rpc_error_handler); if(!nhproxy_rpc_init(rd, wr, NULL)) { fprintf(stderr, "We failed to initialize NhExt.\n"); exit(-1); } if(!nhproxy_rpc_set_protocol(1)) { fprintf(stderr, "Failed to select protocol 1.\n"); exit(1); } /*** Do some tests ***/ nhproxy_rpc(0x7FFF, 0, 0); nhproxy_rpc_end(); nhproxy_io_close(rd); nhproxy_io_close(wr); if (!killchild()) { fprintf(stderr, "Error trying to kill child.\n"); exit(1); } fprintf(stderr, "finished.\n"); }