| 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stdarg.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #if defined(SEAP_THREAD_SAFE) |
| 31 | # include <pthread.h> |
| 32 | #endif |
| 33 | |
| 34 | #include "generic/common.h" |
| 35 | #include "_seap-types.h" |
| 36 | #include "_sexp-types.h" |
| 37 | #include "generic/redblack.h" |
| 38 | #include "_seap-command.h" |
| 39 | #include "_seap-packet.h" |
| 40 | #include "_seap.h" |
| 41 | #include "debug_priv.h" |
| 42 | |
| 43 | int SEAP_cmd_register (SEAP_CTX_t *ctx, SEAP_cmdcode_t code, uint32_t flags, SEAP_cmdfn_t func, ...) /* sd, arg */ |
| 44 | { |
| 45 | va_list ap; |
| 46 | int sd; |
| 47 | void *arg; |
| 48 | |
| 49 | SEAP_desc_t *dsc; |
| 50 | SEAP_cmdtbl_t *tbl; |
| 51 | SEAP_cmdrec_t *rec; |
| 52 | |
| 53 | _A(ctx != NULL); |
| 54 | _A(func != NULL); |
| 55 | |
| 56 | sd = -1; |
| 57 | arg = NULL; |
| 58 | |
| 59 | va_start (ap, func); |
| 60 | |
| 61 | if (flags & SEAP_CMDREG_LOCAL) { |
| 62 | sd = va_arg (ap, int); |
| 63 | dsc = SEAP_desc_get (ctx->sd_table, sd); |
| 64 | |
| 65 | if (dsc == NULL) { |
| 66 | va_end(ap); |
| 67 | return (-1); |
| 68 | } |
| 69 | |
| 70 | tbl = dsc->cmd_c_table; |
| 71 | } else { |
| 72 | tbl = ctx->cmd_c_table; |
| 73 | } |
| 74 | |
| 75 | _A(tbl != NULL); |
| 76 | |
| 77 | if (flags & SEAP_CMDREG_USEARG) { |
| 78 | arg = va_arg (ap, void *); |
| 79 | _A(arg != NULL); |
| 80 | } |
| 81 | |
| 82 | va_end(ap); |
| 83 | |
| 84 | rec = SEAP_cmdrec_new (); |
| 85 | rec->code = code; |
| 86 | rec->func = func; |
| 87 | rec->arg = arg; |
| 88 | |
| 89 | switch (SEAP_cmdtbl_add (tbl, rec)) { |
| 90 | case 0: |
| 91 | /* rec is freed by SEAP_cmdtbl_add */ |
| 92 | break; |
| 93 | case SEAP_CMDTBL_ECOLL: |
| 94 | dD("Can't register command: code=%u, tbl=%p: already registered.", |
| 95 | code, (void *)tbl); |
| 96 | SEAP_cmdrec_free (rec); |
| 97 | return (-1); |
| 98 | case -1: |
| 99 | dD("Can't register command: code=%u, func=%p, tbl=%p, arg=%p: errno=%u, %s.", |
| 100 | code, (void *)func, (void *)tbl, arg, errno, strerror (errno)); |
| 101 | SEAP_cmdrec_free (rec); |
| 102 | return (-1); |
| 103 | default: |
| 104 | SEAP_cmdrec_free (rec); |
| 105 | errno = EDOOFUS; |
| 106 | return (-1); |
| 107 | } |
| 108 | |
| 109 | return (0); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * SEAP_cmd_register (ctx, SEAP_CMDREG_USEARG, 1, cmdfn, sd, NULL); |
| 114 | * SEAP_cmd_register (ctx, SEAP_CMDREG_GLOBAL, 1, cmdfn); |
| 115 | * SEAP_cmd_register (ctx, 0, 1, cmdfn, sd); |
| 116 | * SEAP_cmd_register (ctx, SEAP_CMDREG_USEARG | SEAP_CMDREG_GLOBAL, 1, cmdfn, NULL); |
| 117 | */ |
| 118 | SEAP_cmdrec_t *SEAP_cmdrec_new (void) |
| 119 | { |
| 120 | SEAP_cmdrec_t *r = malloc(sizeof(SEAP_cmdrec_t)); |
| 121 | r->code = 0; |
| 122 | r->func = NULL; |
| 123 | r->arg = NULL; |
| 124 | |
| 125 | return (r); |
| 126 | } |
| 127 | |
| 128 | void SEAP_cmdrec_free (SEAP_cmdrec_t *r) |
| 129 | { |
| 130 | free(r); |
| 131 | } |
| 132 | |
| 133 | SEAP_cmdtbl_t *SEAP_cmdtbl_new (void) |
| 134 | { |
| 135 | SEAP_cmdtbl_t *t = malloc(sizeof(SEAP_cmdtbl_t)); |
| 136 | |
| 137 | t->table = NULL; |
| 138 | t->maxcnt = 0; |
| 139 | |
| 140 | #if defined(SEAP_THREAD_SAFE) |
| 141 | if (pthread_rwlock_init (&t->lock, NULL) != 0) { |
| 142 | dD("Can't initialize rwlock: %u, %s.", |
| 143 | errno, strerror (errno)); |
| 144 | free(t); |
| 145 | return (NULL); |
| 146 | } |
| 147 | #endif |
| 148 | return (t); |
| 149 | } |
| 150 | |
| 151 | void SEAP_cmdtbl_free (SEAP_cmdtbl_t *t) |
| 152 | { |
| 153 | if (t != NULL) |
| 154 | SEAP_cmdtbl_backendT_free (t); |
| 155 | free(t); |
| 156 | } |
| 157 | |
| 158 | int SEAP_cmdtbl_setsize (SEAP_cmdtbl_t *t, size_t maxsz) |
| 159 | { |
| 160 | t->maxcnt = maxsz; |
| 161 | return (0); |
| 162 | } |
| 163 | |
| 164 | int SEAP_cmdtbl_setfl (SEAP_cmdtbl_t *t, uint8_t f) |
| 165 | { |
| 166 | t->flags |= f; |
| 167 | return (0); |
| 168 | } |
| 169 | |
| 170 | int SEAP_cmdtbl_unsetfl (SEAP_cmdtbl_t *t, uint8_t f) |
| 171 | { |
| 172 | t->flags &= ~(f); |
| 173 | return (0); |
| 174 | } |
| 175 | |
| 176 | int SEAP_cmdtbl_add (SEAP_cmdtbl_t *t, SEAP_cmdrec_t *r) |
| 177 | { |
| 178 | return SEAP_cmdtbl_backendT_add (t, r); |
| 179 | } |
| 180 | |
| 181 | int SEAP_cmdtbl_ins (SEAP_cmdtbl_t *t, SEAP_cmdrec_t *r) |
| 182 | { |
| 183 | return SEAP_cmdtbl_backendT_ins (t, r); |
| 184 | } |
| 185 | |
| 186 | int SEAP_cmdtbl_del (SEAP_cmdtbl_t *t, SEAP_cmdrec_t *r) |
| 187 | { |
| 188 | return SEAP_cmdtbl_backendT_del (t, r->code); |
| 189 | } |
| 190 | |
| 191 | SEAP_cmdrec_t *SEAP_cmdtbl_get (SEAP_cmdtbl_t *t, SEAP_cmdcode_t c) |
| 192 | { |
| 193 | return SEAP_cmdtbl_backendT_get (t, c); |
| 194 | } |
| 195 | |
| 196 | int SEAP_cmdtbl_cmp (SEAP_cmdrec_t *a, SEAP_cmdrec_t *b) |
| 197 | { |
| 198 | return (int)(a->code - b->code); |
| 199 | } |
| 200 | |
| 201 | static SEXP_t *__SEAP_cmd_sync_handler (SEXP_t *res, void *arg) |
| 202 | { |
| 203 | struct SEAP_synchelper *h = (struct SEAP_synchelper *)arg; |
| 204 | |
| 205 | h->args = res; |
| 206 | (void) pthread_mutex_lock (&h->mtx); |
| 207 | h->signaled = 1; |
| 208 | (void) pthread_cond_broadcast (&h->cond); |
| 209 | (void) pthread_mutex_unlock (&h->mtx); |
| 210 | |
| 211 | return (NULL); |
| 212 | } |
| 213 | |
| 214 | SEXP_t *SEAP_cmd_exec (SEAP_CTX_t *ctx, |
| 215 | int sd, |
| 216 | uint32_t flags, |
| 217 | SEAP_cmdcode_t code, |
| 218 | SEXP_t *args, |
| 219 | SEAP_cmdtype_t type, |
| 220 | SEAP_cmdfn_t func, |
| 221 | void *funcarg) |
| 222 | { |
| 223 | SEAP_desc_t *dsc = NULL; |
| 224 | SEAP_cmdrec_t *rec = NULL; |
| 225 | SEAP_cmdtbl_t *tbl[2]; |
| 226 | SEXP_t *res = NULL; |
| 227 | int8_t i; |
| 228 | |
| 229 | _A(ctx != NULL); |
| 230 | |
| 231 | #if !defined(NDEBUG) || defined(VALIDATE_SEXP) |
| 232 | if (args != NULL) { |
| 233 | SEXP_VALIDATE(args); |
| 234 | } |
| 235 | #endif |
| 236 | |
| 237 | dD("code=%u, args=%p", code, args); |
| 238 | |
| 239 | dsc = SEAP_desc_get (ctx->sd_table, sd); |
| 240 | |
| 241 | if (dsc == NULL) |
| 242 | return (NULL); |
| 243 | |
| 244 | if (flags & (SEAP_EXEC_LOCAL | SEAP_EXEC_WQUEUE)) { |
| 245 | dD("EXEC_LOCAL"); |
| 246 | |
| 247 | /* get table pointers */ |
| 248 | if (flags & SEAP_EXEC_WQUEUE) { |
| 249 | i = 0; |
| 250 | tbl[0] = dsc->cmd_w_table; |
| 251 | } else if (flags & SEAP_EXEC_LONLY) { |
| 252 | i = 0; |
| 253 | tbl[0] = dsc->cmd_c_table; |
| 254 | } else { |
| 255 | i = 1; |
| 256 | |
| 257 | if (flags & SEAP_EXEC_GFIRST) { |
| 258 | tbl[1] = ctx->cmd_c_table; |
| 259 | tbl[0] = dsc->cmd_c_table; |
| 260 | } else { |
| 261 | tbl[1] = dsc->cmd_c_table; |
| 262 | tbl[0] = ctx->cmd_c_table; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* lookup command */ |
| 267 | rec = NULL; |
| 268 | |
| 269 | for (; i >= 0; --i) |
| 270 | if ((rec = SEAP_cmdtbl_get (tbl[i], code)) != NULL) |
| 271 | break; |
| 272 | |
| 273 | dD("rec=%p, w=%u", rec, (flags & SEAP_EXEC_WQUEUE) ? 1 : 0); |
| 274 | |
| 275 | if (rec == NULL) { |
| 276 | |
| 277 | return (NULL); |
| 278 | } |
| 279 | /* execute command */ |
| 280 | res = rec->func (args, rec->arg); |
| 281 | |
| 282 | dD("res=%p", res); |
| 283 | |
| 284 | /* filter result */ |
| 285 | if (func != NULL) |
| 286 | res = func (res, funcarg); |
| 287 | |
| 288 | dD("func@%p(res)=%p", func, res); |
| 289 | |
| 290 | /* delete command from the wait queue */ |
| 291 | if (flags & SEAP_EXEC_WQUEUE) { |
| 292 | SEAP_cmdtbl_del(tbl[i], rec); |
| 293 | } |
| 294 | |
| 295 | return (res); |
| 296 | } else { |
| 297 | SEAP_cmd_t *cmdptr; |
| 298 | SEAP_packet_t *packet; |
| 299 | |
| 300 | dD("EXEC_REMOTE"); |
| 301 | |
| 302 | packet = SEAP_packet_new (); |
| 303 | cmdptr = SEAP_packet_settype (packet, SEAP_PACKET_CMD); |
| 304 | |
| 305 | cmdptr->id = SEAP_desc_gencmdid (ctx->sd_table, sd); |
| 306 | cmdptr->rid = 0; |
| 307 | cmdptr->class = SEAP_CMDCLASS_USR; |
| 308 | cmdptr->code = code; |
| 309 | cmdptr->args = args; |
| 310 | cmdptr->flags = 0; |
| 311 | |
| 312 | switch (type) { |
| 313 | case SEAP_CMDTYPE_SYNC: { |
| 314 | struct SEAP_synchelper h; |
| 315 | |
| 316 | cmdptr->flags |= SEAP_CMDFLAG_SYNC; |
| 317 | |
| 318 | if (pthread_cond_init (&h.cond, NULL) != 0 || |
| 319 | pthread_mutex_init (&h.mtx, NULL) != 0) |
| 320 | abort (); |
| 321 | |
| 322 | h.args = NULL; |
| 323 | h.signaled = 0; |
| 324 | |
| 325 | rec = SEAP_cmdrec_new (); |
| 326 | rec->code = cmdptr->id; |
| 327 | rec->func = &__SEAP_cmd_sync_handler; |
| 328 | rec->arg = &h; |
| 329 | |
| 330 | switch (SEAP_cmdtbl_add (dsc->cmd_w_table, rec)) { |
| 331 | case 0: |
| 332 | break; |
| 333 | case SEAP_CMDTBL_ECOLL: |
| 334 | dD("Can't register async command handler: id=%u, tbl=%p, sd=%u: already registered.", |
| 335 | rec->code, (void *)dsc->cmd_w_table, sd); |
| 336 | SEAP_cmdrec_free (rec); |
| 337 | SEAP_packet_free(packet); |
| 338 | return (NULL); |
| 339 | case -1: |
| 340 | dD("Can't register async command handler: id=%u, tbl=%p, sd=%u: errno=%u, %s.", |
| 341 | rec->code, (void *)dsc->cmd_w_table, sd, errno, strerror (errno)); |
| 342 | SEAP_cmdrec_free (rec); |
| 343 | SEAP_packet_free(packet); |
| 344 | return (NULL); |
| 345 | default: |
| 346 | SEAP_cmdrec_free (rec); |
| 347 | SEAP_packet_free(packet); |
| 348 | errno = EDOOFUS; |
| 349 | return (NULL); |
| 350 | } |
| 351 | |
| 352 | if (SEAP_packet_send (ctx, sd, packet) != 0) { |
| 353 | protect_errno { |
| 354 | dD("FAIL: errno=%u, %s.", errno, strerror (errno)); |
| 355 | SEAP_cmdtbl_del(dsc->cmd_w_table, rec); |
| 356 | SEAP_packet_free(packet); |
| 357 | } |
| 358 | return (NULL); |
| 359 | } |
| 360 | |
| 361 | if (flags & SEAP_EXEC_RECV) { |
| 362 | /* struct timespec timeout; */ |
| 363 | SEAP_packet_t *packet_rcv; |
| 364 | /* |
| 365 | * We have to do own receiving of events. We'll queue |
| 366 | * all events (errors, messages) that we are not interested |
| 367 | * in. After a command (reply) is received, we'll try to |
| 368 | * check whether the condition we are waiting for was signaled. |
| 369 | */ |
| 370 | |
| 371 | packet_rcv = NULL; |
| 372 | /* |
| 373 | timeout.tv_sec = 0; |
| 374 | timeout.tv_nsec = 0; |
| 375 | */ |
| 376 | for (;;) { |
| 377 | if (SEAP_packet_recv(ctx, sd, &packet_rcv) != 0) { |
| 378 | dD("FAIL: ctx=%p, sd=%d, errno=%u, %s.", ctx, sd, errno, strerror(errno)); |
| 379 | SEAP_packet_free(packet); |
| 380 | return(NULL); |
| 381 | } |
| 382 | |
| 383 | switch(SEAP_packet_gettype(packet_rcv)) { |
| 384 | case SEAP_PACKET_CMD: |
| 385 | switch (__SEAP_recvmsg_process_cmd (ctx, sd, SEAP_packet_cmd(packet_rcv))) { |
| 386 | case 0: |
| 387 | SEAP_packet_free(packet_rcv); |
| 388 | break; |
| 389 | default: |
| 390 | SEAP_packet_free(packet); |
| 391 | errno = EDOOFUS; |
| 392 | return(NULL); |
| 393 | } |
| 394 | case SEAP_PACKET_MSG: |
| 395 | /* FIXME */ |
| 396 | break; |
| 397 | case SEAP_PACKET_ERR: |
| 398 | /* FIXME */ |
| 399 | break; |
| 400 | default: |
| 401 | abort(); |
| 402 | } |
| 403 | |
| 404 | /* Morbo: THIS IS NOT HOW SYCHNRONIZATION WORKS! */ |
| 405 | if (h.signaled) { |
| 406 | h.signaled = 0; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | } else { |
| 411 | /* |
| 412 | * Someone else does receiving of events for us. |
| 413 | * Just wait for the condition to be signaled. |
| 414 | */ |
| 415 | pthread_mutex_lock(&h.mtx); |
| 416 | while (!h.signaled) { |
| 417 | pthread_cond_wait(&h.cond, &h.mtx); |
| 418 | } |
| 419 | // This might not be needed, but still |
| 420 | h.signaled = 0; |
| 421 | pthread_mutex_unlock(&h.mtx); |
| 422 | } |
| 423 | |
| 424 | dD("cond return: h.args=%p", h.args); |
| 425 | |
| 426 | if (h.args == NULL) |
| 427 | res = NULL; |
| 428 | else if (func != NULL) |
| 429 | res = func (h.args, funcarg); |
| 430 | else |
| 431 | res = h.args; |
| 432 | |
| 433 | /* |
| 434 | * SEAP_cmdtbl_del(dsc->cmd_w_table, rec); |
| 435 | */ |
| 436 | pthread_cond_destroy (&(h.cond)); |
| 437 | pthread_mutex_destroy (&(h.mtx)); |
| 438 | SEAP_packet_free(packet); |
| 439 | |
| 440 | return (res); |
| 441 | } |
| 442 | case SEAP_CMDTYPE_ASYNC: |
| 443 | cmdptr->flags |= SEAP_CMDFLAG_ASYNC; |
| 444 | |
| 445 | /* Register handler */ |
| 446 | rec = SEAP_cmdrec_new (); |
| 447 | rec->code = cmdptr->id; |
| 448 | rec->func = func; |
| 449 | rec->arg = funcarg; |
| 450 | |
| 451 | switch (SEAP_cmdtbl_add (dsc->cmd_w_table, rec)) { |
| 452 | case 0: |
| 453 | break; |
| 454 | case SEAP_CMDTBL_ECOLL: |
| 455 | dD("Can't register async command handler: id=%u, tbl=%p, sd=%u: already registered.", |
| 456 | rec->code, (void *)dsc->cmd_w_table, sd); |
| 457 | SEAP_cmdrec_free (rec); |
| 458 | SEAP_packet_free(packet); |
| 459 | return (NULL); |
| 460 | case -1: |
| 461 | dD("Can't register async command handler: id=%u, tbl=%p, sd=%u: errno=%u, %s.", |
| 462 | rec->code, (void *)dsc->cmd_w_table, sd, errno, strerror (errno)); |
| 463 | SEAP_cmdrec_free(rec); |
| 464 | SEAP_packet_free(packet); |
| 465 | return (NULL); |
| 466 | default: |
| 467 | SEAP_cmdrec_free(rec); |
| 468 | SEAP_packet_free(packet); |
| 469 | errno = EDOOFUS; |
| 470 | return (NULL); |
| 471 | } |
| 472 | |
| 473 | if (SEAP_packet_send (ctx, sd, packet) != 0) { |
| 474 | protect_errno { |
| 475 | dD("FAIL: errno=%u, %s.", errno, strerror (errno)); |
| 476 | SEAP_cmdtbl_del(dsc->cmd_w_table, rec); |
| 477 | SEAP_packet_free(packet); |
| 478 | } |
| 479 | return (NULL); |
| 480 | } |
| 481 | |
| 482 | SEAP_packet_free(packet); |
| 483 | |
| 484 | return (args); |
| 485 | default: |
| 486 | errno = EINVAL; |
| 487 | SEAP_packet_free(packet); |
| 488 | return (NULL); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /* NOTREACHED */ |
| 493 | errno = EDOOFUS; |
| 494 | return (NULL); |
| 495 | } |
| 496 | |
| 497 | SEAP_cmdjob_t *SEAP_cmdjob_new (void) |
| 498 | { |
| 499 | SEAP_cmdjob_t *j = malloc(sizeof(SEAP_cmdjob_t)); |
| 500 | j->ctx = NULL; |
| 501 | j->sd = -1; |
| 502 | |
| 503 | return (j); |
| 504 | } |
| 505 | |
| 506 | void SEAP_cmdjob_free (SEAP_cmdjob_t *j) |
| 507 | { |
| 508 | free(j); |
| 509 | } |