Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #pragma once
7 :
8 : #include <stddef.h>
9 :
10 : #include "core/helper.h"
11 :
12 : #define _cleanup_bf_response_ __attribute__((cleanup(bf_response_free)))
13 :
14 : /**
15 : * @enum bf_response_type
16 : *
17 : * Type of response received from the daemon.
18 : */
19 : enum bf_response_type
20 : {
21 : BF_RES_SUCCESS,
22 : BF_RES_FAILURE,
23 : _BF_RES_MAX
24 : };
25 :
26 : /**
27 : * @struct bf_response
28 : *
29 : * Response received from the daemon.
30 : *
31 : * @var bf_response::type
32 : * Type of the response: success or failure.
33 : * @var bf_response::data_len
34 : * Length of the data in the response.
35 : * @var bf_response::data
36 : * Data in the response.
37 : * @var bf_response::error
38 : * Error code.
39 : */
40 : struct bf_response
41 : {
42 : enum bf_response_type type;
43 :
44 : union
45 : {
46 : struct
47 : {
48 : size_t data_len;
49 : char data[];
50 : };
51 :
52 : struct
53 : {
54 : int error;
55 : };
56 : };
57 : };
58 :
59 : /**
60 : * Allocate a response without copying data.
61 : *
62 : * Space will be allocated in the response for @p data_len bytes of data, but
63 : * no data will be copied, nor will the response's data be initialized.
64 : *
65 : * The response's type will be set to BF_RES_SUCCESS.
66 : *
67 : * @param response Pointer to the response to allocate. Must be non-NULL.
68 : * @param data_len Size of the data to allocate.
69 : * @return 0 on success, or negative errno code on failure.
70 : */
71 : int bf_response_new_raw(struct bf_response **response, size_t data_len);
72 :
73 : /**
74 : * Allocate and initialise a new successful response.
75 : *
76 : * @param response Pointer to the response to allocate. Must be non-NULL.
77 : * @param data Client-specific data.
78 : * @param data_len Length of the client-specific data.
79 : * @return 0 on success, or negative errno code on failure.
80 : */
81 : int bf_response_new_success(struct bf_response **response, const char *data,
82 : size_t data_len);
83 :
84 : /**
85 : * Allocate and initialise a new failure response.
86 : *
87 : * @param response Pointer to the response to allocate. Must be non-NULL.
88 : * @param error Error code that store in the response.
89 : * @return 0 on success, or negative errno code on failure.
90 : */
91 : int bf_response_new_failure(struct bf_response **response, int error);
92 :
93 : /**
94 : * Free a response.
95 : *
96 : * If @p response points to a NULL pointer, this function does nothing. Once the
97 : * function returns, @p response points to a NULL pointer.
98 : *
99 : * @param response Response to free. Can't be NULL.
100 : */
101 : void bf_response_free(struct bf_response **response);
102 :
103 : /**
104 : * Copy a response.
105 : *
106 : * @param dest The destination response. It will be allocated during the call.
107 : * Can't be NULL.
108 : * @param src The source response, to copy. Can't be NULL.
109 : * @return 0 on success, negative error code on failure.
110 : */
111 : int bf_response_copy(struct bf_response **dest, const struct bf_response *src);
112 :
113 : /**
114 : * Get the total size of the response: request structure and data (if any).
115 : *
116 : * @param response Response to get the size of. Can't be NULL.
117 : * @return Total size of the response.
118 : */
119 0 : static inline size_t bf_response_size(const struct bf_response *response)
120 : {
121 0 : bf_assert(response);
122 :
123 0 : return sizeof(struct bf_response) +
124 0 : (response->type == BF_RES_SUCCESS ? response->data_len : 0);
125 : }
|