[24319] | 1 | /* $Id: mr_query.c 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
[23095] | 2 | * |
---|
| 3 | * Perform a Moira query |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
---|
| 6 | * For copying and distribution information, please see the file |
---|
| 7 | * <mit-copyright.h>. |
---|
| 8 | * |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | #include <mit-copyright.h> |
---|
| 12 | #include <moira.h> |
---|
| 13 | #include "mr_private.h" |
---|
| 14 | |
---|
| 15 | #include <errno.h> |
---|
| 16 | #include <stdlib.h> |
---|
| 17 | #include <string.h> |
---|
| 18 | |
---|
[24319] | 19 | RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/lib/mr_query.c $ $Id: mr_query.c 3956 2010-01-05 20:56:56Z zacheiss $"); |
---|
[23095] | 20 | |
---|
| 21 | /* |
---|
| 22 | * This routine is the primary external interface to the mr library. |
---|
| 23 | * |
---|
| 24 | * It builds a new argument vector with the query handle prepended, |
---|
| 25 | * and calls mr_query_internal. |
---|
| 26 | */ |
---|
| 27 | static int level = 0; |
---|
| 28 | |
---|
| 29 | int mr_query(char *name, int argc, char **argv, |
---|
| 30 | int (*callproc)(int, char **, void *), void *callarg) |
---|
| 31 | { |
---|
| 32 | int status, stopcallbacks = 0; |
---|
| 33 | mr_params params, reply; |
---|
| 34 | |
---|
| 35 | CHECK_CONNECTED; |
---|
| 36 | if (level) |
---|
| 37 | return MR_QUERY_NOT_REENTRANT; |
---|
| 38 | |
---|
| 39 | params.u.mr_procno = MR_QUERY; |
---|
| 40 | params.mr_argc = argc + 1; |
---|
| 41 | params.mr_argl = NULL; |
---|
| 42 | params.mr_argv = malloc(sizeof(char *) * (argc + 1)); |
---|
| 43 | if (!params.mr_argv) |
---|
| 44 | return ENOMEM; |
---|
| 45 | params.mr_argv[0] = name; |
---|
| 46 | memcpy(params.mr_argv + 1, argv, sizeof(char *) * argc); |
---|
| 47 | |
---|
| 48 | level++; |
---|
| 49 | if ((status = mr_do_call(¶ms, &reply))) |
---|
| 50 | goto punt; |
---|
| 51 | |
---|
| 52 | while ((status = reply.u.mr_status) == MR_MORE_DATA) |
---|
| 53 | { |
---|
| 54 | if (callproc && !stopcallbacks) |
---|
| 55 | stopcallbacks = (*callproc)(reply.mr_argc, reply.mr_argv, callarg); |
---|
| 56 | mr_destroy_reply(reply); |
---|
| 57 | |
---|
| 58 | if (mr_receive(_mr_conn, &reply) != MR_SUCCESS) |
---|
| 59 | { |
---|
| 60 | mr_disconnect(); |
---|
| 61 | status = MR_ABORTED; |
---|
| 62 | goto punt_1; |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | punt: |
---|
| 67 | mr_destroy_reply(reply); |
---|
| 68 | punt_1: |
---|
| 69 | level--; |
---|
| 70 | free(params.mr_argv); |
---|
| 71 | |
---|
| 72 | return status; |
---|
| 73 | } |
---|