blob: 060e2b3460d407f339c0e3a8862f842bad61ae17 [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 Cespedes8cc1b9d2002-03-01 19:54:23 +010024static void
25signal_alarm(int sig) {
Juan Cespedes28f60191998-04-12 00:04:39 +020026 struct process * tmp = list_of_processes;
27
28 signal(SIGALRM,SIG_DFL);
29 while(tmp) {
30 struct opt_p_t * tmp2 = opt_p;
31 while(tmp2) {
32 if (tmp->pid == tmp2->pid) {
33 tmp = tmp->next;
34 if (!tmp) {
35 return;
36 }
37 break;
38 }
39 tmp2 = tmp2->next;
40 }
41 if (opt_d>1) {
42 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
43 }
44 kill(tmp->pid, SIGSTOP);
45 tmp = tmp->next;
46 }
47}
48
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010049static void
50signal_exit(int sig) {
Juan Cespedes28f60191998-04-12 00:04:39 +020051 exiting=1;
52 if (opt_d) {
53 output_line(0,"Received interrupt signal; exiting...");
54 }
55 signal(SIGINT,SIG_IGN);
56 signal(SIGTERM,SIG_IGN);
57 signal(SIGALRM,signal_alarm);
58 if (opt_p) {
59 struct opt_p_t * tmp = opt_p;
60 while(tmp) {
61 if (opt_d>1) {
62 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
63 }
64 kill(tmp->pid, SIGSTOP);
65 tmp = tmp->next;
66 }
67 }
68 alarm(1);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020069}
70
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010071static void
72normal_exit(void) {
Juan Cespedes28f60191998-04-12 00:04:39 +020073 output_line(0,0);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020074}
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020075
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010076int
77main(int argc, char **argv) {
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020078 struct opt_p_t * opt_p_tmp;
79
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);
85 read_config_file("/etc/ltrace.conf");
86 if (getenv("HOME")) {
87 char path[PATH_MAX];
88 sprintf(path, getenv("HOME")); /* FIXME: buffer overrun */
89 strcat(path, "/.ltrace.conf");
90 read_config_file(path);
91 }
92 if (opt_d && opt_e) {
93 struct opt_e_t * tmp = opt_e;
94 while(tmp) {
95 printf("Option -e: %s\n", tmp->name);
96 tmp = tmp->next;
97 }
98 }
99 if (command) {
100 execute_program(open_program(command), argv);
101 }
102 opt_p_tmp = opt_p;
103 while (opt_p_tmp) {
104 open_pid(opt_p_tmp->pid, 1);
105 opt_p_tmp = opt_p_tmp->next;
106 }
107 while(1) {
108 process_event(wait_for_something());
109 }
110}