2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2009
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
21 #include <sys/socket.h>
23 #include <libsmbclient.h>
25 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
27 static char *smb_domain;
28 static char *smb_user;
29 static char *smb_password;
30 static char *smb_server;
31 static char *smb_share;
33 typedef struct _data_t {
38 typedef struct _smb_handle_t {
42 /* a tree to store all open handles, indexed by path */
43 typedef struct _tree_t {
46 struct _tree_t *parent;
48 struct _tree_t *right;
51 static tree_t *open_files;
54 static tree_t *find_path(tree_t *tree, const char *path)
62 i = strcmp(path, tree->path.dptr);
67 return find_path(tree->left, path);
70 return find_path(tree->right, path);
73 static smb_handle_t *lookup_path(const char *path)
77 t = find_path(open_files, path);
85 static void free_node(tree_t *t)
87 free(discard_const(t->path.dptr));
91 static void delete_path(const char *path)
95 t = find_path(open_files, path);
100 /* we have a left child */
104 for(tmp_tree=t->left;tmp_tree->right;tmp_tree=tmp_tree->right)
106 tmp_tree->right = t->right;
108 t->right->parent = tmp_tree;
111 if (t->parent == NULL) {
112 open_files = tmp_tree;
113 tmp_tree->parent = NULL;
118 if (t->parent->left == t) {
119 t->parent->left = t->left;
121 t->left->parent = t->parent;
127 t->parent->right = t->left;
129 t->left->parent = t->parent;
135 /* we only have a right child */
139 for(tmp_tree=t->right;tmp_tree->left;tmp_tree=tmp_tree->left)
141 tmp_tree->left = t->left;
143 t->left->parent = tmp_tree;
146 if (t->parent == NULL) {
147 open_files = tmp_tree;
148 tmp_tree->parent = NULL;
153 if (t->parent->left == t) {
154 t->parent->left = t->right;
156 t->right->parent = t->parent;
162 t->parent->right = t->right;
164 t->right->parent = t->parent;
170 /* we are a leaf node */
171 if (t->parent == NULL) {
174 if (t->parent->left == t) {
175 t->parent->left = NULL;
177 t->parent->right = NULL;
184 static void insert_path(const char *path, smb_handle_t *hnd)
190 tmp_t = find_path(open_files, path);
195 t = malloc(sizeof(tree_t));
197 fprintf(stderr, "MALLOC failed to allocate tree_t in insert_fhandle\n");
201 t->path.dptr = strdup(path);
202 if (t->path.dptr == NULL) {
203 fprintf(stderr, "STRDUP failed to allocate key in insert_fhandle\n");
206 t->path.dsize = strlen(path);
214 if (open_files == NULL) {
221 i = strcmp(t->path.dptr, tmp_t->path.dptr);
223 tmp_t->handle = t->handle;
224 free(discard_const(t->path.dptr));
229 if (tmp_t->left == NULL) {
237 if (tmp_t->right == NULL) {
242 tmp_t = tmp_t->right;
254 static void failed(struct child_struct *child)
257 fprintf(stderr, "ERROR: child %d failed at line %d\n", child->id, child->line);
261 static int check_status(int ret, const char *status)
263 if (!strcmp(status, "*")) {
267 if ((!strcmp(status, "SUCCESS")) && (ret == 0)) {
271 if ((!strcmp(status, "ERROR")) && (ret != 0)) {
279 void smb_auth_fn(const char *server, const char *share, char *wrkgrp, int wrkgrplen, char *user, int userlen, char *passwd, int passwdlen)
284 if (smb_domain != NULL) {
285 strncpy(wrkgrp, smb_domain, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
287 strncpy(user, smb_user, userlen - 1); user[userlen - 1] = 0;
288 strncpy(passwd, smb_password, passwdlen - 1); passwd[passwdlen - 1] = 0;
291 static int smb_init(void)
298 if (options.smb_share == NULL) {
299 fprintf(stderr, "You must specify --smb-share=<share> with the \"smb\" backend.\n");
302 if (options.smb_share[0] != '/' || options.smb_share[1] != '/') {
303 fprintf(stderr, "--smb-share Must be of the form //SERVER/SHARE[/PATH]\n");
306 smb_server = strdup(options.smb_share+2);
307 tmp = index(smb_server, '/');
309 fprintf(stderr, "--smb-share Must be of the form //SERVER/SHARE[/PATH]\n");
315 if (options.smb_user == NULL) {
316 fprintf(stderr, "You must specify --smb-user=[<domain>/]<user>%%<password> with the \"smb\" backend.\n");
320 smb_domain = strdup(options.smb_user);
321 tmp = index(smb_domain, '/');
323 smb_user = smb_domain;
329 tmp = index(smb_user, '%');
333 smb_password = tmp+1;
337 ctx = smbc_new_context();
339 fprintf(stderr, "Could not allocate SMB Context\n");
343 ctx->callbacks.auth_fn = smb_auth_fn;
345 if (!smbc_init_context(ctx)) {
346 smbc_free_context(ctx, 0);
347 fprintf(stderr, "failed to initialize context\n");
350 smbc_set_context(ctx);
352 asprintf(&str, "smb://%s/%s", smb_server, smb_share);
353 ret = smbc_opendir(str);
357 fprintf(stderr, "Failed to access //%s/%s\n", smb_server, smb_share);
361 smbc_free_context(ctx, 1);
365 static void smb_setup(struct child_struct *child)
367 struct smb_child *ctx;
371 ctx = malloc(sizeof(struct smb_child));
373 fprintf(stderr, "Failed to malloc child ctx\n");
378 ctx->ctx = smbc_new_context();
379 if (ctx->ctx == NULL) {
380 fprintf(stderr, "Could not allocate SMB Context\n");
384 ctx->ctx->callbacks.auth_fn = smb_auth_fn;
386 if (!smbc_init_context(ctx->ctx)) {
387 smbc_free_context(ctx->ctx, 0);
388 fprintf(stderr, "failed to initialize context\n");
391 smbc_set_context(ctx->ctx);
394 /* create clients and /clients/client? */
395 asprintf(&str, "smb://%s/%s/clients", smb_server, smb_share);
396 smbc_mkdir(str, 0777);
398 asprintf(&str, "smb://%s/%s/clients/client%d", smb_server, smb_share, child->id);
399 smbc_mkdir(str, 0777);
403 asprintf(&smb_share, "%s/clients/client%d", smb_share, child->id);
406 static void smb_mkdir(struct dbench_op *op)
414 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, dir);
416 ret = smbc_mkdir(str, 0777);
419 if (check_status(ret, op->status)) {
420 fprintf(stderr, "[%d] MKDIR \"%s\" failed - expected %s, got %d\n", op->child->line, dir, op->status, ret);
425 static void smb_rmdir(struct dbench_op *op)
432 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, dir);
433 ret = smbc_rmdir(str);
436 if (check_status(ret, op->status)) {
437 fprintf(stderr, "[%d] RMDIR \"%s\" failed - expected %s, got %d\n", op->child->line, dir, op->status, ret);
442 static void smb_open(struct dbench_op *op)
449 if (op->params[0] & 0x01) {
452 if (op->params[0] & 0x02) {
455 if (op->params[0] & 0x04) {
458 if (op->params[0] & 0x08) {
461 if (op->params[0] & 0x10) {
464 if (op->params[0] & 0x20) {
467 if (op->params[0] & 0x40) {
471 file = op->fname + 2;
472 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, file);
474 hnd.fd = smbc_open(str, flags, 0777);
477 if (check_status(hnd.fd<0?-1:0, op->status)) {
478 fprintf(stderr, "[%d] OPEN \"%s\" failed\n", op->child->line, file);
483 insert_path(file, &hnd);
487 static void smb_close(struct dbench_op *op)
493 file = op->fname + 2;
495 hnd = lookup_path(file);
497 fprintf(stderr, "[%d] CLOSE \"%s\" failed. This file is not open.\n", op->child->line, file);
502 ret = smbc_close(hnd->fd);
505 if (check_status(ret, op->status)) {
506 fprintf(stderr, "[%d] CLOSE \"%s\" failed\n", op->child->line, file);
512 static void smb_write(struct dbench_op *op)
521 offset = op->params[0];
522 length = op->params[1];
523 if (length > 65536) {
527 file = op->fname + 2;
529 hnd = lookup_path(file);
531 fprintf(stderr, "[%d] WRITE \"%s\" failed. This file is not open.\n", op->child->line, file);
537 smbc_lseek(hnd->fd, offset, SEEK_SET);
538 ret = smbc_write(hnd->fd, garbage, length);
540 if (check_status(ret==(int)length?0:-1, op->status)) {
541 fprintf(stderr, "[%d] WRITE \"%s\" failed\n", op->child->line, file);
545 op->child->bytes += length;
548 static void smb_read(struct dbench_op *op)
557 offset = op->params[0];
558 length = op->params[1];
559 if (length > 65536) {
563 file = op->fname + 2;
565 hnd = lookup_path(file);
567 fprintf(stderr, "[%d] READ \"%s\" failed. This file is not open.\n", op->child->line, file);
573 smbc_lseek(hnd->fd, offset, SEEK_SET);
574 ret = smbc_read(hnd->fd, garbage, length);
576 if (check_status(ret==(int)length?0:-1, op->status)) {
577 fprintf(stderr, "[%d] READ \"%s\" failed\n", op->child->line, file);
581 op->child->bytes += length;
585 static void smb_unlink(struct dbench_op *op)
591 path = op->fname + 2;
592 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, path);
594 ret = smbc_unlink(str);
597 if (check_status(ret, op->status)) {
598 fprintf(stderr, "[%d] UNLINK \"%s\" failed\n", op->child->line, path);
604 static void recursive_delete_tree(struct dbench_op *op, const char *url)
607 struct smbc_dirent *dirent;
609 dir = smbc_opendir(url);
611 fprintf(stderr, "[%d] Deltree \"%s\" failed\n", op->child->line, url);
614 while((dirent = smbc_readdir(dir))) {
617 asprintf(&path, "%s/%s", url, dirent->name);
618 if (!strcmp(dirent->name, ".")) {
621 if (!strcmp(dirent->name, "..")) {
624 if (dirent->smbc_type == SMBC_DIR) {
625 recursive_delete_tree(op, path);
637 static void smb_readdir(struct dbench_op *op)
643 path = op->fname + 2;
644 asprintf(&url, "smb://%s/%s/%s", smb_server, smb_share, path);
646 dir = smbc_opendir(url);
649 fprintf(stderr, "[%d] READDIR \"%s\" failed\n", op->child->line, url);
655 static void smb_deltree(struct dbench_op *op)
660 path = op->fname + 2;
661 asprintf(&url, "smb://%s/%s/%s", smb_server, smb_share, path);
662 recursive_delete_tree(op, url);
666 static void smb_cleanup(struct child_struct *child)
668 struct smb_child *ctx = child->private;
670 struct dbench_op fake_op;
672 asprintf(&url, "smb://%s/%s", smb_server, smb_share);
673 recursive_delete_tree(&fake_op, url);
676 smbc_free_context(ctx->ctx, 1);
682 static struct backend_op ops[] = {
683 { "Deltree", smb_deltree },
684 { "CLOSE", smb_close },
685 { "MKDIR", smb_mkdir },
686 { "OPEN", smb_open },
687 { "READ", smb_read },
688 { "READDIR", smb_readdir },
689 { "RMDIR", smb_rmdir },
690 { "UNLINK", smb_unlink },
691 { "WRITE", smb_write },
695 struct nb_operations smb_ops = {
696 .backend_name = "smbbench",
699 .cleanup = smb_cleanup,