| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame] | 5 | #include <stdio.h> |
| Juan Cespedes | 23658aa | 1997-08-27 22:27:36 +0200 | [diff] [blame] | 6 | #include <stdlib.h> |
| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame] | 7 | #include <unistd.h> |
| Juan Cespedes | 23658aa | 1997-08-27 22:27:36 +0200 | [diff] [blame] | 8 | #include <string.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <sys/param.h> |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 11 | #include <signal.h> |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 12 | #include <sys/wait.h> |
| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame] | 13 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 14 | #include "ltrace.h" |
| Juan Cespedes | 3268a16 | 1997-08-25 16:45:22 +0200 | [diff] [blame] | 15 | #include "output.h" |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 16 | #include "read_config_file.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 17 | #include "options.h" |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 18 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 19 | char * command = NULL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 20 | struct process * list_of_processes = NULL; |
| Juan Cespedes | 24c8253 | 1997-06-25 00:02:58 +0200 | [diff] [blame] | 21 | |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 22 | int exiting=0; /* =1 if a SIGINT or SIGTERM has been received */ |
| 23 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 24 | static void |
| 25 | signal_alarm(int sig) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 26 | struct process * tmp = list_of_processes; |
| 27 | |
| 28 | signal(SIGALRM,SIG_DFL); |
| 29 | while(tmp) { |
| 30 | struct opt_p_t * tmp2 = opt_p; |
| 31 | while(tmp2) { |
| 32 | if (tmp->pid == tmp2->pid) { |
| 33 | tmp = tmp->next; |
| 34 | if (!tmp) { |
| 35 | return; |
| 36 | } |
| 37 | break; |
| 38 | } |
| 39 | tmp2 = tmp2->next; |
| 40 | } |
| 41 | if (opt_d>1) { |
| 42 | output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid); |
| 43 | } |
| 44 | kill(tmp->pid, SIGSTOP); |
| 45 | tmp = tmp->next; |
| 46 | } |
| 47 | } |
| 48 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 49 | static void |
| 50 | signal_exit(int sig) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 51 | exiting=1; |
| 52 | if (opt_d) { |
| 53 | output_line(0,"Received interrupt signal; exiting..."); |
| 54 | } |
| 55 | signal(SIGINT,SIG_IGN); |
| 56 | signal(SIGTERM,SIG_IGN); |
| 57 | signal(SIGALRM,signal_alarm); |
| 58 | if (opt_p) { |
| 59 | struct opt_p_t * tmp = opt_p; |
| 60 | while(tmp) { |
| 61 | if (opt_d>1) { |
| 62 | output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid); |
| 63 | } |
| 64 | kill(tmp->pid, SIGSTOP); |
| 65 | tmp = tmp->next; |
| 66 | } |
| 67 | } |
| 68 | alarm(1); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 71 | static void |
| 72 | normal_exit(void) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 73 | output_line(0,0); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 74 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 75 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 76 | int |
| 77 | main(int argc, char **argv) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 78 | struct opt_p_t * opt_p_tmp; |
| 79 | |
| 80 | atexit(normal_exit); |
| 81 | signal(SIGINT,signal_exit); /* Detach processes when interrupted */ |
| 82 | signal(SIGTERM,signal_exit); /* ... or killed */ |
| 83 | |
| 84 | argv = process_options(argc, argv); |
| 85 | read_config_file("/etc/ltrace.conf"); |
| 86 | if (getenv("HOME")) { |
| 87 | char path[PATH_MAX]; |
| 88 | sprintf(path, getenv("HOME")); /* FIXME: buffer overrun */ |
| 89 | strcat(path, "/.ltrace.conf"); |
| 90 | read_config_file(path); |
| 91 | } |
| 92 | if (opt_d && opt_e) { |
| 93 | struct opt_e_t * tmp = opt_e; |
| 94 | while(tmp) { |
| 95 | printf("Option -e: %s\n", tmp->name); |
| 96 | tmp = tmp->next; |
| 97 | } |
| 98 | } |
| 99 | if (command) { |
| 100 | execute_program(open_program(command), argv); |
| 101 | } |
| 102 | opt_p_tmp = opt_p; |
| 103 | while (opt_p_tmp) { |
| 104 | open_pid(opt_p_tmp->pid, 1); |
| 105 | opt_p_tmp = opt_p_tmp->next; |
| 106 | } |
| 107 | while(1) { |
| 108 | process_event(wait_for_something()); |
| 109 | } |
| 110 | } |