blob: 253dfa2abde39d4dff8cb2d63718a27cc57e2a79 [file] [log] [blame]
Harald Welte8857f3b2022-11-18 13:54:44 +01001/*! \file osmo_io.c
2 * New osmocom async I/O API.
3 *
4 * (C) 2022 by Harald Welte <laforge@osmocom.org>
5 * (C) 2022-2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
6 * Author: Daniel Willmann <dwillmann@sysmocom.de>
7 *
8 * All Rights Reserved.
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22
23#include "../config.h"
24#if defined(__linux__)
25
26#include <fcntl.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <talloc.h>
30#include <unistd.h>
31#include <string.h>
32#include <stdbool.h>
33#include <errno.h>
34
35#include <osmocom/core/osmo_io.h>
36#include <osmocom/core/linuxlist.h>
37#include <osmocom/core/logging.h>
38#include <osmocom/core/msgb.h>
39#include <osmocom/core/socket.h>
40#include <osmocom/core/talloc.h>
41#include <osmocom/core/utils.h>
42
43#include "osmo_io_internal.h"
44
45/*! This environment variable can be set to manually set the backend used in osmo_io */
46#define OSMO_IO_BACKEND_ENV "LIBOSMO_IO_BACKEND"
47
48const struct value_string osmo_io_backend_names[] = {
49 { OSMO_IO_BACKEND_POLL, "poll" },
50 { 0, NULL }
51};
52
53static enum osmo_io_backend g_io_backend;
54
55/* Used by some tests, can't be static */
56struct iofd_backend_ops osmo_iofd_ops;
57
58/*! initialize osmo_io for the current thread */
59void osmo_iofd_init(void)
60{
61 switch (g_io_backend) {
62 case OSMO_IO_BACKEND_POLL:
63 break;
64 default:
65 OSMO_ASSERT(0);
66 break;
67 }
68}
69
70/* ensure main thread always has pre-initialized osmo_io
71 * priority 103: run after on_dso_load_select */
72static __attribute__((constructor(103))) void on_dso_load_osmo_io(void)
73{
74 char *backend = getenv(OSMO_IO_BACKEND_ENV);
75 if (backend == NULL)
76 backend = OSMO_IO_BACKEND_DEFAULT;
77
78 if (!strcmp("POLL", backend)) {
79 g_io_backend = OSMO_IO_BACKEND_POLL;
80 osmo_iofd_ops = iofd_poll_ops;
81 } else {
82 fprintf(stderr, "Invalid osmo_io backend requested: \"%s\"\nCheck the environment variable %s\n", backend, OSMO_IO_BACKEND_ENV);
83 exit(1);
84 }
85
86 osmo_iofd_init();
87}
88
89/*! Allocate the msghdr
90 * \param[in] iofd the osmo_io file structure
91 * \param[in] action the action this msg(hdr) is for (read, write, ..)
92 * \param[in] msg the msg buffer to use. Will allocate a new one if NULL
93 * \returns the newly allocated msghdr or NULL in case of error */
94struct iofd_msghdr *iofd_msghdr_alloc(struct osmo_io_fd *iofd, enum iofd_msg_action action, struct msgb *msg)
95{
96 struct iofd_msghdr *hdr = talloc_zero(iofd, struct iofd_msghdr);
97 if (!hdr)
98 return NULL;
99 if (!msg) {
100 msg = iofd_msgb_alloc(iofd);
101 if (!msg) {
102 talloc_free(hdr);
103 return NULL;
104 }
105 }
106
107 hdr->action = action;
108 hdr->iofd = iofd;
109 hdr->msg = msg;
110
111 return hdr;
112}
113
114/*! Free the msghdr
115 * \param[in] msghdr the msghdr to free
116 */
117void iofd_msghdr_free(struct iofd_msghdr *msghdr)
118{
119 /* msghdr->msg is never owned by msghdr, it will either be freed in the send path or
120 * or passed on to the read callback which takes ownership. */
121 talloc_free(msghdr);
122}
123
124/*! convenience wrapper to call msgb_alloc with parameters from osmo_io_fd */
125struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd)
126{
127 uint16_t headroom = iofd->msgb_alloc.headroom;
128
129 OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom);
130 return msgb_alloc_headroom_c(iofd->msgb_alloc.ctx,
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200131 iofd->msgb_alloc.size + headroom, headroom,
132 iofd->name ? : "iofd_msgb");
Harald Welte8857f3b2022-11-18 13:54:44 +0100133}
134
135/*! return the pending msgb in iofd or NULL if there is none*/
136struct msgb *iofd_msgb_pending(struct osmo_io_fd *iofd)
137{
138 struct msgb *msg = NULL;
139
140 msg = iofd->pending;
141 iofd->pending = NULL;
142
143 return msg;
144}
145
146/*! Return the pending msgb or allocate and return a new one */
147struct msgb *iofd_msgb_pending_or_alloc(struct osmo_io_fd *iofd)
148{
149 struct msgb *msg = NULL;
150
151 msg = iofd_msgb_pending(iofd);
152 if (!msg)
153 msg = iofd_msgb_alloc(iofd);
154
155 return msg;
156}
157
158/*! Enqueue a message to be sent
159 *
160 * Enqueues the message at the back of the queue provided there is enough space.
161 * \param[in] iofd the file descriptor
162 * \param[in] msghdr the message to enqueue
163 * \returns 0 if the message was enqueued succcessfully,
164 * -ENOSPC if the queue already contains the maximum number of messages
165 */
166int iofd_txqueue_enqueue(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
167{
168 if (iofd->tx_queue.current_length >= iofd->tx_queue.max_length)
169 return -ENOSPC;
170
171 llist_add_tail(&msghdr->list, &iofd->tx_queue.msg_queue);
172 iofd->tx_queue.current_length++;
173
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200174 if (iofd->tx_queue.current_length == 1)
Harald Welte8857f3b2022-11-18 13:54:44 +0100175 osmo_iofd_ops.write_enable(iofd);
176
177 return 0;
178}
179
180/*! Enqueue a message at the front
181 *
182 * Used to enqueue a msgb from a partial send again. This function will always
183 * enqueue the message, even if the maximum number of messages is reached.
184 * \param[in] iofd the file descriptor
185 * \param[in] msghdr the message to enqueue
186 */
187void iofd_txqueue_enqueue_front(struct osmo_io_fd *iofd, struct iofd_msghdr *msghdr)
188{
189 llist_add(&msghdr->list, &iofd->tx_queue.msg_queue);
190 iofd->tx_queue.current_length++;
191}
192
193/*! Dequeue a message from the front
194 *
195 * \param[in] iofd the file descriptor
196 * \returns the msghdr from the front of the queue or NULL if the queue is empty
197 */
198struct iofd_msghdr *iofd_txqueue_dequeue(struct osmo_io_fd *iofd)
199{
200 struct llist_head *lh;
201
202 if (iofd->tx_queue.current_length == 0)
203 return NULL;
204
205 lh = iofd->tx_queue.msg_queue.next;
206
207 OSMO_ASSERT(lh);
208 iofd->tx_queue.current_length--;
209 llist_del(lh);
210
211 if (iofd->tx_queue.current_length == 0)
212 osmo_iofd_ops.write_disable(iofd);
213
214 return llist_entry(lh, struct iofd_msghdr, list);
215}
216
217/*! Handle segmentation of the msg. If this function returns *_HANDLE_ONE or MORE then the data in msg will contain
218 * one complete message.
219 * If there are bytes left over, *pending_out will point to a msgb with the remaining data.
220*/
221static enum iofd_seg_act iofd_handle_segmentation(struct osmo_io_fd *iofd, struct msgb *msg, struct msgb **pending_out)
222{
arehbeinc0aa4bd2023-06-16 22:31:32 +0200223 int extra_len, received_len;
Harald Welte8857f3b2022-11-18 13:54:44 +0100224 struct msgb *msg_pending;
225
arehbeinc0aa4bd2023-06-16 22:31:32 +0200226 received_len = msgb_length(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100227
228 if (!iofd->io_ops.segmentation_cb) {
229 *pending_out = NULL;
230 return IOFD_SEG_ACT_HANDLE_ONE;
231 }
232
arehbeinc0aa4bd2023-06-16 22:31:32 +0200233 int expected_len = iofd->io_ops.segmentation_cb(msg);
234 if (expected_len == -EAGAIN) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200235 goto defer;
arehbeinc0aa4bd2023-06-16 22:31:32 +0200236 } else if (expected_len < 0) {
Daniel Willmannd4d03702023-05-17 12:38:14 +0200237 /* Something is wrong, skip this msgb */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200238 LOGPIO(iofd, LOGL_ERROR, "segmentation_cb returned error (%d), skipping msg of size %d\n",
239 expected_len, received_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100240 *pending_out = NULL;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200241 msgb_free(msg);
Harald Welte8857f3b2022-11-18 13:54:44 +0100242 return IOFD_SEG_ACT_DEFER;
243 }
244
arehbeinc0aa4bd2023-06-16 22:31:32 +0200245 extra_len = received_len - expected_len;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200246 /* No segmentation needed, return the whole msgb */
247 if (extra_len == 0) {
248 *pending_out = NULL;
249 return IOFD_SEG_ACT_HANDLE_ONE;
250 /* segment is incomplete */
251 } else if (extra_len < 0) {
252 goto defer;
253 }
254
255 /* msgb contains more than one segment */
256 /* Copy the trailing data over */
Harald Welte8857f3b2022-11-18 13:54:44 +0100257 msg_pending = iofd_msgb_alloc(iofd);
arehbeinc0aa4bd2023-06-16 22:31:32 +0200258 memcpy(msgb_data(msg_pending), msgb_data(msg) + expected_len, extra_len);
Daniel Willmannd4d03702023-05-17 12:38:14 +0200259 msgb_put(msg_pending, extra_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100260 *pending_out = msg_pending;
261
262 /* Trim the original msgb to size */
arehbeinc0aa4bd2023-06-16 22:31:32 +0200263 msgb_trim(msg, expected_len);
Harald Welte8857f3b2022-11-18 13:54:44 +0100264 return IOFD_SEG_ACT_HANDLE_MORE;
Daniel Willmannd4d03702023-05-17 12:38:14 +0200265
266defer:
267 *pending_out = msg;
268 return IOFD_SEG_ACT_DEFER;
Harald Welte8857f3b2022-11-18 13:54:44 +0100269}
270
271/*! Restore message boundaries on read() and pass individual messages to the read callback
272 */
273void iofd_handle_segmented_read(struct osmo_io_fd *iofd, struct msgb *msg, int rc)
274{
275 int res;
276 struct msgb *pending = NULL;
277
278 if (rc <= 0) {
279 iofd->io_ops.read_cb(iofd, rc, msg);
280 return;
281 }
282
283 do {
284 res = iofd_handle_segmentation(iofd, msg, &pending);
285 if (res != IOFD_SEG_ACT_DEFER || rc < 0)
286 iofd->io_ops.read_cb(iofd, rc, msg);
287 if (res == IOFD_SEG_ACT_HANDLE_MORE)
288 msg = pending;
289 } while (res == IOFD_SEG_ACT_HANDLE_MORE);
290
291 OSMO_ASSERT(iofd->pending == NULL);
292 iofd->pending = pending;
293}
294
295/* Public functions */
296
297/*! Send a message through a connected socket
298 *
299 * Appends the message to the internal transmit queue.
300 * If the function returns success (0) it will take ownership of the msgb and
301 * internally call msgb_free() after the write request completes.
302 * In case of an error the msgb needs to be freed by the caller.
303 * \param[in] iofd file descriptor to write to
304 * \param[in] msg message buffer to write
305 * \returns 0 in case of success; a negative value in case of error
306 */
307int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg)
308{
309 int rc;
310 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_WRITE, msg);
311 if (!msghdr)
312 return -ENOMEM;
313
314 msghdr->flags = 0;
315 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
316 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
317 msghdr->hdr.msg_iov = &msghdr->iov[0];
318 msghdr->hdr.msg_iovlen = 1;
319
320 rc = iofd_txqueue_enqueue(iofd, msghdr);
321 if (rc < 0) {
322 iofd_msghdr_free(msghdr);
323 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
324 return rc;
325 }
326
327 return 0;
328}
329
330/*! Send a message through an unconnected socket
331 *
332 * Appends the message to the internal transmit queue.
333 * If the function returns success (0), it will take ownership of the msgb and
334 * internally call msgb_free() after the write request completes.
335 * In case of an error the msgb needs to be freed by the caller.
336 * \param[in] iofd file descriptor to write to
337 * \param[in] msg message buffer to send
338 * \param[in] sendto_flags Flags to pass to the send call
339 * \param[in] dest destination address to send the message to
340 * \returns 0 in case of success; a negative value in case of error
341 */
342int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags, const struct osmo_sockaddr *dest)
343{
344 int rc;
345
346 OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_RECVFROM_SENDTO);
347
348 struct iofd_msghdr *msghdr = iofd_msghdr_alloc(iofd, IOFD_ACT_SENDTO, msg);
349 if (!msghdr)
350 return -ENOMEM;
351
352 if (dest) {
353 msghdr->osa = *dest;
354 msghdr->hdr.msg_name = &msghdr->osa.u.sa;
355 msghdr->hdr.msg_namelen = osmo_sockaddr_size(&msghdr->osa);
356 }
357 msghdr->flags = sendto_flags;
358 msghdr->iov[0].iov_base = msgb_data(msghdr->msg);
359 msghdr->iov[0].iov_len = msgb_length(msghdr->msg);
360 msghdr->hdr.msg_iov = &msghdr->iov[0];
361 msghdr->hdr.msg_iovlen = 1;
362
363 rc = iofd_txqueue_enqueue(iofd, msghdr);
364 if (rc < 0) {
365 iofd_msghdr_free(msghdr);
366 LOGPIO(iofd, LOGL_ERROR, "enqueueing message failed (%d). Rejecting msgb\n", rc);
367 return rc;
368 }
369
370 return 0;
371}
372
Harald Welte8857f3b2022-11-18 13:54:44 +0100373/*! Allocate and setup a new iofd
374 * \param[in] ctx the parent context from which to allocate
375 * \param[in] fd the underlying system file descriptor
376 * \param[in] name the name of the iofd
377 * \param[in] mode the mode of the iofd, whether it should use read()/write(), sendto()/recvfrom()
378 * \param[in] ioops structure with read/write/send/recv callbacks
379 * \param[in] data user data pointer accessible by the ioops callbacks
380 * \returns The newly allocated osmo_io_fd struct or NULL on failure
381 */
382struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, enum osmo_io_fd_mode mode,
383 const struct osmo_io_ops *ioops, void *data)
384{
385 struct osmo_io_fd *iofd = talloc_zero(ctx, struct osmo_io_fd);
386 if (!iofd)
387 return NULL;
388
389 iofd->fd = fd;
390 iofd->mode = mode;
Daniel Willmannf89162f2023-06-26 19:24:46 +0200391 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100392
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200393 if (name)
394 iofd->name = talloc_strdup(iofd, name);
Harald Welte8857f3b2022-11-18 13:54:44 +0100395
396 if (ioops)
397 iofd->io_ops = *ioops;
398
399 iofd->pending = NULL;
400
401 iofd->data = data;
402
403 iofd->msgb_alloc.ctx = ctx;
404 iofd->msgb_alloc.size = OSMO_IO_DEFAULT_MSGB_SIZE;
405 iofd->msgb_alloc.headroom = OSMO_IO_DEFAULT_MSGB_HEADROOM;
406
407 iofd->tx_queue.max_length = 32;
408 INIT_LLIST_HEAD(&iofd->tx_queue.msg_queue);
409
410 return iofd;
411}
412
413/*! Register the fd with the underlying backend
414 *
415 * \param[in] iofd the iofd file descriptor
416 * \param[in] fd the system fd number that will be registeres. If negative will use the one already set.
417 * \returns zero on success, a negative value on error
418*/
419int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
420{
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200421 int rc = 0;
422
Harald Welte8857f3b2022-11-18 13:54:44 +0100423 if (fd >= 0)
424 iofd->fd = fd;
Harald Welte8857f3b2022-11-18 13:54:44 +0100425
426 if (osmo_iofd_ops.register_fd)
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200427 rc = osmo_iofd_ops.register_fd(iofd);
Daniel Willmannacb97762023-06-07 17:02:31 +0200428 if (rc)
429 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100430
Daniel Willmannf89162f2023-06-26 19:24:46 +0200431 IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
Daniel Willmanneb9edba2023-06-06 16:53:38 +0200432 osmo_iofd_ops.read_enable(iofd);
433 osmo_iofd_ops.write_enable(iofd);
434
435 return rc;
Harald Welte8857f3b2022-11-18 13:54:44 +0100436}
437
438/*! Unregister the fd from the underlying backend
439 *
440 * \param[in] iofd the file descriptor
441 * \returns zero on success, a negative value on error
442 */
443int osmo_iofd_unregister(struct osmo_io_fd *iofd)
444{
445 if (osmo_iofd_ops.unregister_fd)
446 return osmo_iofd_ops.unregister_fd(iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200447 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100448
449 return 0;
450}
451
452/*! Get the number of messages in the tx queue
453 *
454 * \param[in] iofd the file descriptor
455 */
456unsigned int osmo_iofd_txqueue_len(struct osmo_io_fd *iofd)
457{
458 return iofd->tx_queue.current_length;
459}
460
461/*! Clear the transmit queue of the the iofd
462 *
463 * This function frees all messages currently pending in the transmit queue
464 * \param[in] iofd the file descriptor
465 */
466void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd)
467{
468 struct iofd_msghdr *hdr;
469 while ((hdr = iofd_txqueue_dequeue(iofd))) {
470 msgb_free(hdr->msg);
471 iofd_msghdr_free(hdr);
472 }
473}
474
475/*! Free the iofd
476 *
477 * This function is safe to use in the read/write callbacks and will defer freeing it until safe to do so.
478 * The iofd will be closed before.
479 * \param[in] iofd the file descriptor
480 */
481void osmo_iofd_free(struct osmo_io_fd *iofd)
482{
483 if (!iofd)
484 return;
485
486 osmo_iofd_close(iofd);
487
Daniel Willmannf89162f2023-06-26 19:24:46 +0200488 if (!IOFD_FLAG_ISSET(iofd, IOFD_FLAG_IN_CALLBACK)) {
Harald Welte8857f3b2022-11-18 13:54:44 +0100489 talloc_free(iofd);
490 } else {
491 /* Prevent our parent context from freeing us prematurely */
492 talloc_steal(NULL, iofd);
Daniel Willmannf89162f2023-06-26 19:24:46 +0200493 IOFD_FLAG_SET(iofd, IOFD_FLAG_TO_FREE);
Harald Welte8857f3b2022-11-18 13:54:44 +0100494 }
495}
496
497/*! Close the iofd
498 *
499 * This function closes the underlying fd and clears any messages in the tx queue
500 * The iofd is not freed and can be assigned a new file descriptor with osmo_iofd_register()
501 * \param[in] iofd the file descriptor
502 * \ returns 0 on success, a negative value otherwise
503 */
504int osmo_iofd_close(struct osmo_io_fd *iofd)
505{
506 int rc = 0;
507
Daniel Willmannf89162f2023-06-26 19:24:46 +0200508 if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_CLOSED))
Harald Welte8857f3b2022-11-18 13:54:44 +0100509 return rc;
510
Daniel Willmannf89162f2023-06-26 19:24:46 +0200511 IOFD_FLAG_SET(iofd, IOFD_FLAG_CLOSED);
Harald Welte8857f3b2022-11-18 13:54:44 +0100512
513 /* Free pending msgs in tx queue */
514 osmo_iofd_txqueue_clear(iofd);
515 msgb_free(iofd->pending);
516
517 iofd->pending = NULL;
518
519 if (osmo_iofd_ops.close)
520 rc = osmo_iofd_ops.close(iofd);
521 iofd->fd = -1;
522 return rc;
523}
524
525/*! Set the size and headroom of the msgb allocated when receiving messages
526 * \param[in] size the size of the msgb when receiving data
527 * \param[in] headroom the headroom of the msgb when receiving data
528 */
529void osmo_iofd_set_alloc_info(struct osmo_io_fd *iofd, unsigned int size, unsigned int headroom)
530{
531 iofd->msgb_alloc.headroom = headroom;
532 iofd->msgb_alloc.size = size;
533}
534
535/*! Get the associated user-data from an iofd
536 * \param[in] iofd the file descriptor
537 * \returns the data that was previously set with \ref osmo_iofd_setup()
538 */
539void *osmo_iofd_get_data(const struct osmo_io_fd *iofd)
540{
541 return iofd->data;
542}
543
544/*! Set the associated user-data from an iofd
545 * \param[in] iofd the file descriptor
546 * \param[in] data the data to set
547 */
548void osmo_iofd_set_data(struct osmo_io_fd *iofd, void *data)
549{
550 iofd->data = data;
551}
552
553/*! Get the private number from an iofd
554 * \param[in] iofd the file descriptor
555 * \returns the private number that was previously set with \ref osmo_iofd_set_priv_nr()
556 */
557unsigned int osmo_iofd_get_priv_nr(const struct osmo_io_fd *iofd)
558{
559 return iofd->priv_nr;
560}
561
562/*! Set the private number from an iofd
563 * \param[in] iofd the file descriptor
564 * \param[in] priv_nr the private number to set
565 */
566void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr)
567{
568 iofd->priv_nr = priv_nr;
569}
570
571/*! Get the underlying file descriptor from an iofd
572 * \param[in] iofd the file descriptor
573 * \returns the underlying file descriptor number */
574int osmo_iofd_get_fd(const struct osmo_io_fd *iofd)
575{
576 return iofd->fd;
577}
578
579/*! Get the name of the file descriptor
580 * \param[in] iofd the file descriptor
581 * \returns the name of the iofd as given in \ref osmo_iofd_setup() */
582const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd)
583{
584 return iofd->name;
585}
586
Pau Espin Pedrol63e45e62023-06-16 16:19:45 +0200587/*! Set the name of the file descriptor
588 * \param[in] iofd the file descriptor
589 * \param[in] name the name to set on the file descriptor */
590void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name)
591{
592 osmo_talloc_replace_string(iofd, &iofd->name, name);
593}
594
arehbein0c374c62023-05-14 21:43:11 +0200595/*! Set the osmo_io_ops for an iofd
596 * \param[in] iofd Target iofd file descriptor
597 * \param[in] ioops osmo_io_ops structure to be set */
598void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops)
599{
600 iofd->io_ops = *ioops;
601}
602
Harald Welte8857f3b2022-11-18 13:54:44 +0100603#endif /* defined(__linux__) */