~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/alertpipe.c -rw-r--r-- 3.3 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 9 months ago
                                                                                
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2017, Sean Bright
 *
 * Sean Bright <sean.bright@gmail.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Alert Pipe API
 *
 * \author Sean Bright
 */

#include "asterisk.h"

#include <unistd.h>
#include <fcntl.h>

#ifdef HAVE_EVENTFD
# include <sys/eventfd.h>
#endif

#include "asterisk/alertpipe.h"
#include "asterisk/logger.h"

int ast_alertpipe_init(int alert_pipe[2])
{
#ifdef HAVE_EVENTFD

	int fd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE);
	if (fd > -1) {
		alert_pipe[0] = alert_pipe[1] = fd;
		return 0;
	}

	ast_log(LOG_WARNING, "Failed to create alert pipe with eventfd(), falling back to pipe(): %s\n",
		strerror(errno));
	ast_alertpipe_clear(alert_pipe);

#endif

#ifdef HAVE_PIPE2

	if (pipe2(alert_pipe, O_NONBLOCK)) {
		ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno));
		return -1;
	}

#else

	if (pipe(alert_pipe)) {
		ast_log(LOG_WARNING, "Failed to create alert pipe: %s\n", strerror(errno));
		return -1;
	} else {
		if (ast_fd_set_flags(alert_pipe[0], O_NONBLOCK)
		   || ast_fd_set_flags(alert_pipe[1], O_NONBLOCK)) {
			ast_alertpipe_close(alert_pipe);
			return -1;
		}
	}

#endif

	return 0;
}

void ast_alertpipe_close(int alert_pipe[2])
{
#ifdef HAVE_EVENTFD

	if (alert_pipe[0] == alert_pipe[1]) {
		if (alert_pipe[0] > -1) {
			close(alert_pipe[0]);
			ast_alertpipe_clear(alert_pipe);
		}
		return;
	}

#endif

	if (alert_pipe[0] > -1) {
		close(alert_pipe[0]);
	}
	if (alert_pipe[1] > -1) {
		close(alert_pipe[1]);
	}
	ast_alertpipe_clear(alert_pipe);
}

ast_alert_status_t ast_alertpipe_read(int alert_pipe[2])
{
	uint64_t tmp;

	if (!ast_alertpipe_readable(alert_pipe)) {
		return AST_ALERT_NOT_READABLE;
	}

	if (read(alert_pipe[0], &tmp, sizeof(tmp)) < 0) {
		if (errno != EINTR && errno != EAGAIN) {
			ast_log(LOG_WARNING, "read() failed: %s\n", strerror(errno));
			return AST_ALERT_READ_FAIL;
		}
	}

	return AST_ALERT_READ_SUCCESS;
}

ssize_t ast_alertpipe_write(int alert_pipe[2])
{
	uint64_t tmp = 1;

	if (!ast_alertpipe_writable(alert_pipe)) {
		errno = EBADF;
		return 0;
	}

	/* preset errno in case returned size does not match */
	errno = EPIPE;
	return write(alert_pipe[1], &tmp, sizeof(tmp)) != sizeof(tmp);
}

ast_alert_status_t ast_alertpipe_flush(int alert_pipe[2])
{
	int bytes_read;
	uint64_t tmp[16];

	if (!ast_alertpipe_readable(alert_pipe)) {
		return AST_ALERT_NOT_READABLE;
	}

	/* Read the alertpipe until it is exhausted. */
	for (;;) {
		bytes_read = read(alert_pipe[0], tmp, sizeof(tmp));
		if (bytes_read < 0) {
			if (errno == EINTR) {
				continue;
			}
			if (errno == EAGAIN || errno == EWOULDBLOCK) {
				/*
				 * Would block so nothing left to read.
				 * This is the normal loop exit.
				 */
				break;
			}
			ast_log(LOG_WARNING, "read() failed flushing alertpipe: %s\n",
				strerror(errno));
			return AST_ALERT_READ_FAIL;
		}
		if (!bytes_read) {
			/* Read nothing so we are done */
			break;
		}
	}

	return AST_ALERT_READ_SUCCESS;
}