blob: ebca3589cb41a113fecd2f9b30d10075c23513ad [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 Cespedesac3db291998-04-25 14:31:58 +020041 if (opt_d && opt_e) {
42 struct opt_e_t * tmp = opt_e;
43 while(tmp) {
44 printf("Option -e: %s\n", tmp->name);
45 tmp = tmp->next;
46 }
47 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010048 if (command) {
49 execute_program(open_program(command), argv);
50 }
Juan Cespedes28f60191998-04-12 00:04:39 +020051 opt_p_tmp = opt_p;
52 while (opt_p_tmp) {
53 open_pid(opt_p_tmp->pid, 1);
54 opt_p_tmp = opt_p_tmp->next;
55 }
Juan Cespedes1c2be911997-06-09 01:12:01 +020056 while(1) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010057 process_event(wait_for_something());
58 }
59}
60
Juan Cespedes28f60191998-04-12 00:04:39 +020061static void signal_alarm(int sig)
62{
63 struct process * tmp = list_of_processes;
64
65 signal(SIGALRM,SIG_DFL);
66 while(tmp) {
67 struct opt_p_t * tmp2 = opt_p;
68 while(tmp2) {
69 if (tmp->pid == tmp2->pid) {
70 tmp = tmp->next;
71 if (!tmp) {
72 return;
73 }
74 break;
75 }
76 tmp2 = tmp2->next;
77 }
78 if (opt_d>1) {
79 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
80 }
81 kill(tmp->pid, SIGSTOP);
82 tmp = tmp->next;
83 }
84}
85
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020086static void signal_exit(int sig)
87{
Juan Cespedes28f60191998-04-12 00:04:39 +020088 exiting=1;
89 if (opt_d) {
90 output_line(0,"Received interrupt signal; exiting...");
91 }
92 signal(SIGINT,SIG_IGN);
93 signal(SIGTERM,SIG_IGN);
94 signal(SIGALRM,signal_alarm);
95 if (opt_p) {
96 struct opt_p_t * tmp = opt_p;
97 while(tmp) {
98 if (opt_d>1) {
99 output_line(0,"Sending SIGSTOP to process %u\n",tmp->pid);
100 }
101 kill(tmp->pid, SIGSTOP);
102 tmp = tmp->next;
103 }
104 }
105 alarm(1);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200106}
107
108static void normal_exit(void)
109{
Juan Cespedes28f60191998-04-12 00:04:39 +0200110 output_line(0,0);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +0200111}