/*
Unix SMB/CIFS implementation.
byte range lock tester - with local filesystem support
Copyright (C) Andrew Tridgell 1999
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "includes.h"
#include "system/passwd.h"
#include "lib/events/events.h"
static fstring password;
static fstring username;
static int got_pass;
static int numops = 1000;
static bool showall;
static bool analyze;
static bool hide_unlock_fails;
static bool use_oplocks;
#define FILENAME "\\locktest.dat"
#define LOCKRANGE 100
#define LOCKBASE 0
/*
#define LOCKBASE (0x40000000 - 50)
*/
#define READ_PCT 50
#define LOCK_PCT 25
#define UNLOCK_PCT 65
#define RANGE_MULTIPLE 1
#define NSERVERS 2
#define NCONNECTIONS 2
#define NUMFSTYPES 2
#define NFILES 2
#define LOCK_TIMEOUT 0
#define FSTYPE_SMB 0
#define FSTYPE_NFS 1
struct record {
char r1, r2;
char conn, f, fstype;
unsigned int start, len;
char needed;
};
static struct record *recorded;
static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
{
char *path;
int ret;
switch (fstype) {
case FSTYPE_SMB:
return smbcli_open(c, fname, flags, DENY_NONE);
case FSTYPE_NFS:
asprintf(&path, "%s%s", nfs, fname);
string_replace(path,'\\', '/');
ret = open(path, flags, 0666);
SAFE_FREE(Path);
return ret;
}
return -1;
}
static bool try_close(struct smbcli_state *c, int fstype, int fd)
{
switch (fstype) {
case FSTYPE_SMB:
return smbcli_close(c, fd);
case FSTYPE_NFS:
return close(fd) == 0;
}
return false;
}
static bool try_lock(struct smbcli_state *c, int fstype,
int fd, unsigned int start, unsigned int len,
enum brl_type op)
{
struct flock lock;
switch (fstype) {
case FSTYPE_SMB:
return smbcli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
case FSTYPE_NFS:
lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = start;
lock.l_len = len;
lock.l_pid = getpid();
return fcntl(fd,F_SETLK,&lock) == 0;
}
return false;
}
static bool try_unlock(struct smbcli_state *c, int fstype,
int fd, unsigned int start, unsigned int len)
{
struct flock lock;
switch (fstype) {
case FSTYPE_SMB:
return smbcli_unlock(c, fd, start, len);
case FSTYPE_NFS:
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = start;
lock.l_len = len;
lock.l_pid = getpid();
return fcntl(fd,F_SETLK,&lock) == 0;
}
return false;
}
/*****************************************************
return a connection to a server
*******************************************************/
static struct smbcli_state *connect_one(TALLOC_CTX *mem_ctx,
char *share, const char **ports,
struct smb_options *options,
struct smb_options *session_options,
struct gensec_settings *gensec_settings,
struct tevent_context *ev)
{
struct smbcli_state *c;
char *server_n;
char *server;
char *myname;
static int count;
NTSTATUS nt_status;
server = talloc_strdup(mem_ctx, share+2);
share = strchr_m(server,'\\');
if (!share) return NULL;
*share = 0;
share++;
server_n = server;
if (!got_pass) {
char *pass = getpass("Password: ");
if (pass) {
password = talloc_strdup(mem_ctx, pass);
}
}
myname = talloc_asprintf(mem_ctx, "lock-%u-%u", getpid(), count++);
nt_status = smbcli_full_connection(NULL,
&c, myname, server_n, ports, share, NULL,
username, lp_workgroup(), password, ev,
options, session_options, gensec_settings);
if (!NT_STATUS_IS_OK(nt_status)) {
DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
return NULL;
}
c->use_oplocks = use_oplocks;
return c;
}
static void reconnect(TALLOC_CTX *mem_ctx,
struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
char *nfs[NSERVERS],
int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
const char **ports,
struct smbcli_options *options,
struct smbcli_session_options *session_options,
struct gensec_settings *gensec_settings,
struct tevent_context *ev,
char *share1, char *share2)
{
int server, conn, f, fstype;
char *share[2];
share[0] = share1;
share[1] = share2;
fstype = FSTYPE_SMB;
for (server=0;serverconn;
unsigned int f = rec->f;
unsigned int fstype = rec->fstype;
unsigned int start = rec->start;
unsigned int len = rec->len;
unsigned int r1 = rec->r1;
unsigned int r2 = rec->r2;
enum brl_type op;
int server;
bool ret[NSERVERS];
if (r1 < READ_PCT) {
op = READ_LOCK;
} else {
op = WRITE_LOCK;
}
if (r2 < LOCK_PCT) {
/* set a lock */
for (server=0;server %u:%u\n",
conn, fstype, f,
start, start+len-1, len,
op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
ret[0], ret[1]);
}
if (ret[0] != ret[1]) return false;
} else if (r2 < LOCK_PCT+UNLOCK_PCT) {
/* unset a lock */
for (server=0;server %u:%u\n",
conn, fstype, f,
start, start+len-1, len,
ret[0], ret[1]);
}
if (!hide_unlock_fails && ret[0] != ret[1]) return false;
} else {
/* reopen the file */
for (server=0;server