static void create_child (void) { GError *err; GIOChannel *in_channels[2]; GIOChannel *out_channels[2]; GIOChannel **sub_channels; GSource *source; sub_channels = g_new (GIOChannel *, 2); io_pipe (in_channels); io_pipe (out_channels); sub_channels[0] = in_channels[0]; sub_channels[1] = out_channels[1]; source = g_io_create_watch (out_channels[0], G_IO_IN | G_IO_HUP); g_assert(source != NULL); g_source_set_closure (source, g_cclosure_new (G_CALLBACK (input_callback), in_channels[1], NULL)); g_source_attach (source, NULL); g_thread_create(run_child,sub_channels,FALSE,&err); }
static void create_child (void) { int pid; GIOChannel *in_channels[2]; GIOChannel *out_channels[2]; GSource *source; io_pipe (in_channels); io_pipe (out_channels); pid = fork (); if (pid > 0) /* Parent */ { g_io_channel_close (in_channels[0]); g_io_channel_close (out_channels[1]); source = g_io_create_watch (out_channels[0], G_IO_IN | G_IO_HUP); g_source_set_closure (source, g_cclosure_new (G_CALLBACK (input_callback), in_channels[1], (GClosureNotify)g_io_channel_unref)); g_source_attach (source, NULL); g_source_unref (source); g_io_channel_unref (in_channels[0]); g_io_channel_unref (out_channels[0]); g_io_channel_unref (out_channels[1]); } else if (pid == 0) /* Child */ { g_io_channel_close (in_channels[1]); g_io_channel_close (out_channels[0]); setsid (); run_child (in_channels[0], out_channels[1]); } else /* Error */ { fprintf (stderr, "Cannot fork: %s\n", g_strerror (errno)); exit (1); } }
static void create_child (void) { int pid, errsv; GIOChannel *in_channels[2]; GIOChannel *out_channels[2]; io_pipe (in_channels); io_pipe (out_channels); pid = fork (); errsv = errno; if (pid > 0) /* Parent */ { g_io_channel_close (in_channels[0]); g_io_channel_unref (in_channels[0]); g_io_channel_close (out_channels[1]); g_io_channel_unref (out_channels[1]); g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP, input_callback, in_channels[1]); } else if (pid == 0) /* Child */ { g_io_channel_close (in_channels[1]); g_io_channel_close (out_channels[0]); setsid (); run_child (in_channels[0], out_channels[1]); } else /* Error */ { fprintf (stderr, "Cannot fork: %s\n", g_strerror (errsv)); exit (1); } }
int main() { int64 pfd[2]; char buf[20480]; unsigned int i; if (!io_pipe(pfd)) return 111; io_nonblock(pfd[1]); if (!io_fd(pfd[1])) return 111; switch (fork()) { case -1: return 111; case 0: /* child */ io_close(pfd[1]); sleep(1); for (;;) { switch (io_waitread(pfd[0],buf,sizeof buf)) { case -1: buffer_putsflush(buffer_2,"io_waitread returned -1!\n"); return 111; case -3: buffer_puts(buffer_2,"io_waitread: "); buffer_puterror(buffer_2); buffer_putnlflush(buffer_2); return 111; case 0: io_close(pfd[0]); return 0; } } } io_close(pfd[0]); for (i=0; i<sizeof(buf); ++i) buf[i]="abcdefghihjklmnopqrstuvwxyz"[i%26]; for (i=0; i<1000; ++i) { int64 r; if ((r=io_waitwrite(pfd[1],buf,sizeof buf))!=sizeof buf) { buffer_puts(buffer_2,"io_waitwrite returned "); buffer_putlonglong(buffer_2,r); buffer_putnlflush(buffer_2); } } return 0; }
int main(int argc, char **argv) { ssize_t c; io_t *in, *out; codec_t *null0 = NULL; codec_t *null1 = NULL; codec_t *null2 = NULL; codec_t *null3 = NULL; codec_t *null4 = NULL; codec_t *zip = NULL; codec_t *unzip = NULL; codec_t *encrypt = NULL; codec_t *decrypt = NULL; unsigned char key[CODEC_CIPHER_KEY_BUFSZ]; unsigned char iv[CODEC_CIPHER_IV_LEN]; memset(ctx, 0, sizeof(context_t)); parse_opt(argc, argv); /* open the input stream */ if(ctx->file_in) { dbg_err_if(u_file_open(ctx->file_in, O_RDONLY, &in)); dbg_err_if(io_name_set(in, ctx->file_in)); } else { dbg_err_if(io_fd_create(0, 0, &in)); dbg_err_if(io_name_set(in, "stdin")); } /* open the output stream */ if(ctx->file_out) { dbg_err_if(u_file_open(ctx->file_out, O_WRONLY | O_CREAT | O_TRUNC, &out)); dbg_err_if(io_name_set(out, ctx->file_out)); } else { dbg_err_if(io_fd_create(1, 0, &out)); dbg_err_if(io_name_set(out, "stdout")); } /* create some null codec to stress-test the io_t */ dbg_err_if(codec_null_create(&null0)); dbg_err_if(codec_null_create(&null1)); dbg_err_if(codec_null_create(&null2)); dbg_err_if(codec_null_create(&null3)); dbg_err_if(codec_null_create(&null4)); /* zip */ #ifdef HAVE_LIBZ dbg_err_if(codec_gzip_create(GZIP_COMPRESS, &zip)); dbg_err_if(codec_gzip_create(GZIP_UNCOMPRESS, &unzip)); #endif /* aes256 */ #ifdef SSL_ON memset(key, 0, sizeof(key)); memset(iv, 0, sizeof(iv)); strcpy(key, "pwd"); strcpy(iv, "iv"); dbg_err_if(codec_cipher_create(CIPHER_ENCRYPT, EVP_aes_256_cbc(), key, iv, &encrypt)); dbg_err_if(codec_cipher_create(CIPHER_DECRYPT, EVP_aes_256_cbc(), key, iv, &decrypt)); #endif if(ctx->encode || ctx->decode) { /* for testing purpose attach the encode codec on input stream and * the decode one on the output; also add a few null codecs */ if(ctx->encode) { dbg_err_if(io_codec_add_tail(in, null0)); dbg_err_if(io_codec_add_tail(in, null1)); if(ctx->comp) dbg_err_if(io_codec_add_tail(in, zip)); dbg_err_if(io_codec_add_tail(in, null2)); if(ctx->cipher) dbg_err_if(io_codec_add_tail(in, encrypt)); dbg_err_if(io_codec_add_tail(in, null3)); dbg_err_if(io_codec_add_tail(in, null4)); } else { dbg_err_if(io_codec_add_tail(out, null0)); dbg_err_if(io_codec_add_tail(out, null1)); if(ctx->cipher) dbg_err_if(io_codec_add_tail(out, decrypt)); dbg_err_if(io_codec_add_tail(out, null2)); if(ctx->comp) dbg_err_if(io_codec_add_tail(out, unzip)); dbg_err_if(io_codec_add_tail(out, null3)); dbg_err_if(io_codec_add_tail(out, null4)); } } while((c = io_pipe(out, in)) > 0) ; dbg_if(io_free(in)); dbg_if(io_free(out)); return EXIT_SUCCESS; err: return EXIT_FAILURE; }