| 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 | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 13 | #include <sys/ioctl.h> |
| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame] | 14 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 15 | #include "ltrace.h" |
| Juan Cespedes | 3268a16 | 1997-08-25 16:45:22 +0200 | [diff] [blame] | 16 | #include "output.h" |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 17 | #include "read_config_file.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 18 | #include "options.h" |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 19 | #include "debug.h" |
| Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 20 | |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 21 | #ifndef SYSCONFDIR |
| 22 | #define SYSCONFDIR "/etc" |
| 23 | #endif |
| 24 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 25 | char * command = NULL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 26 | struct process * list_of_processes = NULL; |
| Juan Cespedes | 24c8253 | 1997-06-25 00:02:58 +0200 | [diff] [blame] | 27 | |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 28 | int exiting=0; /* =1 if a SIGINT or SIGTERM has been received */ |
| 29 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 30 | static void |
| 31 | signal_alarm(int sig) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 32 | struct process * tmp = list_of_processes; |
| 33 | |
| 34 | signal(SIGALRM,SIG_DFL); |
| 35 | while(tmp) { |
| 36 | struct opt_p_t * tmp2 = opt_p; |
| 37 | while(tmp2) { |
| 38 | if (tmp->pid == tmp2->pid) { |
| 39 | tmp = tmp->next; |
| 40 | if (!tmp) { |
| 41 | return; |
| 42 | } |
| 43 | break; |
| 44 | } |
| 45 | tmp2 = tmp2->next; |
| 46 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 47 | debug(2,"Sending SIGSTOP to process %u\n",tmp->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 48 | kill(tmp->pid, SIGSTOP); |
| 49 | tmp = tmp->next; |
| 50 | } |
| 51 | } |
| 52 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 53 | static void |
| 54 | signal_exit(int sig) { |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 55 | exiting=1; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 56 | debug(1,"Received interrupt signal; exiting..."); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 57 | signal(SIGINT,SIG_IGN); |
| 58 | signal(SIGTERM,SIG_IGN); |
| 59 | signal(SIGALRM,signal_alarm); |
| 60 | if (opt_p) { |
| 61 | struct opt_p_t * tmp = opt_p; |
| 62 | while(tmp) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 63 | debug(2,"Sending SIGSTOP to process %u\n",tmp->pid); |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 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 | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame^] | 74 | if (opt_c) { |
| 75 | summary(); |
| 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 | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 79 | static void |
| 80 | guess_cols(void) { |
| 81 | struct winsize ws; |
| 82 | char * c; |
| 83 | |
| 84 | opt_a = DEFAULT_ACOLUMN; |
| 85 | c = getenv("COLUMNS"); |
| 86 | if (c && *c) { |
| 87 | char * endptr; |
| 88 | int cols; |
| 89 | cols = strtol(c, &endptr, 0); |
| 90 | if (cols>0 && !*endptr) { |
| 91 | opt_a = cols * 5/8; |
| 92 | } |
| 93 | } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col>0) { |
| 94 | opt_a = ws.ws_col * 5/8; |
| 95 | } |
| 96 | } |
| 97 | |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 98 | int |
| 99 | main(int argc, char **argv) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 100 | struct opt_p_t * opt_p_tmp; |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 101 | char * home; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 102 | |
| 103 | atexit(normal_exit); |
| 104 | signal(SIGINT,signal_exit); /* Detach processes when interrupted */ |
| 105 | signal(SIGTERM,signal_exit); /* ... or killed */ |
| 106 | |
| Juan Cespedes | a0ccf39 | 2003-02-01 19:02:37 +0100 | [diff] [blame] | 107 | guess_cols(); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 108 | argv = process_options(argc, argv); |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 109 | read_config_file(SYSCONFDIR "/ltrace.conf"); |
| 110 | home = getenv("HOME"); |
| 111 | if (home) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 112 | char path[PATH_MAX]; |
| Juan Cespedes | f1bfe20 | 2002-03-27 00:22:23 +0100 | [diff] [blame] | 113 | if (strlen(home) > PATH_MAX-15) { |
| 114 | fprintf(stderr, "Error: $HOME too long\n"); |
| 115 | exit(1); |
| 116 | } |
| 117 | strcpy(path, getenv("HOME")); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 118 | strcat(path, "/.ltrace.conf"); |
| 119 | read_config_file(path); |
| 120 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 121 | if (opt_e) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 122 | struct opt_e_t * tmp = opt_e; |
| 123 | while(tmp) { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 124 | debug(1,"Option -e: %s\n", tmp->name); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 125 | tmp = tmp->next; |
| 126 | } |
| 127 | } |
| 128 | if (command) { |
| 129 | execute_program(open_program(command), argv); |
| 130 | } |
| 131 | opt_p_tmp = opt_p; |
| 132 | while (opt_p_tmp) { |
| 133 | open_pid(opt_p_tmp->pid, 1); |
| 134 | opt_p_tmp = opt_p_tmp->next; |
| 135 | } |
| 136 | while(1) { |
| 137 | process_event(wait_for_something()); |
| 138 | } |
| 139 | } |