/
dsmlogevent.c
247 lines (210 loc) · 6.31 KB
/
dsmlogevent.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright (c) 2015 IPnett AS. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dsmapitd.h"
#include "dsmapifp.h"
#include "dapitype.h"
#include "dapiproc.h"
#include "dapidata.h"
//#include "dapiutil.h" /* seems unneeded now */
#include "dsmrc.h"
#include <sys/stat.h>
// #define DEBUG 1
char **globalArgv;
void printdsmerror(dsUint32_t,dsInt16_t);
int
main(int argc,char **argv) {
int fdesc, usingfile=0, done=0;
struct stat statbuf;
FILE* stream;
dsInt16_t dsmresult;
dsUint32_t myhandle;
optStruct *my_opts;
dsmInitExIn_t *init_in;
dsmInitExOut_t *init_out;
dsmLogExIn_t *log_in;
dsmLogExOut_t *log_out;
char strbuf[1024];
if (((2 == argc)&&(strncmp(argv[1],"-f",2))) || \
((3 == argc)&&(strncmp(argv[1],"-f",2)==0))) {
/* handle -f case first */
if (3 == argc) {
usingfile=1;
fdesc=open(argv[2],O_RDONLY);
if (fdesc < 0) {
printf("Could not open file %s\n", argv[2]);
exit(1);
}
if (0==fstat(fdesc, &statbuf)) {
if ((statbuf.st_size > 5*1024) || \
(statbuf.st_size < 2)) {
printf("Input file %s too small/large to send: %lld\n",
argv[2], statbuf.st_size);
exit(1);
}
#ifdef DEBUG
else {
printf("Size of file: %lld\n",statbuf.st_size);
}
#endif /* DEBUG */
} else {
perror("fstat()");
exit(1);
}
stream=fdopen(fdesc,"r");
if (NULL==stream) {
perror("fdopen failed");
exit(1);
}
}
if ((my_opts=(optStruct*)calloc(1,sizeof(*my_opts))) == NULL) {
printf("No mem for calloc\n");
exit(1);
}
#ifdef DEBUG
else {
printf("Sizeof optstruct: %d\n",sizeof(*my_opts));
}
#endif /* DEBUG */
dsmresult=dsmQueryCliOptions(my_opts);
#ifdef DEBUG
printf("dsmQuery returned: %d\n",dsmresult);
#endif /* DEBUG */
if ((init_in=(dsmInitExIn_t*)calloc(1,sizeof(*init_in))) == NULL) {
printf("No mem for init_in\n");
exit(1);
}
#ifdef DEBUG
else {
printf("sizeof: %d\n",sizeof(*init_in));
}
#endif /* DEBUG */
if ((init_out=(dsmInitExOut_t*)calloc(1,sizeof(*init_out))) == NULL) {
printf("No mem for init_out\n");
exit(1);
}
#ifdef DEBUG
else {
printf("sizeof: %d\n",sizeof(*init_out));
}
#endif /* DEBUG */
if ((log_in=(dsmLogExIn_t*)calloc(1,sizeof(*log_in))) == NULL) {
printf("No mem for log_in\n");
exit(1);
}
#ifdef DEBUG
else {
printf("sizeof: %d\n",sizeof(*log_in));
}
#endif /* DEBUG */
if ((log_out=(dsmLogExOut_t*)calloc(1,sizeof(*log_out))) == NULL) {
printf("No mem for log_out\n");
exit(1);
}
#ifdef DEBUG
else {
printf("Sizeof: %d\n",sizeof(*log_out));
}
#endif /* DEBUG */
/* Preparing the initial settings */
init_in->stVersion = dsmInitExInVersion;
init_in->apiVersionExP = &apiApplVer;
init_in->clientNodeNameP = NULL;
init_in->clientOwnerNameP = NULL;
init_in->clientPasswordP = NULL;
init_in->applicationTypeP = NULL;
init_in->configfile = NULL;
init_in->options = NULL;
init_in->userNameP = NULL;
init_in->userPasswordP = NULL;
dsmresult=dsmInitEx(&myhandle,init_in,init_out);
if (DSM_RC_OK != dsmresult) {
printdsmerror(myhandle,dsmresult);
printf("dsmInitEx failed: %d\n",dsmresult);
exit(1);
}
#ifdef DEBUG
printf("dsmInitEx returned: %d\n",dsmresult);
#endif /* DEBUG */
/* set common settings for both cmdline and file log shipping */
log_in->severity = logSevInfo; /* ANE4990 */
strncpy(log_in->appMsgID, "IPN4711",8);
if(0==usingfile) {
log_in->message = argv[1];
log_in->logType = logBoth;
done=1;
} else {
log_in->logType = logServer;
log_in->message = fgets(strbuf,1010,stream);
}
do {
#ifdef DEBUG
printf("would send string: %s", log_in->message);
#else
dsmresult=dsmLogEventEx(myhandle,log_in,log_out);
if (DSM_RC_OK != dsmresult) {
printdsmerror(myhandle, dsmresult);
if (dsmresult == DSM_RC_STRING_TOO_LONG) {
printf("(max ~1000 chars)\n");
}
/* wont stop, we still want to run the exit cleanup */
} else {
printf("Message sent ok.\n");
}
#endif /* DEBUG */
if (usingfile) {
log_in->message = fgets(strbuf,1010,stream);
if (log_in->message == NULL) {
done=1; /* break out of loop in case fgets stops
reading useful data out of the file */
}
}
} while (!done);
dsmresult=dsmTerminate(myhandle);
if (DSM_RC_OK != dsmresult) {
printdsmerror(myhandle, dsmresult);
} else {
printf("Exiting\n");
}
exit(0);
} else {
printf("Usage #1: %s <string in quotes to send>\n", argv[0]);
printf("Usage #2: %s -f <filename to send>\n", argv[0]);
exit(1);
}
}
void
printdsmerror(dsUint32_t handle, dsInt16_t dserror) {
char rcmess[DSM_MAX_RC_MSG_LENGTH];
if (0 == dsmRCMsg(handle, dserror,rcmess)) {
printf("Error: %d\n%s", dserror, rcmess);
} else {
printf("Can't translate error");
}
return;
}