]> andersk Git - moira.git/blame - lib/mr_query.c
Avoid buffer overruns and check return value of malloc()
[moira.git] / lib / mr_query.c
CommitLineData
fa59b86f 1/* $Id$
7ac48069 2 *
3 * Perform a Moira query
92eb0760 4 *
7ac48069 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>.
5eaef520 8 *
92eb0760 9 */
10
babbc197 11#include <mit-copyright.h>
7ac48069 12#include <moira.h>
8defc06b 13#include "mr_private.h"
7ac48069 14
a3bbcc2b 15#include <errno.h>
a43ce477 16#include <stdlib.h>
7ac48069 17#include <string.h>
18
19RCSID("$Header$");
20
21int mr_query_internal(int argc, char **argv,
22 int (*callback)(int, char **, void *), void *callarg);
92eb0760 23
83e80378 24/*
8defc06b 25 * This routine is the primary external interface to the mr library.
83e80378 26 *
27 * It builds a new argument vector with the query handle prepended,
8defc06b 28 * and calls mr_query_internal.
83e80378 29 */
a43ce477 30static int level = 0;
83e80378 31
5eaef520 32int mr_query(char *name, int argc, char **argv,
7ac48069 33 int (*callproc)(int, char **, void *), void *callarg)
92eb0760 34{
44d12d58 35 char **nargv = malloc(sizeof(char *) * (argc + 1));
36 int status = 0;
5eaef520 37
a3bbcc2b 38 if (!nargv)
39 return ENOMEM;
5eaef520 40 nargv[0] = name;
41 memcpy(nargv + 1, argv, sizeof(char *) * argc);
42 status = mr_query_internal(argc + 1, nargv, callproc, callarg);
43 free(nargv);
44 return status;
83e80378 45}
5eaef520 46
83e80378 47/*
59ec8dae 48 * This routine makes a Moira query.
83e80378 49 *
50 * argv[0] is the query name.
51 * argv[1..argc-1] are the query arguments.
52 *
53 * callproc is called once for each returned value, with arguments
54 * argc, argv, and callarg.
55 * If it returns a non-zero value, further calls to it are not done, and
56 * all future data from the server is ignored (there should be some
57 * way to send it a quench..)
58 */
92eb0760 59
7ac48069 60int mr_query_internal(int argc, char **argv,
61 int (*callproc)(int, char **, void *), void *callarg)
83e80378 62{
5eaef520 63 int status;
64 mr_params params_st;
44d12d58 65 mr_params *params = NULL;
5eaef520 66 mr_params *reply = NULL;
67 int stopcallbacks = 0;
68
69 if (level)
70 return MR_QUERY_NOT_REENTRANT;
71
72 CHECK_CONNECTED;
73 level++;
4bff4d9d 74
5eaef520 75 params = &params_st;
76 params->mr_version_no = sending_version_no;
77 params->mr_procno = MR_QUERY;
78 params->mr_argc = argc;
79 params->mr_argl = NULL;
80 params->mr_argv = argv;
92eb0760 81
5eaef520 82 if ((status = mr_do_call(params, &reply)))
83 goto punt;
92eb0760 84
5eaef520 85 while ((status = reply->mr_status) == MR_MORE_DATA)
86 {
7ac48069 87 if (!stopcallbacks && callproc)
5eaef520 88 stopcallbacks = (*callproc)(reply->mr_argc, reply->mr_argv, callarg);
89 mr_destroy_reply(reply);
90 reply = NULL;
83e80378 91
5eaef520 92 initialize_operation(_mr_recv_op, mr_start_recv, &reply, NULL);
93 queue_operation(_mr_conn, CON_INPUT, _mr_recv_op);
83e80378 94
7ac48069 95 complete_operation(_mr_recv_op);
5eaef520 96 if (OP_STATUS(_mr_recv_op) != OP_COMPLETE)
97 {
98 mr_disconnect();
99 status = MR_ABORTED;
100 goto punt_1;
92eb0760 101 }
5eaef520 102 }
83e80378 103punt:
5eaef520 104 mr_destroy_reply(reply);
4bff4d9d 105punt_1:
5eaef520 106 level--;
107 return status;
92eb0760 108}
This page took 0.099992 seconds and 5 git commands to generate.