OpenVAS Scanner  22.7.9
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 
31 int
32 ipc_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 
49 char *
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 
84 int
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;
97 exit:
98  return rc;
99 }
100 
109 int
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);
121 exit:
122  return rc;
123 }
124 
131 struct 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;
142 error:
143  if (pc != NULL)
144  free (pc);
145  return NULL;
146 }
IPC_MAX_BUFFER
#define IPC_MAX_BUFFER
Definition: ipc_pipe.c:17
ipc_pipe_retrieve
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
ipc_pipe_close
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
ipc_pipe_context::fd
int fd[2]
Definition: ipc_pipe.h:11
ipc_pipe.h
len
uint8_t len
Definition: nasl_packet_forgery.c:1
ipc_pipe_send
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
ipc_pipe_context
Definition: ipc_pipe.h:10
ipc_pipe_destroy
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
free
void free(void *)
ipc_init_pipe
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