/platform/bluetooth/external/bluetooth/glib/tests/child-test.c

https://github.com/lems111/Intercept-CM6-Kernel · C · 206 lines · 135 code · 39 blank · 32 comment · 12 complexity · 6d61dd04a15b7448195163fce892d7db MD5 · raw file

  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #include "config.h"
  26. #include <sys/types.h>
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #include <stdlib.h>
  31. #include <glib.h>
  32. #ifdef G_OS_WIN32
  33. #include <windows.h>
  34. #endif
  35. #ifdef G_OS_WIN32
  36. #define GPID_FORMAT "%p"
  37. #else
  38. #define GPID_FORMAT "%d"
  39. #endif
  40. GMainLoop *main_loop;
  41. gint alive;
  42. #ifdef G_OS_WIN32
  43. char *argv0;
  44. #endif
  45. GPid
  46. get_a_child (gint ttl)
  47. {
  48. GPid pid;
  49. #ifdef G_OS_WIN32
  50. STARTUPINFO si;
  51. PROCESS_INFORMATION pi;
  52. gchar *cmdline;
  53. memset (&si, 0, sizeof (si));
  54. si.cb = sizeof (&si);
  55. memset (&pi, 0, sizeof (pi));
  56. cmdline = g_strdup_printf( "child-test -c%d", ttl);
  57. if (!CreateProcess (argv0, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
  58. g_error ("CreateProcess failed: %s\n", g_win32_error_message (GetLastError ()));
  59. g_free(cmdline);
  60. CloseHandle (pi.hThread);
  61. pid = pi.hProcess;
  62. return pid;
  63. #else
  64. pid = fork ();
  65. if (pid < 0)
  66. exit (1);
  67. if (pid > 0)
  68. return pid;
  69. sleep (ttl);
  70. _exit (0);
  71. #endif /* G_OS_WIN32 */
  72. }
  73. gboolean
  74. child_watch_callback (GPid pid, gint status, gpointer data)
  75. {
  76. #ifdef VERBOSE
  77. gint ttl = GPOINTER_TO_INT (data);
  78. g_print ("child " GPID_FORMAT " (ttl %d) exited, status %d\n", pid, ttl, status);
  79. #endif
  80. g_spawn_close_pid (pid);
  81. if (--alive == 0)
  82. g_main_loop_quit (main_loop);
  83. return TRUE;
  84. }
  85. static gboolean
  86. quit_loop (gpointer data)
  87. {
  88. GMainLoop *main_loop = data;
  89. g_main_loop_quit (main_loop);
  90. return TRUE;
  91. }
  92. #ifdef TEST_THREAD
  93. static gpointer
  94. test_thread (gpointer data)
  95. {
  96. GMainLoop *new_main_loop;
  97. GSource *source;
  98. GPid pid;
  99. gint ttl = GPOINTER_TO_INT (data);
  100. new_main_loop = g_main_loop_new (NULL, FALSE);
  101. pid = get_a_child (ttl);
  102. source = g_child_watch_source_new (pid);
  103. g_source_set_callback (source, (GSourceFunc) child_watch_callback, data, NULL);
  104. g_source_attach (source, g_main_loop_get_context (new_main_loop));
  105. g_source_unref (source);
  106. #ifdef VERBOSE
  107. g_print ("whee! created pid: " GPID_FORMAT " (ttl %d)\n", pid, ttl);
  108. #endif
  109. g_main_loop_run (new_main_loop);
  110. return NULL;
  111. }
  112. #endif
  113. int
  114. main (int argc, char *argv[])
  115. {
  116. #ifndef TEST_THREAD
  117. GPid pid;
  118. #endif
  119. #ifdef G_OS_WIN32
  120. argv0 = argv[0];
  121. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'c')
  122. {
  123. int ttl = atoi (argv[1] + 2);
  124. Sleep (ttl * 1000);
  125. /* Exit on purpose with STILL_ACTIVE (which isn't a very common
  126. * exit status) to verify that g_child_watch_check() in gmain.c
  127. * doesn't believe a child still to be active if it happens to
  128. * exit with that status.
  129. */
  130. exit (STILL_ACTIVE);
  131. }
  132. #endif
  133. /* Only run the test, if threads are enabled and a default thread
  134. * implementation is available.
  135. */
  136. #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
  137. #ifdef TEST_THREAD
  138. g_thread_init (NULL);
  139. #endif
  140. main_loop = g_main_loop_new (NULL, FALSE);
  141. #ifdef G_OS_WIN32
  142. system ("ipconfig /all");
  143. #else
  144. system ("/bin/true");
  145. #endif
  146. alive = 2;
  147. g_timeout_add (30000, quit_loop, main_loop);
  148. #ifdef TEST_THREAD
  149. g_thread_create (test_thread, GINT_TO_POINTER (10), FALSE, NULL);
  150. g_thread_create (test_thread, GINT_TO_POINTER (20), FALSE, NULL);
  151. #else
  152. pid = get_a_child (10);
  153. g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
  154. GINT_TO_POINTER (10));
  155. pid = get_a_child (20);
  156. g_child_watch_add (pid, (GChildWatchFunc) child_watch_callback,
  157. GINT_TO_POINTER (20));
  158. #endif
  159. g_main_loop_run (main_loop);
  160. if (alive > 0)
  161. {
  162. g_warning ("%d children still alive\n", alive);
  163. return 1;
  164. }
  165. #endif
  166. return 0;
  167. }