1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2016. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#include <stdio.h>
#include <string.h>
#include <stropts.h>
#include <poll.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
/* arguments */
#define OWNPATH 1
#define NARG 1
/* status codes */
#define OK 0
#define NOT_OK 1
/* hardwired constants*/
#define PIPENAME "syslog.otp"
#define DIE_CMD "die"
#define ONLY_STDIN_CMD "only_stdin"
#define BUFFER_SIZE 1001
#define MAXPATH_SIZE 1001
#define STDIN 0
#define STDOUT 1
#define HEADER_SIZE 2
#define WAIT 10
#define INTERVAL 1
#define FALSE 0
#define TRUE 1
#define FDS_STDIN 0
#define FDS_PIPE 1
int main(int argc, char *argv[])
/* usage: ferrule ownpath */
{
int i, pipe_fd;
long int nfds=2L;
char buf[BUFFER_SIZE], pipename[MAXPATH_SIZE], packet_size[2];
struct pollfd fds[2];
struct stat stat_buf;
/* enough arguments? */
if(argc < NARG+1)
exit(NOT_OK);
/* make pipe name */
strcpy(pipename, argv[OWNPATH]);
strcat(pipename, "/");
strcat(pipename, PIPENAME);
/* wait for creation of pipe */
for(i=WAIT; i>0; i--)
if(stat(pipename, &stat_buf) == 0)
break;
else{
if(i == 0)
exit(NOT_OK);
else
sleep(INTERVAL);
}
/* open pipe, exit if error */
if((pipe_fd = open(pipename, O_RDONLY | O_NONBLOCK)) == -1)
exit(NOT_OK);
/* setup for pipe */
fds[FDS_PIPE].fd = pipe_fd;
fds[FDS_PIPE].events = POLLRDNORM;
fds[FDS_PIPE].revents = 0;
/* setup for stdin */
fds[FDS_STDIN].fd = STDIN;
fds[FDS_STDIN].events = POLLRDNORM;
fds[FDS_STDIN].revents = 0;
/* loop */
while(1){
/* wait for input */
if(poll(&fds[0], nfds, INFTIM) == -1)
exit(NOT_OK);
/* analyse input from pipe */
if(fds[FDS_PIPE].revents != 0){
/* pipe error, error exit */
if((fds[FDS_PIPE].revents & (POLLHUP | POLLERR | POLLNVAL)) != 0)
exit(NOT_OK);
/* read pipe and write to stdout, exit if error */
if((fds[FDS_PIPE].revents & POLLRDNORM) != 0){
i=0;
do{
read(pipe_fd, &buf[i++], (size_t)1);
}while(buf[i-1] != '\n');
i--;
/* send if string is not empty */
if(i != 0){
/* make packet size, [0]=MSB, [1]=LSB */
packet_size[0] = (i >> 8) & 0xff;
packet_size[1] = i & 0xff;
/* send to OTP process */
if(write(STDOUT, packet_size, HEADER_SIZE) != HEADER_SIZE)
exit(NOT_OK);
if(write(STDOUT, buf, i) != i)
exit(NOT_OK);
}
}
}
/* analyse input from stdin */
if(fds[FDS_STDIN].revents != 0){
/* error exit */
if((fds[FDS_STDIN].revents & (POLLHUP | POLLERR | POLLNVAL)) != 0)
exit(NOT_OK);
/* read bytes */
if((fds[FDS_STDIN].revents & POLLRDNORM) != 0){
/* get packet size, [0]=MSB, [1]=LSB */
if(read(STDIN, packet_size, HEADER_SIZE) != HEADER_SIZE)
exit(NOT_OK);
i = (packet_size[0] << 8) | packet_size[1];
/* get packet */
if(read(STDIN, buf, i) != i)
exit(NOT_OK);
/* check if die command */
if(strncmp(DIE_CMD, buf, i) == FALSE)
exit(OK);
/* check if only_stdin command */
if(strncmp(ONLY_STDIN_CMD, buf, i) == FALSE)
nfds=1L;
}
}
}
}
|