Imported Upstream version 2.3
[ossec-hids.git] / src / logcollector / read_command.c
1 /* @(#) $Id: read_command.c,v 1.3 2009/11/05 19:55:34 dcid Exp $ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 3) as published by the FSF - Free Software
9  * Foundation
10  */
11
12 /* Read the syslog */
13
14
15 #include "shared.h"
16 #include "logcollector.h"
17
18
19
20 /* Read Output of commands */
21 void *read_command(int pos, int *rc, int drop_it)
22 {
23     int cmd_size = 0;
24     char *p;
25     char str[OS_MAXSTR+1];
26
27     FILE *cmd_output;
28
29     str[OS_MAXSTR]= '\0';
30     *rc = 0;
31
32
33     debug2("%s: DEBUG: Running command '%s'", ARGV0, logff[pos].command);
34
35
36     cmd_output = popen(logff[pos].command, "r");
37     if(!cmd_output)
38     {
39         merror("%s: ERROR: Unable to execute command: '%s'.",
40                ARGV0, logff[pos].command);
41
42         logff[pos].command = NULL;
43     }
44
45
46     snprintf(str, 256, "ossec: output: '%s': ", logff[pos].command);
47     cmd_size = strlen(str);
48
49
50     while(fgets(str + cmd_size, OS_MAXSTR - OS_LOG_HEADER - 256, cmd_output) != NULL)
51     {
52         /* Getting the last occurence of \n */
53         if ((p = strrchr(str, '\n')) != NULL) 
54         {
55             *p = '\0';
56         }
57         
58         debug2("%s: DEBUG: Reading command message: '%s'", ARGV0, str);
59
60         
61         /* Sending message to queue */
62         if(drop_it == 0)
63         {
64             if(SendMSG(logr_queue,str,logff[pos].command,
65                         LOCALFILE_MQ) < 0)
66             {
67                 merror(QUEUE_SEND, ARGV0);
68                 if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
69                 {
70                     ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
71                 }
72             }
73         }
74
75         continue;
76     }
77
78     pclose(cmd_output);
79
80     return(NULL); 
81 }
82
83 /* EOF */