[go: up one dir, main page]

File: call.c

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (195 lines) | stat: -rw-r--r-- 5,632 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "box/call.h"
#include "lua/call.h"
#include "schema.h"
#include "session.h"
#include "func.h"
#include "port.h"
#include "box.h"
#include "txn.h"
#include "xrow.h"
#include "iproto_constants.h"
#include "rmean.h"
#include "small/obuf.h"
#include "tt_static.h"

static const struct port_vtab port_msgpack_vtab;

void
port_msgpack_create(struct port *base, const char *data, uint32_t data_sz)
{
	struct port_msgpack *port_msgpack = (struct port_msgpack *) base;
	memset(port_msgpack, 0, sizeof(*port_msgpack));
	port_msgpack->vtab = &port_msgpack_vtab;
	port_msgpack->data = data;
	port_msgpack->data_sz = data_sz;
}

static const char *
port_msgpack_get_msgpack(struct port *base, uint32_t *size)
{
	struct port_msgpack *port = (struct port_msgpack *) base;
	assert(port->vtab == &port_msgpack_vtab);
	*size = port->data_sz;
	return port->data;
}

static int
port_msgpack_dump_msgpack(struct port *base, struct obuf *out)
{
	struct port_msgpack *port = (struct port_msgpack *)base;
	assert(port->vtab == &port_msgpack_vtab);
	size_t size = port->data_sz;
	if (obuf_dup(out, port->data, size) == size)
		return 0;
	diag_set(OutOfMemory, size, "obuf_dup", "port->data");
	return -1;
}

extern void
port_msgpack_dump_lua(struct port *base, struct lua_State *L, bool is_flat);

extern const char *
port_msgpack_dump_plain(struct port *base, uint32_t *size);

void
port_msgpack_destroy(struct port *base)
{
	struct port_msgpack *port = (struct port_msgpack *)base;
	assert(port->vtab == &port_msgpack_vtab);
	free(port->plain);
}

int
port_msgpack_set_plain(struct port *base, const char *plain, uint32_t len)
{
	struct port_msgpack *port = (struct port_msgpack *)base;
	assert(port->plain == NULL);
	port->plain = (char *)malloc(len + 1);
	if (port->plain == NULL) {
		diag_set(OutOfMemory, len + 1, "malloc", "port->plain");
		return -1;
	}
	memcpy(port->plain, plain, len);
	port->plain[len] = 0;
	return 0;
}

static const struct port_vtab port_msgpack_vtab = {
	.dump_msgpack = port_msgpack_dump_msgpack,
	.dump_msgpack_16 = NULL,
	.dump_lua = port_msgpack_dump_lua,
	.dump_plain = port_msgpack_dump_plain,
	.get_msgpack = port_msgpack_get_msgpack,
	.get_vdbemem = NULL,
	.destroy = port_msgpack_destroy,
};

int
box_module_reload(const char *name)
{
	struct credentials *credentials = effective_user();
	if ((credentials->universal_access & (PRIV_X | PRIV_U)) !=
	    (PRIV_X | PRIV_U)) {
		struct user *user = user_find(credentials->uid);
		if (user != NULL)
			diag_set(AccessDeniedError, priv_name(PRIV_U),
				 schema_object_name(SC_UNIVERSE), "",
				 user->def->name);
		return -1;
	}
	struct module *module = NULL;
	if (module_reload(name, name + strlen(name), &module) == 0) {
		if (module != NULL)
			return 0;
		else
			diag_set(ClientError, ER_NO_SUCH_MODULE, name);
	}
	return -1;
}

int
box_process_call(struct call_request *request, struct port *port)
{
	rmean_collect(rmean_box, IPROTO_CALL, 1);
	/**
	 * Find the function definition and check access.
	 */
	const char *name = request->name;
	assert(name != NULL);
	uint32_t name_len = mp_decode_strl(&name);
	/* Transaction is not started. */
	assert(!in_txn());

	int rc;
	struct port args;
	port_msgpack_create(&args, request->args,
			    request->args_end - request->args);
	struct func *func = func_by_name(name, name_len);
	if (func != NULL) {
		rc = func_call(func, &args, port);
	} else if ((rc = access_check_universe_object(PRIV_X | PRIV_U,
				SC_FUNCTION, tt_cstr(name, name_len))) == 0) {
		rc = box_lua_call(name, name_len, &args, port);
	}
	if (rc != 0)
		return -1;
	if (in_txn() != NULL) {
		diag_set(ClientError, ER_FUNCTION_TX_ACTIVE);
		port_destroy(port);
		return -1;
	}
	return 0;
}

int
box_process_eval(struct call_request *request, struct port *port)
{
	rmean_collect(rmean_box, IPROTO_EVAL, 1);
	/* Check permissions */
	if (access_check_universe(PRIV_X) != 0)
		return -1;
	struct port args;
	port_msgpack_create(&args, request->args,
			    request->args_end - request->args);
	const char *expr = request->expr;
	uint32_t expr_len = mp_decode_strl(&expr);
	if (box_lua_eval(expr, expr_len, &args, port) != 0)
		return -1;
	if (in_txn() != 0) {
		diag_set(ClientError, ER_FUNCTION_TX_ACTIVE);
		port_destroy(port);
		return -1;
	}
	return 0;
}