blob: fbd6ca45a98e301a485b6e87dbc88ec7287a1145 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes1c2be911997-06-09 01:12:01 +02005#include <stdio.h>
Juan Cespedes23658aa1997-08-27 22:27:36 +02006#include <stdlib.h>
Juan Cespedes1c2be911997-06-09 01:12:01 +02007#include <unistd.h>
Juan Cespedes23658aa1997-08-27 22:27:36 +02008#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <errno.h>
10#include <sys/param.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020011#include <signal.h>
Juan Cespedes28f60191998-04-12 00:04:39 +020012#include <sys/wait.h>
Juan Cespedes1c2be911997-06-09 01:12:01 +020013
Juan Cespedes5e01f651998-03-08 22:31:44 +010014#include "ltrace.h"
Juan Cespedes3268a161997-08-25 16:45:22 +020015#include "output.h"
Juan Cespedes28f60191998-04-12 00:04:39 +020016#include "read_config_file.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010017#include "options.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010018#include "debug.h"
Juan Cespedes96935a91997-08-09 23:45:39 +020019
Juan Cespedesf1bfe202002-03-27 00:22:23 +010020#ifndef SYSCONFDIR
21#define SYSCONFDIR "/etc"
22#endif
23
Juan Cespedes1fe93d51998-03-13 00:29:21 +010024char * command = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +010025struct process * list_of_processes = NULL;
Juan Cespedes24c82531997-06-25 00:02:58 +020026
Juan Cespedes28f60191998-04-12 00:04:39 +020027int exiting=0; /* =1 if a SIGINT or SIGTERM has been received */
28
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010029static void
30signal_alarm(int sig) {
Juan Cespedes28f60191998-04-12 00:04:39 +020031 struct process * tmp = list_of_processes;
32
33 signal(SIGALRM,SIG_DFL);
34 while(tmp) {
35 struct opt_p_t * tmp2 = opt_p;
36 while(tmp2) {
37 if (tmp->pid == tmp2->pid) {
38 tmp = tmp->next;
39 if (!tmp) {
40 return;
41 }
42 break;
43 }
44 tmp2 = tmp2->next;
45 }
Juan Cespedescac15c32003-01-31 18:58:58 +010046 debug(2,"Sending SIGSTOP to process %u\n",tmp->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +020047 kill(tmp->pid, SIGSTOP);
48 tmp = tmp->next;
49 }
50}
51
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010052static void
53signal_exit(int sig) {
Juan Cespedes28f60191998-04-12 00:04:39 +020054 exiting=1;
Juan Cespedescac15c32003-01-31 18:58:58 +010055 debug(1,"Received interrupt signal; exiting...");
Juan Cespedes28f60191998-04-12 00:04:39 +020056 signal(SIGINT,SIG_IGN);
57 signal(SIGTERM,SIG_IGN);
58 signal(SIGALRM,signal_alarm);
59 if (opt_p) {
60 struct opt_p_t * tmp = opt_p;
61 while(tmp) {
Juan Cespedescac15c32003-01-31 18:58:58 +010062 debug(2,"Sending SIGSTOP to process %u\n",tmp->pid);
Juan Cespedes28f60191998-04-12 00:04:39 +020063 kill(tmp->pid, SIGSTOP);
64 tmp = tmp->next;
65 }
66 }
67 alarm(1);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020068}
69
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010070static void
71normal_exit(void) {
Juan Cespedes28f60191998-04-12 00:04:39 +020072 output_line(0,0);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020073}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020074
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010075int
76main(int argc, char **argv) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020077 struct opt_p_t * opt_p_tmp;
Juan Cespedesf1bfe202002-03-27 00:22:23 +010078 char * home;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020079
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);
Juan Cespedesf1bfe202002-03-27 00:22:23 +010085 read_config_file(SYSCONFDIR "/ltrace.conf");
86 home = getenv("HOME");
87 if (home) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020088 char path[PATH_MAX];
Juan Cespedesf1bfe202002-03-27 00:22:23 +010089 if (strlen(home) > PATH_MAX-15) {
90 fprintf(stderr, "Error: $HOME too long\n");
91 exit(1);
92 }
93 strcpy(path, getenv("HOME"));
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020094 strcat(path, "/.ltrace.conf");
95 read_config_file(path);
96 }
Juan Cespedescac15c32003-01-31 18:58:58 +010097 if (opt_e) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020098 struct opt_e_t * tmp = opt_e;
99 while(tmp) {
Juan Cespedescac15c32003-01-31 18:58:58 +0100100 debug(1,"Option -e: %s\n", tmp->name);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200101 tmp = tmp->next;
102 }
103 }
104 if (command) {
105 execute_program(open_program(command), argv);
106 }
107 opt_p_tmp = opt_p;
108 while (opt_p_tmp) {
109 open_pid(opt_p_tmp->pid, 1);
110 opt_p_tmp = opt_p_tmp->next;
111 }
112 while(1) {
113 process_event(wait_for_something());
114 }
115}