Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / glib / gerror.c
blobba81b5f63788347a8643ad340f208d6ad78f8010
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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.
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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe ; except for g_on_error_stack_trace, but who wants thread safety
29 * then
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "glib.h"
41 #ifdef HAVE_SYS_TIME_H
42 #include <sys/time.h>
43 #endif
44 #ifdef HAVE_SYS_TIMES_H
45 #include <sys/times.h>
46 #endif
47 #include <sys/types.h>
49 #include <time.h>
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
56 #endif /* HAVE_SYS_SELECT_H */
58 #ifdef STDC_HEADERS
59 #include <string.h> /* for bzero on BSD systems */
60 #endif
62 #ifdef _MSC_VER
63 #include <process.h> /* For _getpid() */
64 #endif
66 #ifndef NO_FD_SET
67 # define SELECT_MASK fd_set
68 #else
69 # ifndef _AIX
70 typedef long fd_mask;
71 # endif
72 # if defined(_IBMR2)
73 # define SELECT_MASK void
74 # else
75 # define SELECT_MASK int
76 # endif
77 #endif
80 static void stack_trace (char **args);
82 extern volatile gboolean glib_on_error_halt;
83 volatile gboolean glib_on_error_halt = TRUE;
85 void
86 g_on_error_query (const gchar *prg_name)
88 static const gchar *query1 = "[E]xit, [H]alt";
89 static const gchar *query2 = ", show [S]tack trace";
90 static const gchar *query3 = " or [P]roceed";
91 gchar buf[16];
93 if (!prg_name)
94 prg_name = g_get_prgname ();
96 retry:
98 if (prg_name)
99 fprintf (stdout,
100 "%s (pid:%u): %s%s%s: ",
101 prg_name,
102 (guint) getpid (),
103 query1,
104 query2,
105 query3);
106 else
107 fprintf (stdout,
108 "(process:%u): %s%s: ",
109 (guint) getpid (),
110 query1,
111 query3);
112 fflush (stdout);
114 #ifndef NATIVE_WIN32
115 if (isatty(0) && isatty(1))
116 fgets (buf, 8, stdin);
117 else
118 strcpy (buf, "E\n");
119 #else
120 fgets (buf, 8, stdin);
121 #endif
123 if ((buf[0] == 'E' || buf[0] == 'e')
124 && buf[1] == '\n')
125 _exit (0);
126 else if ((buf[0] == 'P' || buf[0] == 'p')
127 && buf[1] == '\n')
128 return;
129 else if (prg_name
130 && (buf[0] == 'S' || buf[0] == 's')
131 && buf[1] == '\n')
133 g_on_error_stack_trace (prg_name);
134 goto retry;
136 else if ((buf[0] == 'H' || buf[0] == 'h')
137 && buf[1] == '\n')
139 while (glib_on_error_halt)
141 glib_on_error_halt = TRUE;
142 return;
144 else
145 goto retry;
148 void
149 g_on_error_stack_trace (const gchar *prg_name)
151 #ifndef NATIVE_WIN32
152 pid_t pid;
153 gchar buf[16];
154 gchar *args[4] = { "gdb", NULL, NULL, NULL };
156 if (!prg_name)
157 return;
159 sprintf (buf, "%u", (guint) getpid ());
161 args[1] = (gchar*) prg_name;
162 args[2] = buf;
164 pid = fork ();
165 if (pid == 0)
167 stack_trace (args);
168 _exit (0);
170 else if (pid == (pid_t) -1)
172 perror ("unable to fork gdb");
173 return;
176 while (glib_on_error_halt)
178 glib_on_error_halt = TRUE;
179 #else
180 abort ();
181 #endif
184 static gboolean stack_trace_done = FALSE;
186 static void
187 stack_trace_sigchld (int signum)
189 stack_trace_done = TRUE;
192 static void
193 stack_trace (char **args)
195 #ifndef NATIVE_WIN32
196 pid_t pid;
197 int in_fd[2];
198 int out_fd[2];
199 SELECT_MASK fdset;
200 SELECT_MASK readset;
201 struct timeval tv;
202 int sel, index, state;
203 char buffer[256];
204 char c;
206 stack_trace_done = FALSE;
207 signal (SIGCHLD, stack_trace_sigchld);
209 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
211 perror ("unable to open pipe");
212 _exit (0);
215 pid = fork ();
216 if (pid == 0)
218 close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
219 close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
220 close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
222 execvp (args[0], args); /* exec gdb */
223 perror ("exec failed");
224 _exit (0);
226 else if (pid == (pid_t) -1)
228 perror ("unable to fork");
229 _exit (0);
232 FD_ZERO (&fdset);
233 FD_SET (out_fd[0], &fdset);
235 write (in_fd[1], "backtrace\n", 10);
236 write (in_fd[1], "p x = 0\n", 8);
237 write (in_fd[1], "quit\n", 5);
239 index = 0;
240 state = 0;
242 while (1)
244 readset = fdset;
245 tv.tv_sec = 1;
246 tv.tv_usec = 0;
248 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
249 if (sel == -1)
250 break;
252 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
254 if (read (out_fd[0], &c, 1))
256 switch (state)
258 case 0:
259 if (c == '#')
261 state = 1;
262 index = 0;
263 buffer[index++] = c;
265 break;
266 case 1:
267 buffer[index++] = c;
268 if ((c == '\n') || (c == '\r'))
270 buffer[index] = 0;
271 fprintf (stdout, "%s", buffer);
272 state = 0;
273 index = 0;
275 break;
276 default:
277 break;
281 else if (stack_trace_done)
282 break;
285 close (in_fd[0]);
286 close (in_fd[1]);
287 close (out_fd[0]);
288 close (out_fd[1]);
289 _exit (0);
290 #else
291 abort ();
292 #endif