| 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 | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 18 | #include "debug.h" |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 19 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 20 | char *command = NULL; |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame^] | 21 | Process *list_of_processes = NULL; |
| Juan Cespedes | 24c8253 | 1997-06-25 00:02:58 +0200 | [diff] [blame] | 22 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 23 | int exiting = 0; /* =1 if a SIGINT or SIGTERM has been received */ |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 24 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 25 | static void |
| 26 | signal_alarm(int sig) { |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame^] | 27 | Process *tmp = list_of_processes; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 28 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 29 | signal(SIGALRM, SIG_DFL); |
| 30 | while (tmp) { |
| 31 | struct opt_p_t *tmp2 = opt_p; |
| 32 | while (tmp2) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 33 | if (tmp->pid == tmp2->pid) { |
| 34 | tmp = tmp->next; |
| 35 | if (!tmp) { |
| 36 | return; |
| 37 | } |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 38 | tmp2 = opt_p; |
| 39 | continue; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 40 | } |
| 41 | tmp2 = tmp2->next; |
| 42 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 43 | debug(2, "Sending SIGSTOP to process %u\n", tmp->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 44 | kill(tmp->pid, SIGSTOP); |
| 45 | tmp = tmp->next; |
| 46 | } |
| 47 | } |
| 48 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 49 | static void |
| 50 | signal_exit(int sig) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 51 | exiting = 1; |
| 52 | debug(1, "Received interrupt signal; exiting..."); |
| 53 | signal(SIGINT, SIG_IGN); |
| 54 | signal(SIGTERM, SIG_IGN); |
| 55 | signal(SIGALRM, signal_alarm); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 56 | if (opt_p) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 57 | struct opt_p_t *tmp = opt_p; |
| 58 | while (tmp) { |
| 59 | debug(2, "Sending SIGSTOP to process %u\n", tmp->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 60 | kill(tmp->pid, SIGSTOP); |
| 61 | tmp = tmp->next; |
| 62 | } |
| 63 | } |
| 64 | alarm(1); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 67 | static void |
| 68 | normal_exit(void) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 69 | output_line(0, 0); |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 70 | if (options.summary) { |
| Juan Cespedes | 504a385 | 2003-02-04 23:24:38 +0100 | [diff] [blame] | 71 | show_summary(); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 72 | } |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 73 | if (options.output) { |
| 74 | fclose(options.output); |
| 75 | options.output = NULL; |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 76 | } |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 77 | } |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 78 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 79 | int |
| 80 | main(int argc, char **argv) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 81 | struct opt_p_t *opt_p_tmp; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 82 | |
| 83 | atexit(normal_exit); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 84 | signal(SIGINT, signal_exit); /* Detach processes when interrupted */ |
| 85 | signal(SIGTERM, signal_exit); /* ... or killed */ |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 86 | |
| 87 | argv = process_options(argc, argv); |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 88 | while (opt_F) { |
| 89 | /* If filename begins with ~, expand it to the user's home */ |
| 90 | /* directory. This does not correctly handle ~yoda, but that */ |
| 91 | /* isn't as bad as it seems because the shell will normally */ |
| 92 | /* be doing the expansion for us; only the hardcoded */ |
| 93 | /* ~/.ltrace.conf should ever use this code. */ |
| 94 | if (opt_F->filename[0] == '~') { |
| 95 | char path[PATH_MAX]; |
| 96 | char *home_dir = getenv("HOME"); |
| Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 97 | if (home_dir) { |
| 98 | strncpy(path, home_dir, PATH_MAX - 1); |
| 99 | path[PATH_MAX - 1] = '\0'; |
| 100 | strncat(path, opt_F->filename + 1, |
| 101 | PATH_MAX - strlen(path) - 1); |
| 102 | read_config_file(path); |
| 103 | } |
| Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 104 | } else { |
| 105 | read_config_file(opt_F->filename); |
| 106 | } |
| 107 | opt_F = opt_F->next; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 108 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 109 | if (opt_e) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | struct opt_e_t *tmp = opt_e; |
| 111 | while (tmp) { |
| 112 | debug(1, "Option -e: %s\n", tmp->name); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 113 | tmp = tmp->next; |
| 114 | } |
| 115 | } |
| 116 | if (command) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 117 | execute_program(open_program(command, 0), argv); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 118 | } |
| 119 | opt_p_tmp = opt_p; |
| 120 | while (opt_p_tmp) { |
| 121 | open_pid(opt_p_tmp->pid, 1); |
| 122 | opt_p_tmp = opt_p_tmp->next; |
| 123 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 124 | while (1) { |
| Juan Cespedes | e2023f7 | 2009-04-07 12:12:08 +0200 | [diff] [blame] | 125 | process_event(next_event()); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 126 | } |
| 127 | } |