gauc 0.3.0

Couchbase Rust Adapter / CLI
Documentation
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2011-2012 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

#define LCB_IOPS_V12_NO_DEPRECATE 1 /* For Ruby */

#include "internal.h"
#include "plugins/io/select/select_io_opts.h"
#include <libcouchbase/plugins/io/bsdio-inl.c>

#ifdef LCB_EMBED_PLUGIN_LIBEVENT
LIBCOUCHBASE_API lcb_error_t lcb_create_libevent_io_opts(int, lcb_io_opt_t*,void*);
#endif

typedef lcb_error_t (*create_func_t)(int version, lcb_io_opt_t *io, void *cookie);


#ifdef _WIN32
LIBCOUCHBASE_API
lcb_error_t lcb_iocp_new_iops(int, lcb_io_opt_t *, void *);
#define DEFAULT_IOPS LCB_IO_OPS_WINIOCP
#else
#define DEFAULT_IOPS LCB_IO_OPS_LIBEVENT
#endif


typedef struct {
    /** The "base" name of the plugin */
    const char *base;

    /** Corresponding type */
    lcb_io_ops_type_t iotype;

    /** Filename */
    const char *soname;

    /** Symbol used to initialize the plugin */
    const char *symbol;

    /** Function to create the iops (if builtin) */
    create_func_t create;

    /** Static buffers if reading from the environment */
    char s_soname[PATH_MAX];
    char s_symbol[256];
} plugin_info;


#ifdef __APPLE__
#define PLUGIN_SO(NAME) "libcouchbase_"NAME".dylib"
#elif defined(_WIN32)
/** Trailing period intentional. See docs for LoadLibrary */
#if (_DEBUG && _MSC_VER)
    #define PLUGIN_SO(NAME) "libcouchbase_"NAME"_d.dll."
#else
    #define PLUGIN_SO(NAME) "libcouchbase_"NAME".dll."
#endif /* _DEBUG */
#else
#define PLUGIN_SO(NAME) "libcouchbase_"NAME".so"
#endif

#define PLUGIN_SYMBOL(NAME) "lcb_create_"NAME"_io_opts"

#define BUILTIN_CORE(name, type, create) \
        { name, type, NULL, NULL, create, { 0 }, { 0 } }

#define BUILTIN_DL(name, type) \
        { name, type, PLUGIN_SO(name), PLUGIN_SYMBOL(name), NULL, { 0 }, { 0 } }

static plugin_info builtin_plugins[] = {
    BUILTIN_CORE("select", LCB_IO_OPS_SELECT, lcb_create_select_io_opts),
    BUILTIN_CORE("winsock", LCB_IO_OPS_WINSOCK, lcb_create_select_io_opts),

#ifdef _WIN32
    BUILTIN_CORE("iocp", LCB_IO_OPS_WINIOCP, lcb_iocp_new_iops),
#endif

#ifdef LCB_EMBED_PLUGIN_LIBEVENT
    BUILTIN_CORE("libevent", LCB_IO_OPS_LIBEVENT, lcb_create_libevent_io_opts),
#else
    BUILTIN_DL("libevent", LCB_IO_OPS_LIBEVENT),
#endif

    BUILTIN_DL("libev", LCB_IO_OPS_LIBEV),
    BUILTIN_DL("libuv", LCB_IO_OPS_LIBUV),

    { NULL, LCB_IO_OPS_INVALID, NULL, NULL, NULL, { 0 }, { 0 } }
};

/**
 * Checks the environment for plugin information.
 * Returns:
 *   1  information found and valid
 *   0  not found
 *   -1 error
 */
static int get_env_plugin_info(plugin_info *info)
{

    plugin_info *cur = NULL;
    memset(info, 0, sizeof(*info));

    if (!lcb_getenv_nonempty_multi(info->s_soname, sizeof(info->s_soname),
        "LIBCOUCHBASE_EVENT_PLUGIN_NAME", "LCB_IOPS_NAME", NULL)) {
        return 0;
    }

    for (cur = builtin_plugins; cur->base; cur++) {
        if (strlen(cur->base) != strlen(info->s_soname)) {
            continue;
        }

        if (strcmp(cur->base, info->s_soname) == 0) {
            memcpy(info, cur, sizeof(*cur));
            return 1;
        }
    }

    if (!lcb_getenv_nonempty_multi(info->s_symbol, sizeof(info->s_symbol),
        "LIBCOUCHBASE_EVENT_PLUGIN_SYMBOL", "LCB_IOPS_SYMBOL", NULL)) {
        return -1;
    }

    info->soname = info->s_soname;
    info->symbol = info->s_symbol;
    return 1;
}

static plugin_info *find_plugin_info(lcb_io_ops_type_t iotype)
{
    plugin_info *cur;

    if (iotype == LCB_IO_OPS_DEFAULT) {
        iotype = DEFAULT_IOPS;
    }

    for (cur = builtin_plugins; cur->base; cur++) {
        if (cur->iotype == iotype) {
            return cur;
        }
    }
    return NULL;
}

static void options_from_info(struct lcb_create_io_ops_st *opts,
                              const plugin_info *info)
{
    void *cookie;

    switch (opts->version) {
    case 0:
        cookie = opts->v.v0.cookie;
        break;
    case 1:
        cookie = opts->v.v1.cookie;
        break;
    case 2:
        cookie = opts->v.v2.cookie;
        break;
    default:
        lcb_assert("unknown options version" && 0);
        cookie = NULL;
    }

    if (info->create) {
        opts->version = 2;
        opts->v.v2.create = info->create;
        opts->v.v2.cookie = cookie;
        return;
    }

    opts->version = 1;
    opts->v.v1.sofile = info->soname;
    opts->v.v1.symbol = info->symbol;
    opts->v.v1.cookie = cookie;
}

static lcb_error_t create_v2(lcb_io_opt_t *io,
                             const struct lcb_create_io_ops_st *options);

struct plugin_st {
    void *dlhandle;
    union {
        create_func_t create;
        void *voidptr;
    } func;
};

#ifndef _WIN32
static lcb_error_t get_create_func(const char *image,
                                   const char *symbol,
                                   struct plugin_st *plugin,
                                   int do_warn)
{
    void *dlhandle = dlopen(image, RTLD_NOW | RTLD_LOCAL);
    if (dlhandle == NULL) {
        if (do_warn) {
            fprintf(stderr,
                    "[libcouchbase] dlopen of %s failed with '%s'\n",
                    image, dlerror());
        }
        return LCB_DLOPEN_FAILED;
    }

    memset(plugin, 0, sizeof(*plugin));
    plugin->func.create = NULL;
    plugin->func.voidptr = dlsym(dlhandle, symbol);

    if (plugin->func.voidptr == NULL) {
        if (do_warn) {
            fprintf(stderr,
                    "[libcouchbase] dlsym (%s) -> (%s) failed: %s\n",
                    image, symbol, dlerror());
        }
        dlclose(dlhandle);
        dlhandle = NULL;
        return LCB_DLSYM_FAILED;

    } else {
        plugin->dlhandle = dlhandle;
    }
    return LCB_SUCCESS;
}

static void close_dlhandle(void *handle)
{
    dlclose(handle);
}
#else
static lcb_error_t get_create_func(const char *image,
                                   const char *symbol,
                                   struct plugin_st *plugin,
                                   int do_warn)
{
    HMODULE hLibrary = LoadLibrary(image);
    FARPROC hFunction;

    memset(plugin, 0, sizeof(*plugin));

    if (!hLibrary) {
        if (do_warn) {
            fprintf(stderr, "LoadLibrary of %s failed with code %d\n",
                    image, (int)GetLastError());
        }
        return LCB_DLOPEN_FAILED;
    }

    hFunction = GetProcAddress(hLibrary, symbol);
    if (!hFunction) {
        if (do_warn) {
            fprintf(stderr, "GetProcAddress (%s) -> (%s) failed with code %d\n",
                    image, symbol, (int)GetLastError());
        }
        FreeLibrary(hLibrary);
        return LCB_DLSYM_FAILED;
    }

    plugin->func.create = (create_func_t)hFunction;
    plugin->dlhandle = hLibrary;
    return LCB_SUCCESS;
}

static void close_dlhandle(void *handle)
{
    FreeLibrary((HMODULE)handle);
}
#endif

static int want_dl_debug = 0; /* global variable */
static lcb_error_t create_v1(lcb_io_opt_t *io,
                             const struct lcb_create_io_ops_st *options);

LIBCOUCHBASE_API
lcb_error_t lcb_destroy_io_ops(lcb_io_opt_t io)
{
    if (io) {
        void *dlhandle = io->dlhandle;
        if (io->destructor) {
            io->destructor(io);
        }
        if (dlhandle) {
            close_dlhandle(dlhandle);
        }
    }

    return LCB_SUCCESS;
}

/**
 * Note, the 'pi' is just a context variable to ensure the pointers copied
 * to the options are valid. It is *not* meant to be inspected.
 */
static lcb_error_t generate_options(plugin_info *pi,
                                    const struct lcb_create_io_ops_st *user,
                                    struct lcb_create_io_ops_st *ours,
                                    lcb_io_ops_type_t *type)
{
    if (user) {
        memcpy(ours, user, sizeof(*user));

    } else {
        memset(ours, 0, sizeof(*ours));
        ours->version = 0;
        ours->v.v0.type = LCB_IO_OPS_DEFAULT;
    }

    if (ours->version > 0) {
        if (type) {
            *type = LCB_IO_OPS_INVALID;
        }
        /* we don't handle non-v0 options */
        return LCB_SUCCESS;
    }

    if (ours->v.v0.type == LCB_IO_OPS_DEFAULT) {
        int rv;
        memset(pi, 0, sizeof(*pi));

        rv = get_env_plugin_info(pi);
        if (rv > 0) {
            options_from_info(ours, pi);

            if (type) {
                *type = pi->iotype;
            }

        } else if (rv < 0) {
            return LCB_BAD_ENVIRONMENT;

        } else {
            plugin_info *pip = find_plugin_info(LCB_IO_OPS_DEFAULT);
            lcb_assert(pip);

            if (type) {
                *type = pip->iotype;
            }

            options_from_info(ours, pip);

            /* if the plugin is dynamically loadable, we need to
             * fallback to select(2) plugin in case we cannot find the
             * create function */
            if (ours->version == 1) {
                struct plugin_st plugin;
                int want_debug;
                lcb_error_t ret;

                if (lcb_getenv_boolean_multi("LIBCOUCHBASE_DLOPEN_DEBUG",
                    "LCB_DLOPEN_DEBUG", NULL)) {
                    want_debug = 1;
                } else {
                    want_debug = want_dl_debug;
                }
                ret = get_create_func(ours->v.v1.sofile, ours->v.v1.symbol, &plugin, want_debug);
                if (ret != LCB_SUCCESS) {
                    if (type) {
                        *type = LCB_IO_OPS_SELECT;
                    }
                    ours->version = 2;
                    ours->v.v2.create = lcb_create_select_io_opts;
                    ours->v.v2.cookie = NULL;
                }
            }
        }
        return LCB_SUCCESS;

    } else {
        /** Not default, ignore environment */
        plugin_info *pip = find_plugin_info(ours->v.v0.type);
        if (!pip) {
            return LCB_NOT_SUPPORTED;
        }
        options_from_info(ours, pip);
        if (type) {
            *type = pip->iotype;
        }
        return LCB_SUCCESS;
    }
}

LIBCOUCHBASE_API
lcb_error_t lcb_create_io_ops(lcb_io_opt_t *io,
                              const struct lcb_create_io_ops_st *io_opts)
{

    struct lcb_create_io_ops_st options;
    lcb_error_t err;
    plugin_info pi;
    memset(&options, 0, sizeof(options));

    err = lcb_initialize_socket_subsystem();
    if (err != LCB_SUCCESS) {
        return err;
    }

    err = generate_options(&pi, io_opts, &options, NULL);
    if (err != LCB_SUCCESS) {
        return err;
    }

    if (options.version == 1) {
        err = create_v1(io, &options);
    } else if (options.version == 2) {
        err = create_v2(io, &options);
    } else {
        return LCB_NOT_SUPPORTED;
    }

    if (err != LCB_SUCCESS) {
        return err;
    }
    /*XXX:
     * This block of code here because the Ruby SDK relies on undocumented
     * functionality of older versions of libcouchbase in which its send/recv
     * functions assert that the number of IOV elements passed is always going
     * to be 2.
     *
     * This works around the issue by patching the send/recv functions of
     * the ruby implementation at load-time.
     *
     * This block of code will go away once the Ruby SDK is fixed and a released
     * version has been out for enough time that it won't break common existing
     * deployments.
     */
    if (io_opts && io_opts->version == 1 && io_opts->v.v1.symbol != NULL) {
        if (strstr(io_opts->v.v1.symbol, "cb_create_ruby")) {
            wire_lcb_bsd_impl(*io);
        }
    }
    return LCB_SUCCESS;
}

static lcb_error_t create_v1(lcb_io_opt_t *io,
                             const struct lcb_create_io_ops_st *options)
{
    struct plugin_st plugin;
    int want_debug;
    lcb_error_t ret;

    if (lcb_getenv_boolean_multi("LIBCOUCHBASE_DLOPEN_DEBUG",
        "LCB_DLOPEN_DEBUG", NULL)) {
        want_debug = 1;
    } else {
        want_debug = want_dl_debug;
    }
    ret = get_create_func(options->v.v1.sofile,
                          options->v.v1.symbol, &plugin, want_debug);
    if (ret != LCB_SUCCESS) {
        /* try to look up the symbol in the current image */
        lcb_error_t ret2 = get_create_func(NULL, options->v.v1.symbol, &plugin, want_debug);
        if (ret2 != LCB_SUCCESS) {
#ifndef _WIN32
            char path[PATH_MAX];
            /* try to look up the so-file in the libdir */
            snprintf(path, PATH_MAX, "%s/%s", LCB_LIBDIR, options->v.v1.sofile);
            ret2 = get_create_func(path, options->v.v1.symbol, &plugin, want_debug);
#endif
            if (ret2 != LCB_SUCCESS) {
                /* return original error to allow caller to fix it */
                return ret;
            }
        }
    }

    ret = plugin.func.create(0, io, options->v.v1.cookie);
    if (ret != LCB_SUCCESS) {
        if (options->v.v1.sofile != NULL) {
            close_dlhandle(plugin.dlhandle);
        }
        return LCB_CLIENT_ENOMEM;
    } else {
        lcb_io_opt_t iop = *io;
        iop->dlhandle = plugin.dlhandle;
        /* check if plugin selected compatible version */
        if (iop->version < 0 || iop->version > 3) {
            lcb_destroy_io_ops(iop);
            return LCB_PLUGIN_VERSION_MISMATCH;
        }
    }

    return LCB_SUCCESS;
}

static lcb_error_t create_v2(lcb_io_opt_t *io,
                             const struct lcb_create_io_ops_st *options)
{
    lcb_error_t ret;

    ret = options->v.v2.create(0, io, options->v.v2.cookie);
    if (ret != LCB_SUCCESS) {
        return ret;
    } else {
        lcb_io_opt_t iop = *io;
        /* check if plugin selected compatible version */
        if (iop->version < 0 || iop->version > 3) {
            lcb_destroy_io_ops(iop);
            return LCB_PLUGIN_VERSION_MISMATCH;
        }
    }

    return LCB_SUCCESS;
}

lcb_error_t lcb_iops_cntl_handler(int mode,
                                  lcb_t instance, int cmd, void *arg)
{
    (void)instance;

    switch (cmd) {
    case LCB_CNTL_IOPS_DEFAULT_TYPES: {
        struct lcb_create_io_ops_st options;
        struct lcb_cntl_iops_info_st *info = arg;
        lcb_error_t err;
        plugin_info pi;

        memset(&options, 0, sizeof(options));
        if (mode != LCB_CNTL_GET) {
            return LCB_NOT_SUPPORTED;
        }

        if (info->version != 0) {
            return LCB_EINVAL;
        }

        info->v.v0.os_default = DEFAULT_IOPS;

        err = generate_options(&pi,
                               info->v.v0.options,
                               &options,
                               &info->v.v0.effective);

        if (err != LCB_SUCCESS) {
            return LCB_ERROR;
        }

        return LCB_SUCCESS;
    }

    case LCB_CNTL_IOPS_DLOPEN_DEBUG: {
        int *usr = arg;
        if (mode == LCB_CNTL_SET) {
            want_dl_debug = *usr;
        } else {
            *usr = want_dl_debug;
        }
        return LCB_SUCCESS;
    }

    default:
        return LCB_EINVAL;

    }

}

/* In-library wrapper version */
LIBCOUCHBASE_API
void
lcb_iops_wire_bsd_impl2(lcb_bsd_procs *procs, int version)
{
    wire_lcb_bsd_impl2(procs, version);
}