Print this page
10100 Illumos is confused about calloc() arguments
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libilb/common/ilb_comm.c
+++ new/usr/src/lib/libilb/common/ilb_comm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 + *
26 + * Copyright (c) 2018, Joyent, Inc.
25 27 */
26 28
27 29 #include <stdlib.h>
28 30 #include <strings.h>
29 31 #include <unistd.h>
30 32 #include <stddef.h>
31 33 #include <assert.h>
32 34 #include <sys/types.h>
33 35 #include <sys/socket.h>
34 36 #include <thread.h>
35 37 #include <synch.h>
36 38 #include <libilb_impl.h>
37 39 #include <libilb.h>
38 40
39 41 /* Assertion: the calling thread has a hold on the handle */
40 42 static void
41 43 i_ilb_socket_set_err(ilb_handle_t h, ilb_status_t err)
42 44 {
43 45 ilb_handle_impl_t *hi = (ilb_handle_impl_t *)h;
44 46
45 47 if (h == ILB_INVALID_HANDLE)
46 48 return;
47 49 hi->h_valid = B_FALSE;
48 50 hi->h_error = err;
49 51 }
50 52
51 53 ilb_status_t
52 54 ilb_open(ilb_handle_t *hp)
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
53 55 {
54 56 ilb_handle_impl_t *hi = NULL;
55 57 int s = -1;
56 58 struct sockaddr_un sa = {AF_UNIX, SOCKET_PATH};
57 59 ilb_status_t rc = ILB_STATUS_OK;
58 60 int sobufsz;
59 61
60 62 if (hp == NULL)
61 63 return (ILB_STATUS_EINVAL);
62 64
63 - hi = calloc(sizeof (*hi), 1);
65 + hi = calloc(1, sizeof (*hi));
64 66 if (hi == NULL)
65 67 return (ILB_STATUS_ENOMEM);
66 68
67 69 if (cond_init(&hi->h_cv, USYNC_THREAD, NULL) != 0) {
68 70 rc = ILB_STATUS_INTERNAL;
69 71 goto out;
70 72 }
71 73
72 74 if (mutex_init(&hi->h_lock, USYNC_THREAD | LOCK_ERRORCHECK, NULL)
73 75 != 0) {
74 76 rc = ILB_STATUS_INTERNAL;
75 77 goto out;
76 78 }
77 79
78 80 hi->h_busy = B_FALSE;
79 81
80 82 if ((s = socket(PF_UNIX, SOCK_SEQPACKET, 0)) == -1 ||
81 83 connect(s, (struct sockaddr *)&sa, sizeof (sa.sun_path))
82 84 == -1) {
83 85 rc = ILB_STATUS_SOCKET;
84 86 goto out;
85 87 }
86 88
87 89 /* The socket buffer must be at least the max size of a message */
88 90 sobufsz = ILBD_MSG_SIZE;
89 91 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sobufsz,
90 92 sizeof (sobufsz)) != 0) {
91 93 rc = ILB_STATUS_SOCKET;
92 94 (void) close(s);
93 95 goto out;
94 96 }
95 97 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sobufsz,
96 98 sizeof (sobufsz)) != 0) {
97 99 rc = ILB_STATUS_SOCKET;
98 100 (void) close(s);
99 101 goto out;
100 102 }
101 103
102 104 hi->h_socket = s;
103 105 hi->h_valid = B_TRUE;
104 106
105 107 out:
106 108 if (rc != ILB_STATUS_OK && s != -1)
107 109 (void) close(s);
108 110
109 111 if (rc == ILB_STATUS_OK) {
110 112 *hp = (ilb_handle_t)hi;
111 113 } else {
112 114 free(hi);
113 115 *hp = ILB_INVALID_HANDLE;
114 116 }
115 117 return (rc);
116 118 }
117 119
118 120 ilb_status_t
119 121 ilb_close(ilb_handle_t h)
120 122 {
121 123 ilb_handle_impl_t *hi = (ilb_handle_impl_t *)h;
122 124
123 125 if (h == ILB_INVALID_HANDLE)
124 126 return (ILB_STATUS_EINVAL);
125 127
126 128 if (mutex_lock(&hi->h_lock) != 0)
127 129 return (ILB_STATUS_INTERNAL);
128 130
129 131 /* Somebody has done a close, no need to do anything. */
130 132 if (hi->h_closing) {
131 133 return (ILB_STATUS_OK);
132 134 } else {
133 135 hi->h_closing = B_TRUE;
134 136 hi->h_error = ILB_STATUS_HANDLE_CLOSING;
135 137 }
136 138
137 139 /* Wait until there is nobody waiting. */
138 140 while (hi->h_waiter > 0) {
139 141 if (cond_wait(&hi->h_cv, &hi->h_lock) != 0) {
140 142 (void) mutex_unlock(&hi->h_lock);
141 143 return (ILB_STATUS_INTERNAL);
142 144 }
143 145 }
144 146 /* No one is waiting, proceed to free the handle. */
145 147
146 148 (void) close(hi->h_socket);
147 149 (void) mutex_destroy(&hi->h_lock);
148 150 (void) cond_destroy(&hi->h_cv);
149 151 free(hi);
150 152 return (ILB_STATUS_OK);
151 153 }
152 154
153 155 /*
154 156 * Unified routine to communicate with ilbd.
155 157 *
156 158 * If ic is non-NULL, it means that the caller wants to send something
157 159 * to ilbd and expects a reply. If ic is NULL, it means that the caller
158 160 * only expects to receive from ilbd.
159 161 *
160 162 * The rbuf is the buffer supplied by the caller for receiving. If it
161 163 * is NULL, it means that there is no reply expected.
162 164 *
163 165 * This function will not close() the socket to kernel unless there is
164 166 * an error. If the transaction only consists of one exchange, the caller
165 167 * can use i_ilb_close_comm() to close() the socket when done.
166 168 */
167 169 ilb_status_t
168 170 i_ilb_do_comm(ilb_handle_t h, ilb_comm_t *ic, size_t ic_sz, ilb_comm_t *rbuf,
169 171 size_t *rbufsz)
170 172 {
171 173 ilb_status_t rc = ILB_STATUS_OK;
172 174 int r, s;
173 175 ilb_handle_impl_t *hi = (ilb_handle_impl_t *)h;
174 176
175 177 assert(rbuf != NULL);
176 178 if (h == ILB_INVALID_HANDLE)
177 179 return (ILB_STATUS_EINVAL);
178 180
179 181 if (mutex_lock(&hi->h_lock) != 0)
180 182 return (ILB_STATUS_INTERNAL);
181 183
182 184 hi->h_waiter++;
183 185 while (hi->h_busy) {
184 186 if (cond_wait(&hi->h_cv, &hi->h_lock) != 0) {
185 187 hi->h_waiter--;
186 188 (void) cond_signal(&hi->h_cv);
187 189 (void) mutex_unlock(&hi->h_lock);
188 190 return (ILB_STATUS_INTERNAL);
189 191 }
190 192 }
191 193
192 194 if (!hi->h_valid || hi->h_closing) {
193 195 hi->h_waiter--;
194 196 (void) cond_signal(&hi->h_cv);
195 197 (void) mutex_unlock(&hi->h_lock);
196 198 return (hi->h_error);
197 199 }
198 200
199 201 hi->h_busy = B_TRUE;
200 202 (void) mutex_unlock(&hi->h_lock);
201 203
202 204 s = hi->h_socket;
203 205
204 206 r = send(s, ic, ic_sz, 0);
205 207 if (r < ic_sz) {
206 208 rc = ILB_STATUS_WRITE;
207 209 goto socket_error;
208 210 }
209 211 rc = ILB_STATUS_OK;
210 212
211 213 if ((r = recv(s, rbuf, *rbufsz, 0)) <= 0) {
212 214 rc = ILB_STATUS_READ;
213 215 } else {
214 216 *rbufsz = r;
215 217 goto out;
216 218 }
217 219
218 220 socket_error:
219 221 i_ilb_socket_set_err(h, rc);
220 222
221 223 out:
222 224 (void) mutex_lock(&hi->h_lock);
223 225 hi->h_busy = B_FALSE;
224 226 hi->h_waiter--;
225 227 (void) cond_signal(&hi->h_cv);
226 228 (void) mutex_unlock(&hi->h_lock);
227 229
228 230 return (rc);
229 231 }
230 232
231 233 void
232 234 i_ilb_close_comm(ilb_handle_t h)
233 235 {
234 236 (void) ilb_close(h);
235 237 }
↓ open down ↓ |
162 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX