Asterisk - The Open Source Telephony Project  18.5.0
Functions
alertpipe.c File Reference

Alert Pipe API. More...

#include "asterisk.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/eventfd.h>
#include "asterisk/alertpipe.h"
#include "asterisk/logger.h"
Include dependency graph for alertpipe.c:

Go to the source code of this file.

Functions

void ast_alertpipe_close (int alert_pipe[2])
 Close an alert pipe. More...
 
ast_alert_status_t ast_alertpipe_flush (int alert_pipe[2])
 Consume all alerts written to the alert pipe. More...
 
int ast_alertpipe_init (int alert_pipe[2])
 Initialize an alert pipe. More...
 
ast_alert_status_t ast_alertpipe_read (int alert_pipe[2])
 Read an event from an alert pipe. More...
 
ssize_t ast_alertpipe_write (int alert_pipe[2])
 Write an event to an alert pipe. More...
 

Detailed Description

Alert Pipe API.

Author
Sean Bright

Definition in file alertpipe.c.

Function Documentation

◆ ast_alertpipe_close()

void ast_alertpipe_close ( int  alert_pipe[2])

Close an alert pipe.

Since
13.16.0
Parameters
pa two-element containing the alert pipe's file descriptors

Definition at line 79 of file alertpipe.c.

References ast_alertpipe_clear().

Referenced by ast_alertpipe_init(), ast_channel_internal_alertpipe_close(), bridge_channel_destroy(), and really_quit().

80 {
81 #ifdef HAVE_EVENTFD
82 
83  if (alert_pipe[0] == alert_pipe[1]) {
84  if (alert_pipe[0] > -1) {
85  close(alert_pipe[0]);
87  }
88  return;
89  }
90 
91 #endif
92 
93  if (alert_pipe[0] > -1) {
94  close(alert_pipe[0]);
95  }
96  if (alert_pipe[1] > -1) {
97  close(alert_pipe[1]);
98  }
100 }
void ast_alertpipe_clear(int alert_pipe[2])
Sets the alert pipe file descriptors to default values.
Definition: alertpipe.h:97
int alert_pipe[2]
Definition: res_corosync.c:276

◆ ast_alertpipe_flush()

ast_alert_status_t ast_alertpipe_flush ( int  alert_pipe[2])

Consume all alerts written to the alert pipe.

Since
13.16.0
Parameters
pa two-element array containing the alert pipe's file descriptors
Return values
AST_ALERT_READ_SUCCESSon success
AST_ALERT_NOT_READABLEif the alert pipe is not readable
AST_ALERT_READ_FATALif the alert pipe's file descriptors are in blocking mode, or a read error occurs.

Definition at line 134 of file alertpipe.c.

References AST_ALERT_NOT_READABLE, AST_ALERT_READ_FAIL, AST_ALERT_READ_SUCCESS, ast_alertpipe_readable(), ast_log, errno, LOG_WARNING, and tmp().

Referenced by ast_channel_internal_alert_flush().

135 {
136  int bytes_read;
137  uint64_t tmp[16];
138 
140  return AST_ALERT_NOT_READABLE;
141  }
142 
143  /* Read the alertpipe until it is exhausted. */
144  for (;;) {
145  bytes_read = read(alert_pipe[0], tmp, sizeof(tmp));
146  if (bytes_read < 0) {
147  if (errno == EINTR) {
148  continue;
149  }
150  if (errno == EAGAIN || errno == EWOULDBLOCK) {
151  /*
152  * Would block so nothing left to read.
153  * This is the normal loop exit.
154  */
155  break;
156  }
157  ast_log(LOG_WARNING, "read() failed flushing alertpipe: %s\n",
158  strerror(errno));
159  return AST_ALERT_READ_FAIL;
160  }
161  if (!bytes_read) {
162  /* Read nothing so we are done */
163  break;
164  }
165  }
166 
167  return AST_ALERT_READ_SUCCESS;
168 }
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
int ast_alertpipe_readable(int alert_pipe[2])
Determine if the alert pipe is readable.
Definition: alertpipe.h:112
#define ast_log
Definition: astobj2.c:42
int alert_pipe[2]
Definition: res_corosync.c:276
int errno

◆ ast_alertpipe_init()

int ast_alertpipe_init ( int  alert_pipe[2])

Initialize an alert pipe.

Since
13.16.0
Parameters
pa two-element array to hold the alert pipe's file descriptors
Returns
non-zero if a failure occurred, zero otherwise.

Definition at line 38 of file alertpipe.c.

References ast_alertpipe_clear(), ast_alertpipe_close(), ast_fd_set_flags, ast_log, errno, and LOG_WARNING.

Referenced by ast_channel_internal_alertpipe_init(), asterisk_daemon(), and bridge_channel_internal_alloc().

39 {
40 #ifdef HAVE_EVENTFD
41 
42  int fd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE);
43  if (fd > -1) {
44  alert_pipe[0] = alert_pipe[1] = fd;
45  return 0;
46  }
47 
48  ast_log(LOG_WARNING, "Failed to create alert pipe with eventfd(), falling back to pipe(): %s\n",
49  strerror(errno));
51 
52 #endif
53 
54 #ifdef HAVE_PIPE2
55 
56  if (pipe2(alert_pipe, O_NONBLOCK)) {
57  ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno));
58  return -1;
59  }
60 
61 #else
62 
63  if (pipe(alert_pipe)) {
64  ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno));
65  return -1;
66  } else {
67  if (ast_fd_set_flags(alert_pipe[0], O_NONBLOCK)
68  || ast_fd_set_flags(alert_pipe[1], O_NONBLOCK)) {
70  return -1;
71  }
72  }
73 
74 #endif
75 
76  return 0;
77 }
#define LOG_WARNING
Definition: logger.h:274
void ast_alertpipe_close(int alert_pipe[2])
Close an alert pipe.
Definition: alertpipe.c:79
void ast_alertpipe_clear(int alert_pipe[2])
Sets the alert pipe file descriptors to default values.
Definition: alertpipe.h:97
#define ast_fd_set_flags(fd, flags)
Set flags on the given file descriptor.
Definition: utils.h:1009
#define ast_log
Definition: astobj2.c:42
int alert_pipe[2]
Definition: res_corosync.c:276
int errno

◆ ast_alertpipe_read()

ast_alert_status_t ast_alertpipe_read ( int  alert_pipe[2])

Read an event from an alert pipe.

Since
13.16.0
Parameters
pa two-element array containing the alert pipe's file descriptors
Return values
AST_ALERT_READ_SUCCESSon success
AST_ALERT_NOT_READABLEif the alert pipe is not readable
AST_ALERT_READ_FATALif the alert pipe's file descriptors are in blocking mode, or a read error occurs.

Definition at line 102 of file alertpipe.c.

References AST_ALERT_NOT_READABLE, AST_ALERT_READ_FAIL, AST_ALERT_READ_SUCCESS, ast_alertpipe_readable(), ast_log, errno, LOG_WARNING, and tmp().

Referenced by ast_channel_internal_alert_read(), bridge_channel_handle_write(), and monitor_sig_flags().

103 {
104  uint64_t tmp;
105 
107  return AST_ALERT_NOT_READABLE;
108  }
109 
110  if (read(alert_pipe[0], &tmp, sizeof(tmp)) < 0) {
111  if (errno != EINTR && errno != EAGAIN) {
112  ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
113  return AST_ALERT_READ_FAIL;
114  }
115  }
116 
117  return AST_ALERT_READ_SUCCESS;
118 }
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
int ast_alertpipe_readable(int alert_pipe[2])
Determine if the alert pipe is readable.
Definition: alertpipe.h:112
#define ast_log
Definition: astobj2.c:42
int alert_pipe[2]
Definition: res_corosync.c:276
int errno

◆ ast_alertpipe_write()

ssize_t ast_alertpipe_write ( int  alert_pipe[2])

Write an event to an alert pipe.

Since
13.16.0
Parameters
pa two-element array containing the alert pipe's file descriptors
Return values
0Success
1Failure

Definition at line 120 of file alertpipe.c.

References ast_alertpipe_writable(), errno, and tmp().

Referenced by __quit_handler(), _hup_handler(), ast_bridge_channel_queue_frame(), and ast_channel_alert_write().

121 {
122  uint64_t tmp = 1;
123 
125  errno = EBADF;
126  return 0;
127  }
128 
129  /* preset errno in case returned size does not match */
130  errno = EPIPE;
131  return write(alert_pipe[1], &tmp, sizeof(tmp)) != sizeof(tmp);
132 }
static int tmp()
Definition: bt_open.c:389
int ast_alertpipe_writable(int alert_pipe[2])
Determine if the alert pipe is writable.
Definition: alertpipe.h:127
int alert_pipe[2]
Definition: res_corosync.c:276
int errno