Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 1 | #include "config.h" |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <unistd.h> |
| 6 | #include <string.h> |
| 7 | #include <errno.h> |
| 8 | #include <sys/param.h> |
| 9 | #include <signal.h> |
| 10 | #include <sys/wait.h> |
| 11 | |
| 12 | #include "common.h" |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 13 | |
| 14 | char *command = NULL; |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 15 | |
| 16 | int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */ |
| 17 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 18 | static enum pcb_status |
| 19 | stop_non_p_processes (Process * proc, void * data) |
| 20 | { |
| 21 | int stop = 1; |
| 22 | |
| 23 | struct opt_p_t *it; |
| 24 | for (it = opt_p; it != NULL; it = it->next) { |
| 25 | Process * p_proc = pid2proc(it->pid); |
| 26 | if (p_proc == NULL) { |
| 27 | printf("stop_non_p_processes: %d terminated?\n", it->pid); |
| 28 | continue; |
| 29 | } |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 30 | if (p_proc == proc || p_proc->leader == proc->leader) { |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 31 | stop = 0; |
| 32 | break; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if (stop) { |
| 37 | debug(2, "Sending SIGSTOP to process %u", proc->pid); |
| 38 | kill(proc->pid, SIGSTOP); |
| 39 | } |
| 40 | |
| 41 | return pcb_cont; |
| 42 | } |
| 43 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 44 | static void |
| 45 | signal_alarm(int sig) { |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 46 | signal(SIGALRM, SIG_DFL); |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 47 | each_process(NULL, &stop_non_p_processes, NULL); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void |
| 51 | signal_exit(int sig) { |
| 52 | exiting = 1; |
| 53 | debug(1, "Received interrupt signal; exiting..."); |
| 54 | signal(SIGINT, SIG_IGN); |
| 55 | signal(SIGTERM, SIG_IGN); |
| 56 | signal(SIGALRM, signal_alarm); |
| 57 | if (opt_p) { |
| 58 | struct opt_p_t *tmp = opt_p; |
| 59 | while (tmp) { |
| 60 | debug(2, "Sending SIGSTOP to process %u\n", tmp->pid); |
| 61 | kill(tmp->pid, SIGSTOP); |
| 62 | tmp = tmp->next; |
| 63 | } |
| 64 | } |
| 65 | alarm(1); |
| 66 | } |
| 67 | |
| 68 | static void |
| 69 | normal_exit(void) { |
| 70 | output_line(0, 0); |
| 71 | if (options.summary) { |
| 72 | show_summary(); |
| 73 | } |
| 74 | if (options.output) { |
| 75 | fclose(options.output); |
| 76 | options.output = NULL; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | ltrace_init(int argc, char **argv) { |
| 82 | struct opt_p_t *opt_p_tmp; |
| 83 | |
| 84 | atexit(normal_exit); |
| 85 | signal(SIGINT, signal_exit); /* Detach processes when interrupted */ |
| 86 | signal(SIGTERM, signal_exit); /* ... or killed */ |
| 87 | |
| 88 | argv = process_options(argc, argv); |
| 89 | while (opt_F) { |
| 90 | /* If filename begins with ~, expand it to the user's home */ |
| 91 | /* directory. This does not correctly handle ~yoda, but that */ |
| 92 | /* isn't as bad as it seems because the shell will normally */ |
| 93 | /* be doing the expansion for us; only the hardcoded */ |
| 94 | /* ~/.ltrace.conf should ever use this code. */ |
| 95 | if (opt_F->filename[0] == '~') { |
| 96 | char path[PATH_MAX]; |
| 97 | char *home_dir = getenv("HOME"); |
| 98 | if (home_dir) { |
| 99 | strncpy(path, home_dir, PATH_MAX - 1); |
| 100 | path[PATH_MAX - 1] = '\0'; |
| 101 | strncat(path, opt_F->filename + 1, |
| 102 | PATH_MAX - strlen(path) - 1); |
| 103 | read_config_file(path); |
| 104 | } |
| 105 | } else { |
| 106 | read_config_file(opt_F->filename); |
| 107 | } |
| 108 | opt_F = opt_F->next; |
| 109 | } |
| 110 | if (opt_e) { |
| 111 | struct opt_e_t *tmp = opt_e; |
| 112 | while (tmp) { |
| 113 | debug(1, "Option -e: %s\n", tmp->name); |
| 114 | tmp = tmp->next; |
| 115 | } |
| 116 | } |
| 117 | if (command) { |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame] | 118 | open_program(command, execute_program(command, argv), 0); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 119 | } |
| 120 | opt_p_tmp = opt_p; |
| 121 | while (opt_p_tmp) { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 122 | open_pid(opt_p_tmp->pid); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 123 | opt_p_tmp = opt_p_tmp->next; |
| 124 | } |
| 125 | } |
| 126 | |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 127 | static int num_ltrace_callbacks[EVENT_MAX]; |
| 128 | static callback_func * ltrace_callbacks[EVENT_MAX]; |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 129 | |
| 130 | void |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 131 | ltrace_add_callback(callback_func func, Event_type type) { |
| 132 | ltrace_callbacks[type] = realloc(ltrace_callbacks[type], (num_ltrace_callbacks[type]+1)*sizeof(callback_func)); |
| 133 | ltrace_callbacks[type][num_ltrace_callbacks[type]++] = func; |
| 134 | } |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 135 | |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 136 | static void |
| 137 | dispatch_callbacks(Event * ev) { |
| 138 | int i; |
| 139 | /* Ignoring case 1: signal into a dying tracer */ |
| 140 | if (ev->type==EVENT_SIGNAL && |
| 141 | exiting && ev->e_un.signum == SIGSTOP) { |
| 142 | return; |
| 143 | } |
| 144 | /* Ignoring case 2: process being born before a clone event */ |
| 145 | if (ev->proc && ev->proc->state == STATE_IGNORED) { |
| 146 | return; |
| 147 | } |
| 148 | for (i=0; i<num_ltrace_callbacks[ev->type]; i++) { |
| 149 | ltrace_callbacks[ev->type][i](ev); |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 153 | void |
| 154 | ltrace_main(void) { |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 155 | Event * ev; |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 156 | while (1) { |
Juan Cespedes | 40dc635 | 2009-06-25 19:54:10 +0200 | [diff] [blame] | 157 | ev = next_event(); |
Juan Cespedes | 61da337 | 2009-07-03 11:55:44 +0200 | [diff] [blame] | 158 | dispatch_callbacks(ev); |
Juan Cespedes | 03192f8 | 2009-07-03 10:16:22 +0200 | [diff] [blame] | 159 | handle_event(ev); |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 160 | } |
| 161 | } |