blob: 5d57291d33a71df91c627069595adaea6fa0d46b [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 Cespedes96935a91997-08-09 23:45:39 +020018
Juan Cespedes1fe93d51998-03-13 00:29:21 +010019char * command = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +010020struct process * list_of_processes = NULL;
Juan Cespedes24c82531997-06-25 00:02:58 +020021
Juan Cespedes28f60191998-04-12 00:04:39 +020022int exiting=0; /* =1 if a SIGINT or SIGTERM has been received */
23
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020024static void normal_exit(void);
25static void signal_exit(int);
Juan Cespedes28f60191998-04-12 00:04:39 +020026static pid_t my_pid;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020027
Juan Cespedes1c2be911997-06-09 01:12:01 +020028int main(int argc, char **argv)
29{
Juan Cespedes28f60191998-04-12 00:04:39 +020030 struct opt_p_t * opt_p_tmp;
31
32 my_pid = getpid();
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020033 atexit(normal_exit);
34 signal(SIGINT,signal_exit); /* Detach processes when interrupted */
35 signal(SIGTERM,signal_exit); /* ... or killed */
36
Juan Cespedes5e01f651998-03-08 22:31:44 +010037 argv = process_options(argc, argv);
Juan Cespedes5e01f651998-03-08 22:31:44 +010038 read_config_file("/etc/ltrace.conf");
39 if (getenv("HOME")) {
40 char path[PATH_MAX];
41 sprintf(path, getenv("HOME")); /* FIXME: buffer overrun */
42 strcat(path, "/.ltrace.conf");
43 read_config_file(path);
Juan Cespedes23658aa1997-08-27 22:27:36 +020044 }
Juan Cespedesac3db291998-04-25 14:31:58 +020045 if (opt_d && opt_e) {
46 struct opt_e_t * tmp = opt_e;
47 while(tmp) {
48 printf("Option -e: %s\n", tmp->name);
49 tmp = tmp->next;
50 }
51 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010052 if (command) {
53 execute_program(open_program(command), argv);
54 }
Juan Cespedes28f60191998-04-12 00:04:39 +020055 opt_p_tmp = opt_p;
56 while (opt_p_tmp) {
57 open_pid(opt_p_tmp->pid, 1);
58 opt_p_tmp = opt_p_tmp->next;
59 }
Juan Cespedes1c2be911997-06-09 01:12:01 +020060 while(1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010061 process_event(wait_for_something());
62 }
63}
64
Juan Cespedes28f60191998-04-12 00:04:39 +020065static void signal_alarm(int sig)
66{
67 struct process * tmp = list_of_processes;
68
69 signal(SIGALRM,SIG_DFL);
70 while(tmp) {
71 struct opt_p_t * tmp2 = opt_p;
72 while(tmp2) {
73 if (tmp->pid == tmp2->pid) {
74 tmp = tmp->next;
75 if (!tmp) {
76 return;
77 }
78 break;
79 }
80 tmp2 = tmp2->next;
81 }
82 if (opt_d>1) {
83 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
84 }
85 kill(tmp->pid, SIGSTOP);
86 tmp = tmp->next;
87 }
88}
89
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020090static void signal_exit(int sig)
91{
Juan Cespedes28f60191998-04-12 00:04:39 +020092 exiting=1;
93 if (opt_d) {
94 output_line(0,"Received interrupt signal; exiting...");
95 }
96 signal(SIGINT,SIG_IGN);
97 signal(SIGTERM,SIG_IGN);
98 signal(SIGALRM,signal_alarm);
99 if (opt_p) {
100 struct opt_p_t * tmp = opt_p;
101 while(tmp) {
102 if (opt_d>1) {
103 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
104 }
105 kill(tmp->pid, SIGSTOP);
106 tmp = tmp->next;
107 }
108 }
109 alarm(1);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200110}
111
112static void normal_exit(void)
113{
Juan Cespedes28f60191998-04-12 00:04:39 +0200114 output_line(0,0);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200115}