Skip to content

Commit

Permalink
do not passing msg_fp via mscp opts
Browse files Browse the repository at this point in the history
instead, mpr_* functions print messages to stdout or stderr directly.
  • Loading branch information
upa committed Feb 4, 2024
1 parent 304e71d commit c95e6a4
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 114 deletions.
20 changes: 13 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ add_subdirectory(libssh EXCLUDE_FROM_ALL)


# setup mscp compile options
set(MSCP_COMPILE_OPTS -iquote ${CMAKE_CURRENT_BINARY_DIR}/libssh/include)
set(MSCP_BUILD_INCLUDE_DIRS
list(APPEND MSCP_COMPILE_OPTS -iquote ${CMAKE_CURRENT_BINARY_DIR}/libssh/include)
list(APPEND MSCP_BUILD_INCLUDE_DIRS
${mscp_SOURCE_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}/libssh/include)

set(MSCP_LINK_LIBS ssh-static)
list(APPEND MSCP_LINK_LIBS ssh-static)
if(BUILD_CONAN)
find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED)
Expand All @@ -83,14 +83,20 @@ if(BUILD_CONAN)
endif()


# generate version header file
# Symbol check
check_symbol_exists(strlcat string.h HAVE_STRLCAT)

# generate config.h in build dir
configure_file(
${mscp_SOURCE_DIR}/include/mscp_version.h.in
${mscp_SOURCE_DIR}/include/mscp_version.h)
${mscp_SOURCE_DIR}/include/config.h.in
${CMAKE_CURRENT_BINARY_DIR}/include/config.h)
list(APPEND MSCP_BUILD_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/include)


# libmscp.a
set(LIBMSCP_SRC
src/mscp.c src/ssh.c src/fileops.c src/path.c src/platform.c src/message.c)
src/mscp.c src/ssh.c src/fileops.c src/path.c src/platform.c src/message.c
src/openbsd-compat/strlcat.c)
add_library(mscp-static STATIC ${LIBMSCP_SRC})
target_include_directories(mscp-static
PRIVATE ${MSCP_BUILD_INCLUDE_DIRS} ${mscp_SOURCE_DIR}/include)
Expand Down
12 changes: 12 additions & 0 deletions include/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* SPDX-License-Identifier: GPL-3.0-only */
#ifndef _CONFIG_H_
#define _CONFIG_H_

#define MSCP_VERSION "@MSCP_VERSION@"
#define MSCP_BUILD_VERSION "@MSCP_BUILD_VERSION@"


/* Define to 1 if you have the strlcat function. */
#cmakedefine HAVE_STRLCAT 1

#endif /* _CONFIG_H_ */
2 changes: 0 additions & 2 deletions include/mscp.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ struct mscp_opts {
int interval; /** interval between SSH connection attempts */

int severity; /** messaging severity. set MSCP_SERVERITY_* */
int msg_fd; /** fd to output message. default STDOUT (0),
* and -1 disables output */
};

#define MSCP_SSH_MAX_LOGIN_NAME 64
Expand Down
8 changes: 0 additions & 8 deletions include/mscp_version.h.in

This file was deleted.

4 changes: 2 additions & 2 deletions src/fileops.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void sftp_err_to_errno(sftp_session sftp)
errno = ENODEV;
break;
default:
mpr_warn(stderr, "unkown SSH_FX response %d", sftperr);
mpr_warn("unkown SSH_FX response %d", sftperr);
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ static void sftp_attr_to_stat(sftp_attributes attr, struct stat *st)
st->st_mode |= S_IFIFO; /* really? */
break;
default:
mpr_warn(stderr, "unkown SSH_FILEXFER_TYPE %d", attr->type);
mpr_warn("unkown SSH_FILEXFER_TYPE %d", attr->type);
}

/* ToDo: convert atime, ctime, and mtime */
Expand Down
31 changes: 4 additions & 27 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <pthread.h>

#include <mscp.h>
#include <mscp_version.h>
#include <util.h>

#include "config.h"

void usage(bool print_help) {
printf("mscp " MSCP_BUILD_VERSION ": copy files over multiple ssh connections\n"
Expand Down Expand Up @@ -221,12 +221,13 @@ struct target *validate_targets(char **arg, int len)
}

struct mscp *m = NULL;
int msg_fd = 0;
pthread_t tid_stat = 0;

void sigint_handler(int sig)
{
mscp_stop(m);
if (tid_stat > 0)
pthread_cancel(tid_stat);
}

void *print_stat_thread(void *arg);
Expand Down Expand Up @@ -396,15 +397,6 @@ int main(int argc, char **argv)
MSCP_SSH_MAX_LOGIN_NAME - 1);
}

if (!dryrun) {
if (pipe(pipe_fd) < 0) {
fprintf(stderr, "pipe: %s\n", strerror(errno));
return -1;
}
msg_fd = pipe_fd[0];
o.msg_fd = pipe_fd[1];
}

if ((m = mscp_init(remote, direction, &o, &s)) == NULL) {
fprintf(stderr, "mscp_init: %s\n", mscp_get_error());
return -1;
Expand Down Expand Up @@ -619,25 +611,10 @@ struct xfer_stat x;

void print_stat(bool final)
{
struct pollfd pfd = { .fd = msg_fd, .events = POLLIN };
struct mscp_stats s;
char buf[8192];
int timeout;

if (poll(&pfd, 1, !final ? 100 : 0) < 0) {
fprintf(stderr, "poll: %s\n", strerror(errno));
return;
}

if (pfd.revents & POLLIN) {
memset(buf, 0, sizeof(buf));
if (read(msg_fd, buf, sizeof(buf)) < 0) {
fprintf(stderr, "read: %s\n", strerror(errno));
return;
}
print_cli("\r\033[K" "%s", buf);
}

gettimeofday(&x.after, NULL);
if (calculate_timedelta(&x.before, &x.after) > 1 || final) {
mscp_get_stats(m, &s);
Expand All @@ -658,7 +635,6 @@ void print_stat_thread_cleanup(void *arg)

void *print_stat_thread(void *arg)
{
struct pollfd pfd = { .fd = msg_fd, .events = POLLIN };
struct mscp_stats s;
char buf[8192];

Expand All @@ -672,6 +648,7 @@ void *print_stat_thread(void *arg)

while (true) {
print_stat(false);
sleep(1);
}

pthread_cleanup_pop(1);
Expand Down
32 changes: 16 additions & 16 deletions src/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@
void mprint_set_severity(int severity);
int mprint_get_severity();

#define mprint(fp, severity, fmt, ...) \
do { \
if (fp && severity <= mprint_get_severity()) { \
fprintf(fp, fmt, ##__VA_ARGS__); \
fflush(fp); \
} \
#define mprint(fp, severity, fmt, ...) \
do { \
if (severity <= mprint_get_severity()) { \
fprintf(fp, "\r\033[K" fmt "\n", ##__VA_ARGS__); \
fflush(fp); \
} \
} while (0)

#define mpr_err(fp, fmt, ...) \
mprint(fp, MSCP_SEVERITY_ERR, fmt, ##__VA_ARGS__)
#define mpr_warn(fp, fmt, ...) \
mprint(fp, MSCP_SEVERITY_WARN, fmt, ##__VA_ARGS__)
#define mpr_notice(fp, fmt, ...) \
mprint(fp, MSCP_SEVERITY_NOTICE, fmt, ##__VA_ARGS__)
#define mpr_info(fp, fmt, ...) \
mprint(fp, MSCP_SEVERITY_INFO, fmt, ##__VA_ARGS__)
#define mpr_debug(fp, fmt, ...) \
mprint(fp, MSCP_SEVERITY_DEBUG, fmt, ##__VA_ARGS__)
#define mpr_err(fmt, ...) \
mprint(stderr, MSCP_SEVERITY_ERR, fmt, ##__VA_ARGS__)
#define mpr_warn(fmt, ...) \
mprint(stderr, MSCP_SEVERITY_WARN, fmt, ##__VA_ARGS__)
#define mpr_notice(fmt, ...) \
mprint(stdout, MSCP_SEVERITY_NOTICE, fmt, ##__VA_ARGS__)
#define mpr_info(fmt, ...) \
mprint(stdout, MSCP_SEVERITY_INFO, fmt, ##__VA_ARGS__)
#define mpr_debug(fmt, ...) \
mprint(stdout, MSCP_SEVERITY_DEBUG, fmt, ##__VA_ARGS__)


/* errorno wrapper */
Expand Down
55 changes: 21 additions & 34 deletions src/mscp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
#include <message.h>
#include <mscp.h>

#include <openbsd-compat/openbsd-compat.h>

struct mscp {
char *remote; /* remote host (and uername) */
int direction; /* copy direction */
struct mscp_opts *opts;
struct mscp_ssh_opts *ssh_opts;

FILE *msg_fp; /* writer fd for message pipe */

int *cores; /* usable cpu cores by COREMASK */
int nr_cores; /* length of array of cores */

Expand Down Expand Up @@ -207,9 +206,6 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
o->max_startups = 1;
}

if (o->msg_fd == 0)
o->msg_fd = STDOUT_FILENO;

return 0;
}

Expand Down Expand Up @@ -261,22 +257,17 @@ struct mscp *mscp_init(const char *remote_host, int direction,
goto free_out;
}
m->direction = direction;
if (o->msg_fd > -1) {
m->msg_fp = fdopen(o->msg_fd, "a");
if (!m->msg_fp) {
mscp_set_error("fdopen failed: %s", strerrno());
goto free_out;
}
} else
m->msg_fp = NULL;

if (strlen(o->coremask) > 0) {
if (expand_coremask(o->coremask, &m->cores, &m->nr_cores) < 0)
goto free_out;
mpr_notice(m->msg_fp, "usable cpu cores:");
for (n = 0; n < m->nr_cores; n++)
mpr_notice(m->msg_fp, " %d", m->cores[n]);
mpr_notice(m->msg_fp, "\n");
char b[512], c[8];
for (n = 0; n < m->nr_cores; n++) {
memset(c, 0, sizeof(c));
snprintf(c, sizeof(c) - 1, " %d", m->cores[n]);
strlcat(b, c, sizeof(b));
}
mpr_notice("usable cpu cores:%s", b);
}

m->opts = o;
Expand Down Expand Up @@ -403,7 +394,6 @@ void *mscp_scan_thread(void *arg)

/* initialize path_resolve_args */
memset(&a, 0, sizeof(a));
a.msg_fp = m->msg_fp;
a.total_bytes = &m->total_bytes;

if (list_count(&m->src_list) > 1)
Expand All @@ -420,7 +410,7 @@ void *mscp_scan_thread(void *arg)
a.max_chunk_sz = m->opts->max_chunk_sz;
a.chunk_align = get_page_mask();

mpr_info(m->msg_fp, "start to walk source path(s)\n");
mpr_info("start to walk source path(s)");

/* walk a src_path recusively, and resolve path->dst_path for each src */
list_for_each_entry(s, &m->src_list, list) {
Expand Down Expand Up @@ -453,7 +443,7 @@ void *mscp_scan_thread(void *arg)
mscp_globfree(&pglob);
}

mpr_info(m->msg_fp, "walk source path(s) done\n");
mpr_info("walk source path(s) done");
chunk_pool_set_filled(&m->cp);
m->ret_scan = 0;
return NULL;
Expand Down Expand Up @@ -536,15 +526,15 @@ int mscp_start(struct mscp *m)
int n, ret = 0;

if ((n = chunk_pool_size(&m->cp)) < m->opts->nr_threads) {
mpr_notice(m->msg_fp, "we have only %d chunk(s). "
"set number of connections to %d\n", n, n);
mpr_notice("we have only %d chunk(s). "
"set number of connections to %d", n, n);
m->opts->nr_threads = n;
}

for (n = 0; n < m->opts->nr_threads; n++) {
t = mscp_copy_thread_spawn(m, n);
if (!t) {
mpr_err(m->msg_fp, "failed to spawn copy thread\n");
mpr_err("failed to spawn copy thread");
break;
}
RWLOCK_WRITE_ACQUIRE(&m->thread_rwlock);
Expand Down Expand Up @@ -592,7 +582,7 @@ int mscp_join(struct mscp *m)
}
}

mpr_notice(m->msg_fp, "%lu/%lu bytes copied for %lu/%lu files\n",
mpr_notice("%lu/%lu bytes copied for %lu/%lu files",
done, m->total_bytes, nr_copied, nr_tobe_copied);

return ret;
Expand Down Expand Up @@ -637,31 +627,29 @@ void *mscp_copy_thread(void *arg)
}

if (sem_wait(m->sem) < 0) {
mscp_set_error("sem_wait: %s", strerrno());
mpr_err(m->msg_fp, "%s", mscp_get_error());
mpr_err("sem_wait: %s", strerrno());
goto err_out;
}

if (!(nomore = chunk_pool_is_empty(&m->cp))) {
if (m->opts->interval > 0)
wait_for_interval(m->opts->interval);
mpr_notice(m->msg_fp, "thread:%d connecting to %s\n", t->id, m->remote);
mpr_notice("thread:%d connecting to %s", t->id, m->remote);
t->sftp = ssh_init_sftp_session(m->remote, m->ssh_opts);
}

if (sem_post(m->sem) < 0) {
mscp_set_error("sem_post: %s", strerrno());
mpr_err(m->msg_fp, "%s", mscp_get_error());
mpr_err("sem_post: %s", strerrno());
goto err_out;
}

if (nomore) {
mpr_notice(m->msg_fp, "thread:%d no more connections needed\n", t->id);
mpr_notice("thread:%d no more connections needed", t->id);
goto out;
}

if (!t->sftp) {
mpr_err(m->msg_fp, "thread:%d: %s\n", t->id, mscp_get_error());
mpr_err("thread:%d: %s", t->id, mscp_get_error());
goto err_out;
}

Expand All @@ -688,16 +676,15 @@ void *mscp_copy_thread(void *arg)
if (!c)
break; /* no more chunks */

if ((t->ret = copy_chunk(m->msg_fp,
c, src_sftp, dst_sftp, m->opts->nr_ahead,
if ((t->ret = copy_chunk(c, src_sftp, dst_sftp, m->opts->nr_ahead,
m->opts->buf_sz, &t->done)) < 0)
break;
}

pthread_cleanup_pop(1);

if (t->ret < 0)
mpr_err(m->msg_fp, "thread:%d copy failed: %s 0x%010lx-0x%010lx\n",
mpr_err("thread:%d copy failed: %s 0x%010lx-0x%010lx",
t->id, c->p->path, c->off, c->off + c->len);

return NULL;
Expand Down
10 changes: 10 additions & 0 deletions src/openbsd-compat/openbsd-compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _OPENBSD_COMPAT_H
#define _OPENBSD_COMPAT_H

#include "config.h"

#ifndef HAVE_STRLCAT
size_t strlcat(char *dst, const char *src, size_t siz);
#endif

#endif /* _OPENBSD_COMPAT_H_ */
Loading

0 comments on commit c95e6a4

Please sign in to comment.