OpenVAS Scanner 23.32.3
ipc_pipe.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "ipc_pipe.h"
7
8#include <errno.h>
9#include <fcntl.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/types.h>
14#include <sys/wait.h>
15#include <unistd.h>
16
17#define IPC_MAX_BUFFER 4096
18
31int
32ipc_pipe_send (struct ipc_pipe_context *context, const char *msg, int len)
33{
34 int wfd, wr;
35 wfd = context->fd[1];
36 wr = write (wfd, msg, len);
37 return wr;
38}
39
49char *
51{
52 char *result = NULL;
53 int rfd, pf;
54 rfd = context->fd[0];
55 pf = fcntl (rfd, F_GETFL, 0);
56 if (pf < 0 && errno != EBADF)
57 // fd is closed or invalid; we assume closed
58 return NULL;
59 fcntl (rfd, F_SETFL, pf | O_NONBLOCK);
60 if ((result = calloc (1, IPC_MAX_BUFFER)) == NULL)
61 return NULL;
62
63 if (read (rfd, result, IPC_MAX_BUFFER) > 0)
64 {
65 return result;
66 }
67 else
68 {
69 free (result);
70 // if temporary unavailable or not a closed descriptor don't
71 // print an error.
72 return NULL;
73 }
74}
75
84int
86{
87 int rc = 0;
88 if (context == NULL)
89 {
90 rc = -1;
91 goto exit;
92 }
93 if ((rc = close (context->fd[0])) < 0)
94 goto exit;
95 if ((rc = close (context->fd[1])) < 0)
96 goto exit;
97exit:
98 return rc;
99}
100
109int
111{
112 int rc = 0;
113 if (context == NULL)
114 {
115 rc = -1;
116 goto exit;
117 }
118 if ((rc = ipc_pipe_close (context)) < 0)
119 goto exit;
120 free (context);
121exit:
122 return rc;
123}
124
131struct ipc_pipe_context *
133{
134 struct ipc_pipe_context *pc = NULL;
135 if ((pc = calloc (1, sizeof (*pc))) == NULL)
136 goto error;
137 if (pipe (pc->fd) == -1)
138 {
139 goto error;
140 }
141 return pc;
142error:
143 if (pc != NULL)
144 free (pc);
145 return NULL;
146}
struct ipc_pipe_context * ipc_init_pipe(void)
initializes a new context. Do not use this method directly, use ipc_init of ipc.h instead.
Definition ipc_pipe.c:132
int ipc_pipe_send(struct ipc_pipe_context *context, const char *msg, int len)
sends given msg via the given context. Do not use this method directly, use ipc_send of ipc....
Definition ipc_pipe.c:32
#define IPC_MAX_BUFFER
Definition ipc_pipe.c:17
int ipc_pipe_destroy(struct ipc_pipe_context *context)
destroys given context. Do not use this method directly, use ipc_destroy of ipc.h instead.
Definition ipc_pipe.c:110
int ipc_pipe_close(struct ipc_pipe_context *context)
closes given context. Do not use this method directly, use ipc_close of ipc.h instead.
Definition ipc_pipe.c:85
char * ipc_pipe_retrieve(struct ipc_pipe_context *context)
retrieves message from the given context. Do not use this method directly, use ipc_retrieve of ipc....
Definition ipc_pipe.c:50
void free(void *)
uint8_t len