Line data Source code
1 : #include <stdlib.h>
2 : #include <stdio.h>
3 : #include <errno.h>
4 : #include <string.h>
5 : #include <sys/stat.h>
6 : #include <sys/types.h>
7 : #include <fcntl.h>
8 : #include <signal.h>
9 : #include <unistd.h>
10 : #include <limits.h>
11 : #include <stdbool.h>
12 :
13 : #include "jWrite.h"
14 :
15 : #include "fifo.h"
16 : #include "log.h"
17 : #include "netio/netio.h"
18 :
19 :
20 55 : bool is_fifo(const char* filename)
21 : {
22 55 : struct stat stat_p;
23 55 : if(0 != stat(filename, &stat_p)) {
24 : // does not exist
25 : return false;
26 : }
27 28 : if(!S_ISFIFO(stat_p.st_mode)) {
28 : // not a fifo
29 0 : return false;
30 : }
31 : return true;
32 : }
33 :
34 :
35 16 : int open_fifo(const char* fifoname)
36 : {
37 16 : if (!is_fifo(fifoname)) {
38 0 : fprintf(stderr, "error: '%s' does not exist or is not a FIFO\n", fifoname);
39 0 : exit(1);
40 : }
41 :
42 16 : signal(SIGPIPE, SIG_IGN);
43 :
44 : // Opening a FIFO for write-only will fail if there is no reader, so we fake a reader
45 16 : int tmpfd = open(fifoname, O_RDONLY | O_NDELAY);
46 :
47 16 : int fd = open(fifoname, O_WRONLY | O_NDELAY | O_NONBLOCK );
48 16 : if(fd < 0) {
49 0 : fprintf(stderr, "error: could not open FIFO %s\n", fifoname);
50 0 : LOG_TRACE("errno=%d str=%s", errno, strerror(errno));
51 0 : exit(1);
52 : }
53 16 : LOG_TRACE("FIFO %s opened, fd=%d", fifoname, fd);
54 :
55 16 : close(tmpfd);
56 :
57 16 : return fd;
58 : }
59 :
60 :
61 27 : int open_file(const char* filename)
62 : {
63 27 : int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH );
64 27 : if(fd < 0) {
65 0 : fprintf(stderr, "error: could not open FILE %s\n", filename);
66 0 : fprintf(stderr, "errno=%d str=%s\n", errno, strerror(errno));
67 0 : LOG_TRACE("errno=%d str=%s", errno, strerror(errno));
68 0 : exit(1);
69 : }
70 27 : LOG_TRACE("FILE %s opened, fd=%d", filename, fd);
71 :
72 27 : return fd;
73 : }
74 :
75 :
76 3839 : void error_write(int fd, netio_tag_t fid, const char *timestamp, const char *level, const char *msg, const struct ers_data* ers_data) {
77 3839 : char buf[PIPE_BUF];
78 3839 : struct jWriteControl jwc;
79 3839 : char device[30];
80 3839 : snprintf(device, 30, "%d", ers_data->device);
81 :
82 3839 : jwOpen(&jwc, buf, PIPE_BUF, JW_OBJECT, JW_NDJSON);
83 : //jwObj_ulong(&jwc, "fid", fid);
84 3839 : jwObj_string(&jwc, "timestamp", timestamp);
85 3839 : jwObj_string(&jwc, "level", level);
86 3839 : jwObj_string(&jwc, "message", msg);
87 3839 : jwObj_string(&jwc, "device", device);
88 3839 : jwObj_string(&jwc, "package", ers_data->package);
89 3839 : jwObj_string(&jwc, "filename", ers_data->filename);
90 3839 : jwObj_ulong(&jwc, "line", ers_data->line);
91 3839 : jwObj_string(&jwc, "functionname", ers_data->function_name);
92 3839 : jwObj_string(&jwc, "hostname", ers_data->host_name);
93 3839 : jwObj_ulong(&jwc, "pid", ers_data->pid);
94 3839 : jwObj_ulong(&jwc, "tid", ers_data->tid);
95 3839 : jwObj_string(&jwc, "cwd", ers_data->cwd);
96 3839 : jwObj_ulong(&jwc, "uid", ers_data->uid);
97 3839 : jwObj_string(&jwc, "uname", ers_data->uname);
98 3839 : jwObj_string(&jwc, "appname", ers_data->app_name);
99 :
100 3839 : int ret = jwClose(&jwc);
101 3839 : if (ret == 0){
102 3839 : unsigned count = strlen(buf);
103 3839 : write(fd, buf, count);
104 : } else {
105 0 : LOG_ERR("Error output error: %s. Output is stopped.", jwErrorToString(ret));
106 : }
107 : // int count = snprintf(buf, PIPE_BUF, "{\"fid\": \"%lu\", \"timestamp\": \"%s\", \"level\": %s, \"message\": %s}",
108 : // fid, timestamp, level, msg);
109 3839 : }
|