mirror of
https://github.com/upa/mscp.git
synced 2026-03-17 05:47:35 +08:00
change indent from space to tab
This commit is contained in:
50
src/atomic.h
50
src/atomic.h
@@ -9,12 +9,12 @@ typedef int refcnt;
|
|||||||
|
|
||||||
static inline void refcnt_inc(refcnt *cnt)
|
static inline void refcnt_inc(refcnt *cnt)
|
||||||
{
|
{
|
||||||
__sync_add_and_fetch(cnt, 1);
|
__sync_add_and_fetch(cnt, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline refcnt refcnt_dec(refcnt *cnt)
|
static inline refcnt refcnt_dec(refcnt *cnt)
|
||||||
{
|
{
|
||||||
return __sync_sub_and_fetch(cnt, 1);
|
return __sync_sub_and_fetch(cnt, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -22,37 +22,37 @@ typedef pthread_mutex_t lock;
|
|||||||
|
|
||||||
static inline void lock_init(lock *l)
|
static inline void lock_init(lock *l)
|
||||||
{
|
{
|
||||||
pthread_mutex_init(l, NULL);
|
pthread_mutex_init(l, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void lock_acquire(lock *l)
|
static inline void lock_acquire(lock *l)
|
||||||
{
|
{
|
||||||
int ret = pthread_mutex_lock(l);
|
int ret = pthread_mutex_lock(l);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case EINVAL:
|
case EINVAL:
|
||||||
pr_err("invalid mutex\n");
|
pr_err("invalid mutex\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
case EDEADLK:
|
case EDEADLK:
|
||||||
pr_err("a deadlock would occur\n");
|
pr_err("a deadlock would occur\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void lock_release(lock *l)
|
static inline void lock_release(lock *l)
|
||||||
{
|
{
|
||||||
int ret = pthread_mutex_unlock(l);
|
int ret = pthread_mutex_unlock(l);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case EINVAL:
|
case EINVAL:
|
||||||
pr_err("invalid mutex\n");
|
pr_err("invalid mutex\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
case EPERM:
|
case EPERM:
|
||||||
pr_err("this thread does not hold this mutex\n");
|
pr_err("this thread does not hold this mutex\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* _ATOMIC_H_ */
|
#endif /* _ATOMIC_H_ */
|
||||||
|
|||||||
1088
src/file.c
1088
src/file.c
File diff suppressed because it is too large
Load Diff
34
src/file.h
34
src/file.h
@@ -10,18 +10,18 @@
|
|||||||
#include <atomic.h>
|
#include <atomic.h>
|
||||||
|
|
||||||
struct file {
|
struct file {
|
||||||
struct list_head list; /* sscp->file_list */
|
struct list_head list; /* sscp->file_list */
|
||||||
|
|
||||||
char path[PATH_MAX]; /* copy source path */
|
char path[PATH_MAX]; /* copy source path */
|
||||||
bool remote; /* source is remote */
|
bool remote; /* source is remote */
|
||||||
size_t size; /* size of this file */
|
size_t size; /* size of this file */
|
||||||
|
|
||||||
char dst_path[PATH_MAX]; /* copy destination path */
|
char dst_path[PATH_MAX]; /* copy destination path */
|
||||||
bool dst_remote; /* destination is remote */
|
bool dst_remote; /* destination is remote */
|
||||||
|
|
||||||
int state; /* destination file state */
|
int state; /* destination file state */
|
||||||
lock lock; /* mutex to protect state */
|
lock lock; /* mutex to protect state */
|
||||||
refcnt refcnt; /* chunks referencing this file */
|
refcnt refcnt; /* chunks referencing this file */
|
||||||
};
|
};
|
||||||
#define FILE_STATE_INIT 0
|
#define FILE_STATE_INIT 0
|
||||||
#define FILE_STATE_OPENED 1
|
#define FILE_STATE_OPENED 1
|
||||||
@@ -51,26 +51,26 @@ struct file {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
struct chunk {
|
struct chunk {
|
||||||
struct list_head list; /* sscp->chunk_list */
|
struct list_head list; /* sscp->chunk_list */
|
||||||
struct file *f;
|
struct file *f;
|
||||||
size_t off; /* offset of this chunk on the file f */
|
size_t off; /* offset of this chunk on the file f */
|
||||||
size_t len; /* length of this chunk */
|
size_t len; /* length of this chunk */
|
||||||
size_t done; /* copied bytes for this chunk by a thread */
|
size_t done; /* copied bytes for this chunk by a thread */
|
||||||
};
|
};
|
||||||
|
|
||||||
char *file_find_hostname(char *path);
|
char *file_find_hostname(char *path);
|
||||||
bool file_has_hostname(char *path);
|
bool file_has_hostname(char *path);
|
||||||
|
|
||||||
int file_fill(sftp_session sftp, struct list_head *file_list, char **src_array, int cnt,
|
int file_fill(sftp_session sftp, struct list_head *file_list, char **src_array, int cnt,
|
||||||
char *dst);
|
char *dst);
|
||||||
|
|
||||||
int chunk_fill(struct list_head *file_list, struct list_head *chunk_list,
|
int chunk_fill(struct list_head *file_list, struct list_head *chunk_list,
|
||||||
int nr_conn, int min_chunk_sz, int max_chunk_sz);
|
int nr_conn, int min_chunk_sz, int max_chunk_sz);
|
||||||
|
|
||||||
struct chunk *chunk_acquire(struct list_head *chunk_list);
|
struct chunk *chunk_acquire(struct list_head *chunk_list);
|
||||||
int chunk_prepare(struct chunk *c, sftp_session sftp);
|
int chunk_prepare(struct chunk *c, sftp_session sftp);
|
||||||
int chunk_copy(struct chunk *c, sftp_session sftp,
|
int chunk_copy(struct chunk *c, sftp_session sftp,
|
||||||
size_t sftp_buf_sz, size_t io_buf_sz, size_t *counter);
|
size_t sftp_buf_sz, size_t io_buf_sz, size_t *counter);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
void file_dump(struct list_head *file_list);
|
void file_dump(struct list_head *file_list);
|
||||||
|
|||||||
760
src/main.c
760
src/main.c
@@ -22,28 +22,28 @@
|
|||||||
/* XXX: need to investigate max buf size for sftp_read/sftp_write */
|
/* XXX: need to investigate max buf size for sftp_read/sftp_write */
|
||||||
|
|
||||||
struct sscp {
|
struct sscp {
|
||||||
char *host; /* remote host (and username) */
|
char *host; /* remote host (and username) */
|
||||||
struct ssh_opts *opts; /* ssh parameters */
|
struct ssh_opts *opts; /* ssh parameters */
|
||||||
sftp_session ctrl; /* control sftp session */
|
sftp_session ctrl; /* control sftp session */
|
||||||
|
|
||||||
struct list_head file_list;
|
struct list_head file_list;
|
||||||
struct list_head chunk_list; /* stack of chunks */
|
struct list_head chunk_list; /* stack of chunks */
|
||||||
lock chunk_lock; /* lock for chunk list */
|
lock chunk_lock; /* lock for chunk list */
|
||||||
|
|
||||||
char *target;
|
char *target;
|
||||||
|
|
||||||
int sftp_buf_sz, io_buf_sz;
|
int sftp_buf_sz, io_buf_sz;
|
||||||
|
|
||||||
struct timeval start; /* timestamp of starting copy */
|
struct timeval start; /* timestamp of starting copy */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sscp_thread {
|
struct sscp_thread {
|
||||||
struct sscp *sscp;
|
struct sscp *sscp;
|
||||||
sftp_session sftp;
|
sftp_session sftp;
|
||||||
|
|
||||||
pthread_t tid;
|
pthread_t tid;
|
||||||
size_t done; /* copied bytes */
|
size_t done; /* copied bytes */
|
||||||
bool finished;
|
bool finished;
|
||||||
};
|
};
|
||||||
|
|
||||||
void *sscp_copy_thread(void *arg);
|
void *sscp_copy_thread(void *arg);
|
||||||
@@ -55,175 +55,175 @@ int nr_threads;
|
|||||||
|
|
||||||
void stop_copy_threads(int sig)
|
void stop_copy_threads(int sig)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
pr("stopping...\n");
|
pr("stopping...\n");
|
||||||
for (n = 0; n < nr_threads; n++) {
|
for (n = 0; n < nr_threads; n++) {
|
||||||
pthread_cancel(threads[n].tid);
|
pthread_cancel(threads[n].tid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void usage(bool print_help) {
|
void usage(bool print_help) {
|
||||||
printf("sscp: super scp, copy files over multiple ssh connections\n"
|
printf("sscp: super scp, copy files over multiple ssh connections\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Usage: sscp [CvqDdh] [-n nr_conns] [-s min_chunk_sz] [-S max_chunk_sz]\n"
|
"Usage: sscp [CvqDdh] [-n nr_conns] [-s min_chunk_sz] [-S max_chunk_sz]\n"
|
||||||
" [-b sftp_buf_sz] [-B io_buf_sz]\n"
|
" [-b sftp_buf_sz] [-B io_buf_sz]\n"
|
||||||
" [-l login_name] [-p port] [-i identity_file]\n"
|
" [-l login_name] [-p port] [-i identity_file]\n"
|
||||||
" [-c cipher_spec] source ... target\n"
|
" [-c cipher_spec] source ... target\n"
|
||||||
"\n");
|
"\n");
|
||||||
|
|
||||||
if (!print_help)
|
|
||||||
return;
|
|
||||||
|
|
||||||
printf(" -n NR_CONNECTIONS number of connections (default: half of # of cpu cores)\n"
|
if (!print_help)
|
||||||
" -s MIN_CHUNK_SIZE min chunk size (default: 64MB)\n"
|
return;
|
||||||
" -S MAX_CHUNK_SIZE max chunk size (default: filesize / nr_conn)\n"
|
|
||||||
" -b SFTP_BUF_SIZE buf size for sftp_read/write (default 131072B)\n"
|
printf(" -n NR_CONNECTIONS number of connections (default: half of # of cpu cores)\n"
|
||||||
" -B IO_BUF_SIZE buf size for read/write (default 131072B)\n"
|
" -s MIN_CHUNK_SIZE min chunk size (default: 64MB)\n"
|
||||||
" Note that this value is derived from\n"
|
" -S MAX_CHUNK_SIZE max chunk size (default: filesize / nr_conn)\n"
|
||||||
" qemu/block/ssh.c. need investigation...\n"
|
" -b SFTP_BUF_SIZE buf size for sftp_read/write (default 131072B)\n"
|
||||||
" -v increment verbose output level\n"
|
" -B IO_BUF_SIZE buf size for read/write (default 131072B)\n"
|
||||||
" -q disable output\n"
|
" Note that this value is derived from\n"
|
||||||
" -D dry run\n"
|
" qemu/block/ssh.c. need investigation...\n"
|
||||||
"\n"
|
" -v increment verbose output level\n"
|
||||||
" -l LOGIN_NAME login name\n"
|
" -q disable output\n"
|
||||||
" -p PORT port number\n"
|
" -D dry run\n"
|
||||||
" -i IDENTITY identity file for publickey authentication\n"
|
"\n"
|
||||||
" -c CIPHER cipher spec, see `ssh -Q cipher`\n"
|
" -l LOGIN_NAME login name\n"
|
||||||
" -C enable compression on libssh\n"
|
" -p PORT port number\n"
|
||||||
" -d increment ssh debug output level\n"
|
" -i IDENTITY identity file for publickey authentication\n"
|
||||||
" -h print this help\n"
|
" -c CIPHER cipher spec, see `ssh -Q cipher`\n"
|
||||||
"\n");
|
" -C enable compression on libssh\n"
|
||||||
|
" -d increment ssh debug output level\n"
|
||||||
|
" -h print this help\n"
|
||||||
|
"\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
char *find_hostname(int ind, int argc, char **argv)
|
char *find_hostname(int ind, int argc, char **argv)
|
||||||
{
|
{
|
||||||
char *h, *hostnames[argc];
|
char *h, *hostnames[argc];
|
||||||
int n, cnt = 0;
|
int n, cnt = 0;
|
||||||
|
|
||||||
for (n = ind; n < argc; n++) {
|
for (n = ind; n < argc; n++) {
|
||||||
h = file_find_hostname(argv[n]);
|
h = file_find_hostname(argv[n]);
|
||||||
if (h)
|
if (h)
|
||||||
hostnames[cnt++] = h;
|
hostnames[cnt++] = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cnt == 0)
|
if (cnt == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* check all hostnames are identical */
|
/* check all hostnames are identical */
|
||||||
for (n = 1; n < cnt; n++) {
|
for (n = 1; n < cnt; n++) {
|
||||||
int s1 = strlen(hostnames[n - 1]);
|
int s1 = strlen(hostnames[n - 1]);
|
||||||
int s2 = strlen(hostnames[n]);
|
int s2 = strlen(hostnames[n]);
|
||||||
if (s1 != s2) {
|
if (s1 != s2) {
|
||||||
pr_err("different hostnames: %s and %s\n",
|
pr_err("different hostnames: %s and %s\n",
|
||||||
hostnames[n - 1], hostnames[n]);
|
hostnames[n - 1], hostnames[n]);
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
if (strncmp(hostnames[n - 1], hostnames[n], s1) != 0) {
|
if (strncmp(hostnames[n - 1], hostnames[n], s1) != 0) {
|
||||||
pr_err("different hostnames: %s and %s\n",
|
pr_err("different hostnames: %s and %s\n",
|
||||||
hostnames[n - 1], hostnames[n]);
|
hostnames[n - 1], hostnames[n]);
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (n = 1; n < cnt; n++) {
|
for (n = 1; n < cnt; n++) {
|
||||||
free(hostnames[n]);
|
free(hostnames[n]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return hostnames[0];
|
return hostnames[0];
|
||||||
|
|
||||||
err_out:
|
err_out:
|
||||||
for (n = 0; n < cnt; n++) {
|
for (n = 0; n < cnt; n++) {
|
||||||
free(hostnames[n]);
|
free(hostnames[n]);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct sscp sscp;
|
struct sscp sscp;
|
||||||
struct ssh_opts opts;
|
struct ssh_opts opts;
|
||||||
int min_chunk_sz = DEFAULT_MIN_CHUNK_SZ;
|
int min_chunk_sz = DEFAULT_MIN_CHUNK_SZ;
|
||||||
int max_chunk_sz = 0;
|
int max_chunk_sz = 0;
|
||||||
int verbose = 1;
|
int verbose = 1;
|
||||||
bool dryrun = false;
|
bool dryrun = false;
|
||||||
int ret = 0, n;
|
int ret = 0, n;
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
memset(&opts, 0, sizeof(opts));
|
memset(&opts, 0, sizeof(opts));
|
||||||
memset(&sscp, 0, sizeof(sscp));
|
memset(&sscp, 0, sizeof(sscp));
|
||||||
INIT_LIST_HEAD(&sscp.file_list);
|
INIT_LIST_HEAD(&sscp.file_list);
|
||||||
INIT_LIST_HEAD(&sscp.chunk_list);
|
INIT_LIST_HEAD(&sscp.chunk_list);
|
||||||
lock_init(&sscp.chunk_lock);
|
lock_init(&sscp.chunk_lock);
|
||||||
sscp.sftp_buf_sz = DEFAULT_SFTP_BUF_SZ;
|
sscp.sftp_buf_sz = DEFAULT_SFTP_BUF_SZ;
|
||||||
sscp.io_buf_sz = DEFAULT_IO_BUF_SZ;
|
sscp.io_buf_sz = DEFAULT_IO_BUF_SZ;
|
||||||
|
|
||||||
nr_threads = (int)(nr_cpus() / 2);
|
nr_threads = (int)(nr_cpus() / 2);
|
||||||
nr_threads = nr_threads == 0 ? 1 : nr_threads;
|
nr_threads = nr_threads == 0 ? 1 : nr_threads;
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "n:s:S:b:B:vqDl:p:i:c:Cdh")) != -1) {
|
while ((ch = getopt(argc, argv, "n:s:S:b:B:vqDl:p:i:c:Cdh")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'n':
|
case 'n':
|
||||||
nr_threads = atoi(optarg);
|
nr_threads = atoi(optarg);
|
||||||
if (nr_threads < 1) {
|
if (nr_threads < 1) {
|
||||||
pr_err("invalid number of connections: %s\n", optarg);
|
pr_err("invalid number of connections: %s\n", optarg);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
min_chunk_sz = atoi(optarg);
|
min_chunk_sz = atoi(optarg);
|
||||||
if (min_chunk_sz < getpagesize()) {
|
if (min_chunk_sz < getpagesize()) {
|
||||||
pr_err("min chunk size must be "
|
pr_err("min chunk size must be "
|
||||||
"larger than or equal to %d: %s\n",
|
"larger than or equal to %d: %s\n",
|
||||||
getpagesize(), optarg);
|
getpagesize(), optarg);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (min_chunk_sz % getpagesize() != 0) {
|
if (min_chunk_sz % getpagesize() != 0) {
|
||||||
pr_err("min chunk size must be "
|
pr_err("min chunk size must be "
|
||||||
"multiple of page size %d: %s\n",
|
"multiple of page size %d: %s\n",
|
||||||
getpagesize(), optarg);
|
getpagesize(), optarg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
max_chunk_sz = atoi(optarg);
|
max_chunk_sz = atoi(optarg);
|
||||||
if (max_chunk_sz < getpagesize()) {
|
if (max_chunk_sz < getpagesize()) {
|
||||||
pr_err("max chunk size must be "
|
pr_err("max chunk size must be "
|
||||||
"larger than or equal to %d: %s\n",
|
"larger than or equal to %d: %s\n",
|
||||||
getpagesize(), optarg);
|
getpagesize(), optarg);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (max_chunk_sz % getpagesize() != 0) {
|
if (max_chunk_sz % getpagesize() != 0) {
|
||||||
pr_err("max chunk size must be "
|
pr_err("max chunk size must be "
|
||||||
"multiple of page size %d: %s\n",
|
"multiple of page size %d: %s\n",
|
||||||
getpagesize(), optarg);
|
getpagesize(), optarg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
sscp.sftp_buf_sz = atoi(optarg);
|
sscp.sftp_buf_sz = atoi(optarg);
|
||||||
if (sscp.sftp_buf_sz < 1) {
|
if (sscp.sftp_buf_sz < 1) {
|
||||||
pr_err("invalid buffer size: %s\n", optarg);
|
pr_err("invalid buffer size: %s\n", optarg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'B':
|
case 'B':
|
||||||
sscp.io_buf_sz = atoi(optarg);
|
sscp.io_buf_sz = atoi(optarg);
|
||||||
if (sscp.io_buf_sz < 1) {
|
if (sscp.io_buf_sz < 1) {
|
||||||
pr_err("invalid buffer size: %s\n", optarg);
|
pr_err("invalid buffer size: %s\n", optarg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
verbose++;
|
verbose++;
|
||||||
break;
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
verbose = -1;
|
verbose = -1;
|
||||||
break;
|
break;
|
||||||
case 'D':
|
case 'D':
|
||||||
dryrun = true;
|
dryrun = true;
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
opts.login_name = optarg;
|
opts.login_name = optarg;
|
||||||
break;
|
break;
|
||||||
@@ -242,322 +242,322 @@ int main(int argc, char **argv)
|
|||||||
case 'd':
|
case 'd':
|
||||||
opts.debuglevel++;
|
opts.debuglevel++;
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
usage(true);
|
usage(true);
|
||||||
return 1;
|
return 1;
|
||||||
default:
|
default:
|
||||||
usage(false);
|
usage(false);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pprint_set_level(verbose);
|
pprint_set_level(verbose);
|
||||||
|
|
||||||
if (max_chunk_sz > 0 && min_chunk_sz > max_chunk_sz) {
|
if (max_chunk_sz > 0 && min_chunk_sz > max_chunk_sz) {
|
||||||
pr_err("smaller max chunk size than min chunk size: %d < %d\n",
|
pr_err("smaller max chunk size than min chunk size: %d < %d\n",
|
||||||
max_chunk_sz, min_chunk_sz);
|
max_chunk_sz, min_chunk_sz);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc - optind < 2) {
|
if (argc - optind < 2) {
|
||||||
/* sscp needs at lease 2 (src and target) argument */
|
/* sscp needs at lease 2 (src and target) argument */
|
||||||
usage(false);
|
usage(false);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sscp.target = argv[argc - 1];
|
sscp.target = argv[argc - 1];
|
||||||
|
|
||||||
/* create control session */
|
/* create control session */
|
||||||
sscp.host = find_hostname(optind, argc, argv);
|
sscp.host = find_hostname(optind, argc, argv);
|
||||||
if (!sscp.host) {
|
if (!sscp.host) {
|
||||||
pr_err("no remote host given\n");
|
pr_err("no remote host given\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
pprint3("connecting to %s for checking destinations...\n", sscp.host);
|
pprint3("connecting to %s for checking destinations...\n", sscp.host);
|
||||||
sscp.ctrl = ssh_make_sftp_session(sscp.host, &opts);
|
sscp.ctrl = ssh_make_sftp_session(sscp.host, &opts);
|
||||||
if (!sscp.ctrl)
|
if (!sscp.ctrl)
|
||||||
return 1;
|
return 1;
|
||||||
sscp.opts = &opts; /* save ssh-able ssh_opts */
|
sscp.opts = &opts; /* save ssh-able ssh_opts */
|
||||||
|
|
||||||
|
|
||||||
/* fill file list */
|
/* fill file list */
|
||||||
ret = file_fill(sscp.ctrl, &sscp.file_list, &argv[optind], argc - optind - 1,
|
ret = file_fill(sscp.ctrl, &sscp.file_list, &argv[optind], argc - optind - 1,
|
||||||
sscp.target);
|
sscp.target);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
file_dump(&sscp.file_list);
|
file_dump(&sscp.file_list);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* fill chunk list */
|
/* fill chunk list */
|
||||||
ret = chunk_fill(&sscp.file_list, &sscp.chunk_list,
|
ret = chunk_fill(&sscp.file_list, &sscp.chunk_list,
|
||||||
nr_threads, min_chunk_sz, max_chunk_sz);
|
nr_threads, min_chunk_sz, max_chunk_sz);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
chunk_dump(&sscp.chunk_list);
|
chunk_dump(&sscp.chunk_list);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (dryrun)
|
if (dryrun)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* register SIGINT to stop thrads */
|
/* register SIGINT to stop thrads */
|
||||||
if (signal(SIGINT, stop_copy_threads) == SIG_ERR) {
|
if (signal(SIGINT, stop_copy_threads) == SIG_ERR) {
|
||||||
pr_err("cannot set signal: %s\n", strerrno());
|
pr_err("cannot set signal: %s\n", strerrno());
|
||||||
ret = 1;
|
ret = 1;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prepare thread instances */
|
/* prepare thread instances */
|
||||||
threads = calloc(nr_threads, sizeof(struct sscp_thread));
|
threads = calloc(nr_threads, sizeof(struct sscp_thread));
|
||||||
memset(threads, 0, nr_threads * sizeof(struct sscp_thread));
|
memset(threads, 0, nr_threads * sizeof(struct sscp_thread));
|
||||||
for (n = 0; n < nr_threads; n++) {
|
for (n = 0; n < nr_threads; n++) {
|
||||||
struct sscp_thread *t = &threads[n];
|
struct sscp_thread *t = &threads[n];
|
||||||
t->sscp = &sscp;
|
t->sscp = &sscp;
|
||||||
t->finished = false;
|
t->finished = false;
|
||||||
pprint3("connecting to %s for a copy thread...\n", sscp.host);
|
pprint3("connecting to %s for a copy thread...\n", sscp.host);
|
||||||
t->sftp = ssh_make_sftp_session(sscp.host, sscp.opts);
|
t->sftp = ssh_make_sftp_session(sscp.host, sscp.opts);
|
||||||
if (!t->sftp)
|
if (!t->sftp)
|
||||||
goto join_out;
|
goto join_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* spawn count thread */
|
/* spawn count thread */
|
||||||
ret = pthread_create(&mtid, NULL, sscp_monitor_thread, &sscp);
|
ret = pthread_create(&mtid, NULL, sscp_monitor_thread, &sscp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
pr_err("pthread_create error: %d\n", ret);
|
pr_err("pthread_create error: %d\n", ret);
|
||||||
stop_copy_threads(0);
|
stop_copy_threads(0);
|
||||||
goto join_out;
|
goto join_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* save start time */
|
/* save start time */
|
||||||
gettimeofday(&sscp.start, NULL);
|
gettimeofday(&sscp.start, NULL);
|
||||||
|
|
||||||
/* spawn threads */
|
/* spawn threads */
|
||||||
for (n = 0; n < nr_threads; n++) {
|
for (n = 0; n < nr_threads; n++) {
|
||||||
struct sscp_thread *t = &threads[n];
|
struct sscp_thread *t = &threads[n];
|
||||||
ret = pthread_create(&t->tid, NULL, sscp_copy_thread, t);
|
ret = pthread_create(&t->tid, NULL, sscp_copy_thread, t);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
pr_err("pthread_create error: %d\n", ret);
|
pr_err("pthread_create error: %d\n", ret);
|
||||||
stop_copy_threads(0);
|
stop_copy_threads(0);
|
||||||
goto join_out;
|
goto join_out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
join_out:
|
join_out:
|
||||||
/* waiting for threads join... */
|
/* waiting for threads join... */
|
||||||
for (n = 0; n < nr_threads; n++)
|
for (n = 0; n < nr_threads; n++)
|
||||||
if (threads[n].tid)
|
if (threads[n].tid)
|
||||||
pthread_join(threads[n].tid, NULL);
|
pthread_join(threads[n].tid, NULL);
|
||||||
|
|
||||||
if (mtid != 0) {
|
if (mtid != 0) {
|
||||||
pthread_cancel(mtid);
|
pthread_cancel(mtid);
|
||||||
pthread_join(mtid, NULL);
|
pthread_join(mtid, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
if (sscp.ctrl)
|
if (sscp.ctrl)
|
||||||
ssh_sftp_close(sscp.ctrl);
|
ssh_sftp_close(sscp.ctrl);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sscp_copy_thread_cleanup(void *arg)
|
void sscp_copy_thread_cleanup(void *arg)
|
||||||
{
|
{
|
||||||
struct sscp_thread *t = arg;
|
struct sscp_thread *t = arg;
|
||||||
if (t->sftp)
|
if (t->sftp)
|
||||||
ssh_sftp_close(t->sftp);
|
ssh_sftp_close(t->sftp);
|
||||||
t->finished = true;
|
t->finished = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *sscp_copy_thread(void *arg)
|
void *sscp_copy_thread(void *arg)
|
||||||
{
|
{
|
||||||
struct sscp_thread *t = arg;
|
struct sscp_thread *t = arg;
|
||||||
struct sscp *sscp = t->sscp;
|
struct sscp *sscp = t->sscp;
|
||||||
sftp_session sftp = t->sftp;
|
sftp_session sftp = t->sftp;
|
||||||
struct chunk *c;
|
struct chunk *c;
|
||||||
|
|
||||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||||
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
||||||
pthread_cleanup_push(sscp_copy_thread_cleanup, t);
|
pthread_cleanup_push(sscp_copy_thread_cleanup, t);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
lock_acquire(&sscp->chunk_lock);
|
lock_acquire(&sscp->chunk_lock);
|
||||||
c = chunk_acquire(&sscp->chunk_list);
|
c = chunk_acquire(&sscp->chunk_list);
|
||||||
lock_release(&sscp->chunk_lock);
|
lock_release(&sscp->chunk_lock);
|
||||||
|
|
||||||
if (!c)
|
if (!c)
|
||||||
break; /* no more chunks */
|
break; /* no more chunks */
|
||||||
|
|
||||||
if (chunk_prepare(c, sftp) < 0)
|
if (chunk_prepare(c, sftp) < 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (chunk_copy(c, sftp,
|
if (chunk_copy(c, sftp,
|
||||||
sscp->sftp_buf_sz, sscp->io_buf_sz, &t->done) < 0)
|
sscp->sftp_buf_sz, sscp->io_buf_sz, &t->done) < 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_cleanup_pop(1);
|
pthread_cleanup_pop(1);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static double calculate_bps(size_t diff, struct timeval *b, struct timeval *a)
|
static double calculate_bps(size_t diff, struct timeval *b, struct timeval *a)
|
||||||
{
|
{
|
||||||
double sec, usec;
|
double sec, usec;
|
||||||
|
|
||||||
if (a->tv_usec < b->tv_usec) {
|
if (a->tv_usec < b->tv_usec) {
|
||||||
a->tv_usec += 1000000;
|
a->tv_usec += 1000000;
|
||||||
a->tv_sec--;
|
a->tv_sec--;
|
||||||
}
|
}
|
||||||
|
|
||||||
sec = a->tv_sec - b->tv_sec;
|
sec = a->tv_sec - b->tv_sec;
|
||||||
usec = a->tv_usec - b->tv_usec;
|
usec = a->tv_usec - b->tv_usec;
|
||||||
sec += usec / 1000000;
|
sec += usec / 1000000;
|
||||||
|
|
||||||
return (double)diff / sec;
|
return (double)diff / sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_progress_bar(double percent, char *suffix)
|
static void print_progress_bar(double percent, char *suffix)
|
||||||
{
|
{
|
||||||
int n, thresh, bar_width;
|
int n, thresh, bar_width;
|
||||||
struct winsize ws;
|
struct winsize ws;
|
||||||
char buf[128];
|
char buf[128];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* [=======> ] XX.X% SUFFIX
|
* [=======> ] XX.X% SUFFIX
|
||||||
*/
|
*/
|
||||||
|
|
||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
|
|
||||||
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
|
||||||
return; /* XXX */
|
return; /* XXX */
|
||||||
bar_width = min(sizeof(buf), ws.ws_col) - strlen(suffix) - 8;
|
bar_width = min(sizeof(buf), ws.ws_col) - strlen(suffix) - 8;
|
||||||
|
|
||||||
if (bar_width > 8) {
|
if (bar_width > 8) {
|
||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
thresh = floor(bar_width * (percent / 100)) - 1;
|
thresh = floor(bar_width * (percent / 100)) - 1;
|
||||||
|
|
||||||
for (n = 1; n < bar_width - 1; n++) {
|
for (n = 1; n < bar_width - 1; n++) {
|
||||||
if (n <= thresh)
|
if (n <= thresh)
|
||||||
buf[n] = '=';
|
buf[n] = '=';
|
||||||
else
|
else
|
||||||
buf[n] = ' ';
|
buf[n] = ' ';
|
||||||
}
|
}
|
||||||
buf[thresh] = '>';
|
buf[thresh] = '>';
|
||||||
buf[0] = '[';
|
buf[0] = '[';
|
||||||
buf[bar_width - 1] = ']';
|
buf[bar_width - 1] = ']';
|
||||||
snprintf(buf + bar_width, sizeof(buf) - bar_width,
|
snprintf(buf + bar_width, sizeof(buf) - bar_width,
|
||||||
" %3d%% ", (int)floor(percent));
|
" %3d%% ", (int)floor(percent));
|
||||||
}
|
}
|
||||||
|
|
||||||
pprint1("%s%s", buf, suffix);
|
pprint1("%s%s", buf, suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_progress(struct timeval *start, struct timeval *end,
|
static void print_progress(struct timeval *start, struct timeval *end,
|
||||||
size_t total, size_t last, size_t done)
|
size_t total, size_t last, size_t done)
|
||||||
{
|
{
|
||||||
char *bps_units[] = { "B/s", "KB/s", "MB/s", "GB/s" };
|
char *bps_units[] = { "B/s", "KB/s", "MB/s", "GB/s" };
|
||||||
char *byte_units[] = { "B", "KB", "MB", "GB", "TB", "PB" };
|
char *byte_units[] = { "B", "KB", "MB", "GB", "TB", "PB" };
|
||||||
char suffix[128];
|
char suffix[128];
|
||||||
int bps_u, byte_tu, byte_du;
|
int bps_u, byte_tu, byte_du;
|
||||||
size_t total_round;
|
size_t total_round;
|
||||||
int percent;
|
int percent;
|
||||||
double bps;
|
double bps;
|
||||||
|
|
||||||
#define array_size(a) (sizeof(a) / sizeof(a[0]))
|
#define array_size(a) (sizeof(a) / sizeof(a[0]))
|
||||||
|
|
||||||
total_round = total;
|
total_round = total;
|
||||||
for (byte_tu = 0; total_round > 1000 && byte_tu < array_size(byte_units) - 1;
|
for (byte_tu = 0; total_round > 1000 && byte_tu < array_size(byte_units) - 1;
|
||||||
byte_tu++)
|
byte_tu++)
|
||||||
total_round /= 1024;
|
total_round /= 1024;
|
||||||
|
|
||||||
bps = calculate_bps(done - last, start, end);
|
bps = calculate_bps(done - last, start, end);
|
||||||
for (bps_u = 0; bps > 1000 && bps_u < array_size(bps_units); bps_u++)
|
for (bps_u = 0; bps > 1000 && bps_u < array_size(bps_units); bps_u++)
|
||||||
bps /= 1000;
|
bps /= 1000;
|
||||||
|
|
||||||
percent = floor(((double)(done) / (double)total) * 100);
|
percent = floor(((double)(done) / (double)total) * 100);
|
||||||
for (byte_du = 0; done > 1000 && byte_du < array_size(byte_units) - 1; byte_du++)
|
for (byte_du = 0; done > 1000 && byte_du < array_size(byte_units) - 1; byte_du++)
|
||||||
done /= 1024;
|
done /= 1024;
|
||||||
|
|
||||||
snprintf(suffix, sizeof(suffix), "%lu%s/%lu%s %.2f%s ",
|
snprintf(suffix, sizeof(suffix), "%lu%s/%lu%s %.2f%s ",
|
||||||
done, byte_units[byte_du], total_round, byte_units[byte_tu],
|
done, byte_units[byte_du], total_round, byte_units[byte_tu],
|
||||||
bps, bps_units[bps_u]);
|
bps, bps_units[bps_u]);
|
||||||
|
|
||||||
print_progress_bar(percent, suffix);
|
print_progress_bar(percent, suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sscp_monitor_thread_cleanup(void *arg)
|
void sscp_monitor_thread_cleanup(void *arg)
|
||||||
{
|
{
|
||||||
struct sscp *sscp = arg;
|
struct sscp *sscp = arg;
|
||||||
struct timeval end;
|
struct timeval end;
|
||||||
struct file *f;
|
struct file *f;
|
||||||
size_t total, done;
|
size_t total, done;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
total = done = 0;
|
total = done = 0;
|
||||||
|
|
||||||
gettimeofday(&end, NULL);
|
gettimeofday(&end, NULL);
|
||||||
|
|
||||||
/* get total byte to be transferred */
|
/* get total byte to be transferred */
|
||||||
list_for_each_entry(f, &sscp->file_list, list) {
|
list_for_each_entry(f, &sscp->file_list, list) {
|
||||||
total += f->size;
|
total += f->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get total byte transferred */
|
/* get total byte transferred */
|
||||||
for (n = 0; n < nr_threads; n++) {
|
for (n = 0; n < nr_threads; n++) {
|
||||||
done += threads[n].done;
|
done += threads[n].done;
|
||||||
}
|
}
|
||||||
|
|
||||||
print_progress(&sscp->start, &end, total, 0, done);
|
print_progress(&sscp->start, &end, total, 0, done);
|
||||||
fputs("\n", stdout); /* the final ouput. we need \n */
|
fputs("\n", stdout); /* the final ouput. we need \n */
|
||||||
}
|
}
|
||||||
|
|
||||||
void *sscp_monitor_thread(void *arg)
|
void *sscp_monitor_thread(void *arg)
|
||||||
{
|
{
|
||||||
struct sscp *sscp = arg;
|
struct sscp *sscp = arg;
|
||||||
struct timeval a, b;
|
struct timeval a, b;
|
||||||
struct file *f;
|
struct file *f;
|
||||||
bool all_done;
|
bool all_done;
|
||||||
size_t total, done, last;
|
size_t total, done, last;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
||||||
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
||||||
pthread_cleanup_push(sscp_monitor_thread_cleanup, sscp);
|
pthread_cleanup_push(sscp_monitor_thread_cleanup, sscp);
|
||||||
|
|
||||||
/* get total byte to be transferred */
|
/* get total byte to be transferred */
|
||||||
total = 0;
|
total = 0;
|
||||||
list_for_each_entry(f, &sscp->file_list, list) {
|
list_for_each_entry(f, &sscp->file_list, list) {
|
||||||
total += f->size;
|
total += f->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
all_done = true;
|
all_done = true;
|
||||||
last = done = 0;
|
last = done = 0;
|
||||||
|
|
||||||
for (n = 0; n < nr_threads; n++) {
|
for (n = 0; n < nr_threads; n++) {
|
||||||
last += threads[n].done;
|
last += threads[n].done;
|
||||||
}
|
}
|
||||||
gettimeofday(&b, NULL);
|
gettimeofday(&b, NULL);
|
||||||
|
|
||||||
usleep(500000);
|
usleep(500000);
|
||||||
|
|
||||||
for (n = 0; n < nr_threads; n++) {
|
for (n = 0; n < nr_threads; n++) {
|
||||||
done += threads[n].done;;
|
done += threads[n].done;;
|
||||||
if (!threads[n].finished)
|
if (!threads[n].finished)
|
||||||
all_done = false;
|
all_done = false;
|
||||||
}
|
}
|
||||||
gettimeofday(&a, NULL);
|
gettimeofday(&a, NULL);
|
||||||
|
|
||||||
print_progress(&b, &a, total, last, done);
|
print_progress(&b, &a, total, last, done);
|
||||||
|
|
||||||
if (all_done || total == done)
|
if (all_done || total == done)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_cleanup_pop(1);
|
pthread_cleanup_pop(1);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,25 +14,25 @@
|
|||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
int nr_cpus()
|
int nr_cpus()
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
size_t size = sizeof(n);
|
size_t size = sizeof(n);
|
||||||
|
|
||||||
if (sysctlbyname("machdep.cpu.core_count", &n, &size, NULL, 0) != 0) {
|
if (sysctlbyname("machdep.cpu.core_count", &n, &size, NULL, 0) != 0) {
|
||||||
pr_err("failed to get number of cpu cores: %s\n", strerrno());
|
pr_err("failed to get number of cpu cores: %s\n", strerrno());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef linux
|
#ifdef linux
|
||||||
int nr_cpus()
|
int nr_cpus()
|
||||||
{
|
{
|
||||||
cpu_set_t cpu_set;
|
cpu_set_t cpu_set;
|
||||||
if (sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set) == 0)
|
if (sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set) == 0)
|
||||||
return CPU_COUNT(&cpu_set);
|
return CPU_COUNT(&cpu_set);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
20
src/pprint.c
20
src/pprint.c
@@ -13,20 +13,20 @@ static pthread_mutex_t pprint_lock = PTHREAD_MUTEX_INITIALIZER;
|
|||||||
|
|
||||||
void pprint_set_level(int level)
|
void pprint_set_level(int level)
|
||||||
{
|
{
|
||||||
pprint_level = level;
|
pprint_level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pprint(int level, const char *fmt, ...)
|
void pprint(int level, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
|
|
||||||
if (level <= pprint_level) {
|
if (level <= pprint_level) {
|
||||||
pthread_mutex_lock(&pprint_lock);
|
pthread_mutex_lock(&pprint_lock);
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
vfprintf(stdout, fmt, va);
|
vfprintf(stdout, fmt, va);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
pthread_mutex_unlock(&pprint_lock);
|
pthread_mutex_unlock(&pprint_lock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
406
src/ssh.c
406
src/ssh.c
@@ -10,265 +10,265 @@ static int ssh_verify_known_hosts(ssh_session session);
|
|||||||
|
|
||||||
static int ssh_set_opts(ssh_session ssh, struct ssh_opts *opts)
|
static int ssh_set_opts(ssh_session ssh, struct ssh_opts *opts)
|
||||||
{
|
{
|
||||||
ssh_set_log_level(opts->debuglevel);
|
ssh_set_log_level(opts->debuglevel);
|
||||||
|
|
||||||
if (opts->login_name &&
|
if (opts->login_name &&
|
||||||
ssh_options_set(ssh, SSH_OPTIONS_USER, opts->login_name) < 0) {
|
ssh_options_set(ssh, SSH_OPTIONS_USER, opts->login_name) < 0) {
|
||||||
pr_err("failed to set login name\n");
|
pr_err("failed to set login name\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts->port &&
|
if (opts->port &&
|
||||||
ssh_options_set(ssh, SSH_OPTIONS_PORT_STR, opts->port) < 0) {
|
ssh_options_set(ssh, SSH_OPTIONS_PORT_STR, opts->port) < 0) {
|
||||||
pr_err("failed to set port number\n");
|
pr_err("failed to set port number\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts->identity &&
|
if (opts->identity &&
|
||||||
ssh_options_set(ssh, SSH_OPTIONS_IDENTITY, opts->identity) < 0) {
|
ssh_options_set(ssh, SSH_OPTIONS_IDENTITY, opts->identity) < 0) {
|
||||||
pr_err("failed to set identity\n");
|
pr_err("failed to set identity\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts->cipher) {
|
if (opts->cipher) {
|
||||||
if (ssh_options_set(ssh, SSH_OPTIONS_CIPHERS_C_S, opts->cipher) < 0) {
|
if (ssh_options_set(ssh, SSH_OPTIONS_CIPHERS_C_S, opts->cipher) < 0) {
|
||||||
pr_err("failed to set cipher client to server\n");
|
pr_err("failed to set cipher client to server\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (ssh_options_set(ssh, SSH_OPTIONS_CIPHERS_S_C, opts->cipher) < 0) {
|
if (ssh_options_set(ssh, SSH_OPTIONS_CIPHERS_S_C, opts->cipher) < 0) {
|
||||||
pr_err("failed to set cipher client to server\n");
|
pr_err("failed to set cipher client to server\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts->compress &&
|
if (opts->compress &&
|
||||||
ssh_options_set(ssh, SSH_OPTIONS_COMPRESSION, "yes") < 0) {
|
ssh_options_set(ssh, SSH_OPTIONS_COMPRESSION, "yes") < 0) {
|
||||||
pr_err("failed to enable ssh compression\n");
|
pr_err("failed to enable ssh compression\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ssh_authenticate(ssh_session ssh, struct ssh_opts *opts)
|
static int ssh_authenticate(ssh_session ssh, struct ssh_opts *opts)
|
||||||
{
|
{
|
||||||
int auth_bit_mask;
|
int auth_bit_mask;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* none method */
|
|
||||||
ret = ssh_userauth_none(ssh, NULL);
|
|
||||||
if (ret == SSH_AUTH_SUCCESS)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
auth_bit_mask = ssh_userauth_list(ssh, NULL);
|
/* none method */
|
||||||
|
ret = ssh_userauth_none(ssh, NULL);
|
||||||
|
if (ret == SSH_AUTH_SUCCESS)
|
||||||
|
return 0;
|
||||||
|
|
||||||
if (auth_bit_mask & SSH_AUTH_METHOD_NONE &&
|
auth_bit_mask = ssh_userauth_list(ssh, NULL);
|
||||||
ssh_userauth_none(ssh, NULL) == SSH_AUTH_SUCCESS) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auth_bit_mask & SSH_AUTH_METHOD_PUBLICKEY &&
|
if (auth_bit_mask & SSH_AUTH_METHOD_NONE &&
|
||||||
ssh_userauth_publickey_auto(ssh, NULL, NULL) == SSH_AUTH_SUCCESS) {
|
ssh_userauth_none(ssh, NULL) == SSH_AUTH_SUCCESS) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auth_bit_mask & SSH_AUTH_METHOD_PASSWORD) {
|
if (auth_bit_mask & SSH_AUTH_METHOD_PUBLICKEY &&
|
||||||
if (!opts->password) {
|
ssh_userauth_publickey_auto(ssh, NULL, NULL) == SSH_AUTH_SUCCESS) {
|
||||||
opts->password = getpass("Password: ");
|
return 0;
|
||||||
}
|
}
|
||||||
if (ssh_userauth_password(ssh, NULL, opts->password) == SSH_AUTH_SUCCESS)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pr_err("authentication failure: %s\n", ssh_get_error(ssh));
|
if (auth_bit_mask & SSH_AUTH_METHOD_PASSWORD) {
|
||||||
return -1;
|
if (!opts->password) {
|
||||||
|
opts->password = getpass("Password: ");
|
||||||
|
}
|
||||||
|
if (ssh_userauth_password(ssh, NULL, opts->password) == SSH_AUTH_SUCCESS)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pr_err("authentication failure: %s\n", ssh_get_error(ssh));
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssh_session ssh_make_ssh_session(char *sshdst, struct ssh_opts *opts)
|
static ssh_session ssh_make_ssh_session(char *sshdst, struct ssh_opts *opts)
|
||||||
{
|
{
|
||||||
ssh_session ssh = ssh_new();
|
ssh_session ssh = ssh_new();
|
||||||
|
|
||||||
if (ssh_set_opts(ssh, opts) != 0)
|
if (ssh_set_opts(ssh, opts) != 0)
|
||||||
goto free_out;
|
goto free_out;
|
||||||
|
|
||||||
if (ssh_options_set(ssh, SSH_OPTIONS_HOST, sshdst) != SSH_OK) {
|
if (ssh_options_set(ssh, SSH_OPTIONS_HOST, sshdst) != SSH_OK) {
|
||||||
pr_err("failed to set destination host\n");
|
pr_err("failed to set destination host\n");
|
||||||
goto free_out;
|
goto free_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ssh_connect(ssh) != SSH_OK) {
|
if (ssh_connect(ssh) != SSH_OK) {
|
||||||
pr_err("failed to connect ssh server: %s\n", ssh_get_error(ssh));
|
pr_err("failed to connect ssh server: %s\n", ssh_get_error(ssh));
|
||||||
goto free_out;
|
goto free_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ssh_authenticate(ssh, opts) != 0) {
|
if (ssh_authenticate(ssh, opts) != 0) {
|
||||||
pr_err("authentication failed: %s\n", ssh_get_error(ssh));
|
pr_err("authentication failed: %s\n", ssh_get_error(ssh));
|
||||||
goto disconnect_out;
|
goto disconnect_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ssh_verify_known_hosts(ssh) != 0) {
|
if (ssh_verify_known_hosts(ssh) != 0) {
|
||||||
goto disconnect_out;
|
goto disconnect_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ssh;
|
return ssh;
|
||||||
|
|
||||||
disconnect_out:
|
disconnect_out:
|
||||||
ssh_disconnect(ssh);
|
ssh_disconnect(ssh);
|
||||||
free_out:
|
free_out:
|
||||||
ssh_free(ssh);
|
ssh_free(ssh);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
sftp_session ssh_make_sftp_session(char *sshdst, struct ssh_opts *opts)
|
sftp_session ssh_make_sftp_session(char *sshdst, struct ssh_opts *opts)
|
||||||
{
|
{
|
||||||
sftp_session sftp;
|
sftp_session sftp;
|
||||||
ssh_session ssh = ssh_make_ssh_session(sshdst, opts);
|
ssh_session ssh = ssh_make_ssh_session(sshdst, opts);
|
||||||
|
|
||||||
if (!ssh) {
|
if (!ssh) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
sftp = sftp_new(ssh);
|
sftp = sftp_new(ssh);
|
||||||
if (!sftp) {
|
if (!sftp) {
|
||||||
pr_err("failed to allocate sftp session: %s\n", ssh_get_error(ssh));
|
pr_err("failed to allocate sftp session: %s\n", ssh_get_error(ssh));
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sftp_init(sftp) != SSH_OK) {
|
if (sftp_init(sftp) != SSH_OK) {
|
||||||
pr_err("failed to initialize sftp session: err code %d\n",
|
pr_err("failed to initialize sftp session: err code %d\n",
|
||||||
sftp_get_error(sftp));
|
sftp_get_error(sftp));
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sftp;
|
return sftp;
|
||||||
err_out:
|
err_out:
|
||||||
ssh_disconnect(ssh);
|
ssh_disconnect(ssh);
|
||||||
ssh_free(ssh);
|
ssh_free(ssh);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* copied from https://api.libssh.org/stable/libssh_tutor_guided_tour.html*/
|
/* copied from https://api.libssh.org/stable/libssh_tutor_guided_tour.html*/
|
||||||
static int ssh_verify_known_hosts(ssh_session session)
|
static int ssh_verify_known_hosts(ssh_session session)
|
||||||
{
|
{
|
||||||
enum ssh_known_hosts_e state;
|
enum ssh_known_hosts_e state;
|
||||||
unsigned char *hash = NULL;
|
unsigned char *hash = NULL;
|
||||||
ssh_key srv_pubkey = NULL;
|
ssh_key srv_pubkey = NULL;
|
||||||
size_t hlen;
|
size_t hlen;
|
||||||
char buf[10];
|
char buf[10];
|
||||||
char *hexa;
|
char *hexa;
|
||||||
char *p;
|
char *p;
|
||||||
int cmp;
|
int cmp;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
rc = ssh_get_server_publickey(session, &srv_pubkey);
|
rc = ssh_get_server_publickey(session, &srv_pubkey);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = ssh_get_publickey_hash(srv_pubkey,
|
rc = ssh_get_publickey_hash(srv_pubkey,
|
||||||
SSH_PUBLICKEY_HASH_SHA1,
|
SSH_PUBLICKEY_HASH_SHA1,
|
||||||
&hash,
|
&hash,
|
||||||
&hlen);
|
&hlen);
|
||||||
ssh_key_free(srv_pubkey);
|
ssh_key_free(srv_pubkey);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
state = ssh_session_is_known_server(session);
|
state = ssh_session_is_known_server(session);
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case SSH_KNOWN_HOSTS_OK:
|
case SSH_KNOWN_HOSTS_OK:
|
||||||
/* OK */
|
/* OK */
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case SSH_KNOWN_HOSTS_CHANGED:
|
case SSH_KNOWN_HOSTS_CHANGED:
|
||||||
fprintf(stderr, "Host key for server changed: it is now:\n");
|
fprintf(stderr, "Host key for server changed: it is now:\n");
|
||||||
//ssh_print_hexa("Public key hash", hash, hlen);
|
//ssh_print_hexa("Public key hash", hash, hlen);
|
||||||
fprintf(stderr, "For security reasons, connection will be stopped\n");
|
fprintf(stderr, "For security reasons, connection will be stopped\n");
|
||||||
ssh_clean_pubkey_hash(&hash);
|
ssh_clean_pubkey_hash(&hash);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
case SSH_KNOWN_HOSTS_OTHER:
|
case SSH_KNOWN_HOSTS_OTHER:
|
||||||
fprintf(stderr, "The host key for this server was not found but an other"
|
fprintf(stderr, "The host key for this server was not found but an other"
|
||||||
"type of key exists.\n");
|
"type of key exists.\n");
|
||||||
fprintf(stderr, "An attacker might change the default server key to"
|
fprintf(stderr, "An attacker might change the default server key to"
|
||||||
"confuse your client into thinking the key does not exist\n");
|
"confuse your client into thinking the key does not exist\n");
|
||||||
ssh_clean_pubkey_hash(&hash);
|
ssh_clean_pubkey_hash(&hash);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
case SSH_KNOWN_HOSTS_NOT_FOUND:
|
case SSH_KNOWN_HOSTS_NOT_FOUND:
|
||||||
fprintf(stderr, "Could not find known host file.\n");
|
fprintf(stderr, "Could not find known host file.\n");
|
||||||
fprintf(stderr, "If you accept the host key here, the file will be"
|
fprintf(stderr, "If you accept the host key here, the file will be"
|
||||||
"automatically created.\n");
|
"automatically created.\n");
|
||||||
|
|
||||||
/* FALL THROUGH to SSH_SERVER_NOT_KNOWN behavior */
|
/* FALL THROUGH to SSH_SERVER_NOT_KNOWN behavior */
|
||||||
|
|
||||||
case SSH_KNOWN_HOSTS_UNKNOWN:
|
case SSH_KNOWN_HOSTS_UNKNOWN:
|
||||||
hexa = ssh_get_hexa(hash, hlen);
|
hexa = ssh_get_hexa(hash, hlen);
|
||||||
fprintf(stderr,"The server is unknown. Do you trust the host key?\n");
|
fprintf(stderr,"The server is unknown. Do you trust the host key?\n");
|
||||||
fprintf(stderr, "Public key hash: %s\n", hexa);
|
fprintf(stderr, "Public key hash: %s\n", hexa);
|
||||||
ssh_string_free_char(hexa);
|
ssh_string_free_char(hexa);
|
||||||
ssh_clean_pubkey_hash(&hash);
|
ssh_clean_pubkey_hash(&hash);
|
||||||
p = fgets(buf, sizeof(buf), stdin);
|
p = fgets(buf, sizeof(buf), stdin);
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
cmp = strncasecmp(buf, "yes", 3);
|
cmp = strncasecmp(buf, "yes", 3);
|
||||||
if (cmp != 0) {
|
if (cmp != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = ssh_session_update_known_hosts(session);
|
rc = ssh_session_update_known_hosts(session);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
fprintf(stderr, "Error %s\n", strerror(errno));
|
fprintf(stderr, "Error %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case SSH_KNOWN_HOSTS_ERROR:
|
case SSH_KNOWN_HOSTS_ERROR:
|
||||||
fprintf(stderr, "Error %s", ssh_get_error(session));
|
fprintf(stderr, "Error %s", ssh_get_error(session));
|
||||||
ssh_clean_pubkey_hash(&hash);
|
ssh_clean_pubkey_hash(&hash);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_clean_pubkey_hash(&hash);
|
ssh_clean_pubkey_hash(&hash);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ssh_sftp_close(sftp_session sftp)
|
void ssh_sftp_close(sftp_session sftp)
|
||||||
{
|
{
|
||||||
ssh_session ssh = sftp_ssh(sftp);
|
ssh_session ssh = sftp_ssh(sftp);
|
||||||
sftp_free(sftp);
|
sftp_free(sftp);
|
||||||
ssh_disconnect(ssh);
|
ssh_disconnect(ssh);
|
||||||
ssh_free(ssh);
|
ssh_free(ssh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int sftp_write2(sftp_file sf, const void *buf, size_t len, size_t sftp_buf_sz)
|
int sftp_write2(sftp_file sf, const void *buf, size_t len, size_t sftp_buf_sz)
|
||||||
{
|
{
|
||||||
int ret, nbytes;
|
int ret, nbytes;
|
||||||
|
|
||||||
for (nbytes = 0; nbytes < len;) {
|
for (nbytes = 0; nbytes < len;) {
|
||||||
ret = sftp_write(sf, buf + nbytes,
|
ret = sftp_write(sf, buf + nbytes,
|
||||||
min(len - nbytes, sftp_buf_sz));
|
min(len - nbytes, sftp_buf_sz));
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
nbytes += ret;
|
nbytes += ret;
|
||||||
}
|
}
|
||||||
return nbytes;
|
return nbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sftp_read2(sftp_file sf, void *buf, size_t len, size_t sftp_buf_sz)
|
int sftp_read2(sftp_file sf, void *buf, size_t len, size_t sftp_buf_sz)
|
||||||
{
|
{
|
||||||
int ret, nbytes;
|
int ret, nbytes;
|
||||||
|
|
||||||
for (nbytes = 0; nbytes < len;) {
|
for (nbytes = 0; nbytes < len;) {
|
||||||
ret = sftp_read(sf, buf + nbytes,
|
ret = sftp_read(sf, buf + nbytes,
|
||||||
min(len - nbytes, sftp_buf_sz));
|
min(len - nbytes, sftp_buf_sz));
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
nbytes += ret;
|
nbytes += ret;
|
||||||
}
|
}
|
||||||
return nbytes;
|
return nbytes;
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/ssh.h
14
src/ssh.h
@@ -6,14 +6,14 @@
|
|||||||
|
|
||||||
|
|
||||||
struct ssh_opts {
|
struct ssh_opts {
|
||||||
char *login_name; /* -l */
|
char *login_name; /* -l */
|
||||||
char *port; /* -p */
|
char *port; /* -p */
|
||||||
char *identity; /* -i */
|
char *identity; /* -i */
|
||||||
char *cipher; /* -c */
|
char *cipher; /* -c */
|
||||||
int compress; /* -C */
|
int compress; /* -C */
|
||||||
int debuglevel; /* -v */
|
int debuglevel; /* -v */
|
||||||
|
|
||||||
char *password; /* filled at the first connecting phase */
|
char *password; /* filled at the first connecting phase */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ssh_make_sftp_session() creates sftp_session. sshdst accpets
|
/* ssh_make_sftp_session() creates sftp_session. sshdst accpets
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
#define pr(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
|
#define pr(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
|
||||||
|
|
||||||
#define pr_info(fmt, ...) fprintf(stderr, "INFO:%s(): " fmt, \
|
#define pr_info(fmt, ...) fprintf(stderr, "INFO:%s(): " fmt, \
|
||||||
__func__, ##__VA_ARGS__)
|
__func__, ##__VA_ARGS__)
|
||||||
|
|
||||||
#define pr_warn(fmt, ...) fprintf(stderr, "\x1b[1m\x1b[33m" \
|
#define pr_warn(fmt, ...) fprintf(stderr, "\x1b[1m\x1b[33m" \
|
||||||
"WARN:%s():\x1b[0m " fmt, \
|
"WARN:%s():\x1b[0m " fmt, \
|
||||||
@@ -24,8 +24,8 @@
|
|||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define pr_debug(fmt, ...) fprintf(stderr, "\x1b[1m\x1b[33m" \
|
#define pr_debug(fmt, ...) fprintf(stderr, "\x1b[1m\x1b[33m" \
|
||||||
"DEBUG:%s():\x1b[0m " fmt, \
|
"DEBUG:%s():\x1b[0m " fmt, \
|
||||||
__func__, ##__VA_ARGS__);
|
__func__, ##__VA_ARGS__);
|
||||||
#else
|
#else
|
||||||
#define pr_debug(fmt, ...)
|
#define pr_debug(fmt, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user