blob: bb7eef84ae991896dddde01ff1c80012c9dfdd3c [file] [log] [blame]
Juan Cespedes1c2be911997-06-09 01:12:01 +02001#include <stdio.h>
Juan Cespedes23658aa1997-08-27 22:27:36 +02002#include <stdlib.h>
Juan Cespedes1c2be911997-06-09 01:12:01 +02003#include <unistd.h>
Juan Cespedes23658aa1997-08-27 22:27:36 +02004#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include <errno.h>
6#include <sys/param.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +02007#include <signal.h>
Juan Cespedes28f60191998-04-12 00:04:39 +02008#include <sys/wait.h>
Juan Cespedes1c2be911997-06-09 01:12:01 +02009
Juan Cespedes5e01f651998-03-08 22:31:44 +010010#include "ltrace.h"
Juan Cespedes3268a161997-08-25 16:45:22 +020011#include "output.h"
Juan Cespedes28f60191998-04-12 00:04:39 +020012#include "read_config_file.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010013#include "options.h"
Juan Cespedes96935a91997-08-09 23:45:39 +020014
Juan Cespedes1fe93d51998-03-13 00:29:21 +010015char * command = NULL;
Juan Cespedes5e01f651998-03-08 22:31:44 +010016struct process * list_of_processes = NULL;
Juan Cespedes24c82531997-06-25 00:02:58 +020017
Juan Cespedes28f60191998-04-12 00:04:39 +020018int exiting=0; /* =1 if a SIGINT or SIGTERM has been received */
19
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020020static void normal_exit(void);
21static void signal_exit(int);
Juan Cespedes28f60191998-04-12 00:04:39 +020022static pid_t my_pid;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020023
Juan Cespedes1c2be911997-06-09 01:12:01 +020024int main(int argc, char **argv)
25{
Juan Cespedes28f60191998-04-12 00:04:39 +020026 struct opt_p_t * opt_p_tmp;
27
28 my_pid = getpid();
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020029 atexit(normal_exit);
30 signal(SIGINT,signal_exit); /* Detach processes when interrupted */
31 signal(SIGTERM,signal_exit); /* ... or killed */
32
Juan Cespedes5e01f651998-03-08 22:31:44 +010033 argv = process_options(argc, argv);
Juan Cespedes5e01f651998-03-08 22:31:44 +010034 read_config_file("/etc/ltrace.conf");
35 if (getenv("HOME")) {
36 char path[PATH_MAX];
37 sprintf(path, getenv("HOME")); /* FIXME: buffer overrun */
38 strcat(path, "/.ltrace.conf");
39 read_config_file(path);
Juan Cespedes23658aa1997-08-27 22:27:36 +020040 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010041 if (command) {
42 execute_program(open_program(command), argv);
43 }
Juan Cespedes28f60191998-04-12 00:04:39 +020044 opt_p_tmp = opt_p;
45 while (opt_p_tmp) {
46 open_pid(opt_p_tmp->pid, 1);
47 opt_p_tmp = opt_p_tmp->next;
48 }
Juan Cespedes1c2be911997-06-09 01:12:01 +020049 while(1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010050 process_event(wait_for_something());
51 }
52}
53
Juan Cespedes28f60191998-04-12 00:04:39 +020054static void signal_alarm(int sig)
55{
56 struct process * tmp = list_of_processes;
57
58 signal(SIGALRM,SIG_DFL);
59 while(tmp) {
60 struct opt_p_t * tmp2 = opt_p;
61 while(tmp2) {
62 if (tmp->pid == tmp2->pid) {
63 tmp = tmp->next;
64 if (!tmp) {
65 return;
66 }
67 break;
68 }
69 tmp2 = tmp2->next;
70 }
71 if (opt_d>1) {
72 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
73 }
74 kill(tmp->pid, SIGSTOP);
75 tmp = tmp->next;
76 }
77}
78
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020079static void signal_exit(int sig)
80{
Juan Cespedes28f60191998-04-12 00:04:39 +020081 exiting=1;
82 if (opt_d) {
83 output_line(0,"Received interrupt signal; exiting...");
84 }
85 signal(SIGINT,SIG_IGN);
86 signal(SIGTERM,SIG_IGN);
87 signal(SIGALRM,signal_alarm);
88 if (opt_p) {
89 struct opt_p_t * tmp = opt_p;
90 while(tmp) {
91 if (opt_d>1) {
92 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
93 }
94 kill(tmp->pid, SIGSTOP);
95 tmp = tmp->next;
96 }
97 }
98 alarm(1);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020099}
100
101static void normal_exit(void)
102{
Juan Cespedes28f60191998-04-12 00:04:39 +0200103 output_line(0,0);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200104}