void test_env (void) { GError *error = NULL; const gchar command[] = "env"; gchar *env[] = { "name1=value1", "name2=value2", NULL }; process = gcut_process_new(command, NULL); setup_process(process); gcut_process_set_env(process, "name1", "value1", "no-value", NULL, "name2", "value2", NULL); cut_assert_equal_string_array_with_free(env, gcut_process_get_env(process)); gcut_process_set_flags(process, G_SPAWN_SEARCH_PATH); gcut_process_run(process, &error); gcut_assert_error(error); wait_exited(); cut_assert_equal_string("name1=value1\n" "name2=value2\n", output_string->str); cut_assert_equal_string("", error_string->str); }
void test_flags (void) { GError *error = NULL; process = gcut_process_new("echo", "XXX", NULL); setup_process(process); gcut_process_set_flags(process, 0); cut_assert_false(gcut_process_get_flags(process) & G_SPAWN_SEARCH_PATH); cut_assert_false(gcut_process_run(process, &actual_error)); expected_error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT, "Failed to execute child process \"%s\" (%s)", "echo", g_strerror(ENOENT)); gcut_assert_equal_error(expected_error, actual_error); gcut_process_set_flags(process, G_SPAWN_SEARCH_PATH); gcut_process_run(process, &error); gcut_assert_error(error); wait_exited(); cut_assert_equal_string("XXX\n", output_string->str); cut_assert_equal_string("", error_string->str); }
void test_io (void) { GError *error = NULL; const gchar buffer[] = "XXX\n"; const gchar command[] = #ifdef G_OS_WIN32 "type" #else "cat" #endif ; process = gcut_process_new(command, NULL); setup_process(process); gcut_process_run(process, &error); gcut_assert_error(error); gcut_process_write(process, buffer, strlen(buffer), &error); gcut_assert_error(error); g_io_channel_shutdown(gcut_process_get_input_channel(process), TRUE, &error); gcut_assert_error(error); wait_exited(); cut_assert_equal_string("XXX\n", output_string->str); cut_assert_equal_string("", error_string->str); }
void test_run (void) { GError *error = NULL; process = gcut_process_new(cuttest_echo_path, "XXX", NULL); setup_process(process); gcut_assert_equal_pid(0, gcut_process_get_pid(process)); gcut_process_run(process, &error); gcut_assert_error(error); gcut_assert_not_equal_pid(0, gcut_process_get_pid(process)); wait_exited(); cut_assert_equal_string("XXX\n", output_string->str); cut_assert_equal_string("", error_string->str); }
void test_kill (void) { GError *error = NULL; const gchar command[] = "cat"; #ifdef G_OS_WIN32 #define SIGKILL 9 #endif process = gcut_process_new(command, NULL); setup_process(process); gcut_process_set_flags(process, G_SPAWN_SEARCH_PATH); gcut_process_run(process, &error); gcut_assert_error(error); gcut_process_kill(process, SIGKILL, &error); gcut_assert_error(error); wait_termed_by_signal(SIGKILL); }
gint main (gint argc, gchar **argv) { struct _CamelLockHelperMsg msg; gint len; gint res; gchar *path; fd_set rset; struct timeval tv; struct _lock_info *info; setup_process (); do { /* do a poll/etc, so we can refresh the .locks as required ... */ FD_ZERO (&rset); FD_SET (STDIN_FILENO, &rset); /* check the minimum timeout we need to refresh the next oldest lock */ if (lock_info_list) { time_t now = time (NULL); time_t left; time_t delay = CAMEL_DOT_LOCK_REFRESH; info = lock_info_list; while (info) { left = CAMEL_DOT_LOCK_REFRESH - (now - info->stamp); left = MAX (left, 0); delay = MIN (left, delay); info = info->next; } tv.tv_sec = delay; tv.tv_usec = 0; } d (fprintf (stderr, "lock helper waiting for input\n")); if (select (STDIN_FILENO + 1, &rset, NULL, NULL, lock_info_list ? &tv : NULL) == -1) { if (errno == EINTR) break; continue; } /* did we get a timeout? scan for any locks that need updating */ if (!FD_ISSET (STDIN_FILENO, &rset)) { time_t now = time (NULL); time_t left; d (fprintf (stderr, "Got a timeout, checking locks\n")); info = lock_info_list; while (info) { left = (now - info->stamp); if (left >= CAMEL_DOT_LOCK_REFRESH) { lock_touch (info->path); info->stamp = now; } info = info->next; } continue; } len = read_n (STDIN_FILENO, &msg, sizeof (msg)); if (len == 0) break; res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL; if (len == sizeof (msg) && msg.magic == CAMEL_LOCK_HELPER_MAGIC) { switch (msg.id) { case CAMEL_LOCK_HELPER_LOCK: res = CAMEL_LOCK_HELPER_STATUS_NOMEM; if (msg.data > 0xffff) { res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL; } else if ((path = malloc (msg.data + 1)) != NULL) { res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL; len = read_n (STDIN_FILENO, path, msg.data); if (len == msg.data) { path[len] = 0; res = lock_path (path, &msg.data); } free (path); } break; case CAMEL_LOCK_HELPER_UNLOCK: res = unlock_id (msg.data); break; } } d (fprintf (stderr, "returning result %d\n", res)); msg.id = res; msg.magic = CAMEL_LOCK_HELPER_RETURN_MAGIC; write_n (STDOUT_FILENO, &msg, sizeof (msg)); } while (1); d (fprintf (stderr, "parent exited, clsoing down remaining id's\n")); while (lock_info_list) unlock_id (lock_info_list->id); return 0; }
void process(t_conv *conv) { setup_process(); if (conv->specifier != 0) g_process[chr_at(SPECIFIER, conv->specifier)](conv); }