From 8315e840358f1ef19295a2cf899144b2faa3ad3d Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 9 Jun 2015 12:17:39 +0200 Subject: fixed postinst script on update/reinstall --- debian/postinst | 5 ++--- scripts/naskconf | 4 ++++ scripts/naskpass.initscript | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/debian/postinst b/debian/postinst index 4306174..4e658c3 100644 --- a/debian/postinst +++ b/debian/postinst @@ -9,10 +9,9 @@ set -e . /usr/share/naskpass/naskconf case "$1" in - install) - ;; - configure|upgrade) + configure) + nask_update db_input high naskpass/activate || true db_go db_get naskpass/activate diff --git a/scripts/naskconf b/scripts/naskconf index 4a4a7e0..f7c9f5d 100644 --- a/scripts/naskconf +++ b/scripts/naskconf @@ -23,9 +23,13 @@ _nask_cmd () { rm /usr/share/initramfs-tools/hooks/naskpass dpkg-divert --package naskpass --rename --remove ${ORGFILE} db_set naskpass/active false + elif [ "x$1" = "xUPDT" ] && [ "$RET" = "true" ]; then + cp /usr/share/naskpass/naskpass.script.initramfs ${ORGFILE} fi return 0 } nask_activate () { _nask_cmd "ACTV"; return $?; } nask_deactivate () { _nask_cmd "DCTV"; return $?; } +nask_update () { _nask_cmd "UPDT"; return $?; } + diff --git a/scripts/naskpass.initscript b/scripts/naskpass.initscript index a2d7e95..21ced37 100644 --- a/scripts/naskpass.initscript +++ b/scripts/naskpass.initscript @@ -310,14 +310,14 @@ setup_mapping() continue fi else - dmesg -D + dmesg -n 1 if ! $cryptkeyscript -c "cryptsetup -T 1 open $cryptsource $crypttarget"; then message "naskpass: failed" continue else message "naskpass: success" fi - dmesg -E + dmesg -n 4 fi fi -- cgit v1.2.3 From c89e18ec972f165650a453aa8bd8b30309e323e6 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 13 Oct 2015 09:01:50 +0200 Subject: added semaphore tests --- .gitignore | 3 +++ Makefile | 5 ++++- tests/Makefile | 15 +++++++++++++++ tests/consumer.c | 38 ++++++++++++++++++++++++++++++++++++++ tests/producer.c | 39 +++++++++++++++++++++++++++++++++++++++ tests/semconfig.h | 8 ++++++++ tests/semtest.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 tests/Makefile create mode 100644 tests/consumer.c create mode 100644 tests/producer.c create mode 100644 tests/semconfig.h create mode 100644 tests/semtest.c diff --git a/.gitignore b/.gitignore index b4c01e9..4b8fd3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ /naskpass +/tests/producer +/tests/consumer +/tests/semtest *.swp diff --git a/Makefile b/Makefile index 43d155d..fe89038 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 DBGFLAGS = -g LDFLAGS ?= $(shell ncurses5-config --libs) -pthread -CC := gcc +CC ?= gcc INSTALL ?= install VERSION ?= $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) BIN = naskpass @@ -11,9 +11,11 @@ all: $(BIN) $(BIN): $(SOURCES) $(CC) $(SOURCES) -D_VERSION=\"$(VERSION)\" $(CFLAGS) $(LDFLAGS) -o $(BIN) + $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all debug: $(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' + $(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' install: $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass @@ -30,6 +32,7 @@ uninstall: clean: rm -f $(BIN) + $(MAKE) -C tests clean source: -dh_make --createorig -p naskpass_$(VERSION) -s -y diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..22a5469 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,15 @@ +CFLAGS = -Wall +LDFLAGS = -lpthread +CC = gcc +SOURCES = $(wildcard *.c) +BINARIES = $(patsubst %.c,%,$(SOURCES)) + +all: $(BINARIES) + +%: %.c + $(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<) + +clean: + rm -f $(BINARIES) + +.PHONY: all install clean diff --git a/tests/consumer.c b/tests/consumer.c new file mode 100644 index 0000000..3f93d1e --- /dev/null +++ b/tests/consumer.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 0; + + if ( (mysem = sem_open(TESTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL && + (mycnt = sem_open(CNTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL ) { + assert( sem_getvalue(mycnt, &semval) == 0 ); + printf("factory producing %d items\n", semval); + + while ( semval-- >= 0 ) { + usleep(250000); + LOG("consumer: -1"); + assert( sem_wait(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + } else { + exit(1); + } + assert( sem_close(mysem) == 0 ); + + exit(0); +} + diff --git a/tests/producer.c b/tests/producer.c new file mode 100644 index 0000000..25af700 --- /dev/null +++ b/tests/producer.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 10; + + if (argc == 2) { + semval = atoi(argv[1]); + } + + if ( (mysem = sem_open(TESTSEM, O_CREAT, S_IRUSR | S_IWUSR, 0)) != NULL && + (mycnt = sem_open(CNTSEM, O_CREAT, S_IRUSR | S_IWUSR, semval)) != NULL ) { + while (semval-- >= 0) { + sleep(1); + LOG("producer: +1"); + assert( sem_post(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + assert( sem_close(mysem) == 0 && sem_close(mycnt) == 0 ); + assert( sem_unlink(TESTSEM) == 0 && sem_unlink(CNTSEM) == 0 ); + } else { + exit(1); + } + exit(0); +} + diff --git a/tests/semconfig.h b/tests/semconfig.h new file mode 100644 index 0000000..e34984d --- /dev/null +++ b/tests/semconfig.h @@ -0,0 +1,8 @@ +#ifndef CONFIG_H +#define CONFIG_H 1 + +#define LOG(text) fprintf(stderr, "%s\n", text); +#define TESTSEM "/testsem" +#define CNTSEM "/testcnt" + +#endif diff --git a/tests/semtest.c b/tests/semtest.c new file mode 100644 index 0000000..7968127 --- /dev/null +++ b/tests/semtest.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +sem_t *mysem = NULL; +pid_t child; + +#define LOG(cmd) fprintf(stderr, "%s\n", cmd); + +int main(int argc, char **argv) { + sem_unlink("/mysem"); + if ( (mysem = sem_open("/mysem", O_CREAT, S_IRUSR | S_IWUSR, 1)) != NULL ) { + if ( (child = fork()) == 0 ) { + /* child */ + sleep(1); + LOG("child: sempost"); + sem_post(mysem); + LOG("child: done"); + sleep(1); + exit(0); + } else if (child > 0) { + /* parent */ + LOG("parent: semwait"); + sem_wait(mysem); + LOG("parent: waitpid"); + waitpid(child, NULL, 0); + } else if (child < 0) { + perror("fork"); + } + } else { + sem_close(mysem); + exit(1); + } + exit(0); +} + -- cgit v1.2.3 From a108c1aec43d1f2a5dac0984a5fe9aa4b1c74b91 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 13 Oct 2015 17:42:21 +0200 Subject: better ipc: using POSIX (semaphores && msg queues) --- Makefile | 2 +- config.h | 4 ++ main.c | 129 +++++++++++++++++++++++++++++++++++++++++---------------------- ui.c | 66 +++++++++++++++++--------------- ui.h | 5 +-- 5 files changed, 126 insertions(+), 80 deletions(-) diff --git a/Makefile b/Makefile index fe89038..1abb87a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 DBGFLAGS = -g -LDFLAGS ?= $(shell ncurses5-config --libs) -pthread +LDFLAGS ?= $(shell ncurses5-config --libs) -pthread -lrt CC ?= gcc INSTALL ?= install VERSION ?= $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) diff --git a/config.h b/config.h index 26fdb3c..a559771 100644 --- a/config.h +++ b/config.h @@ -5,6 +5,10 @@ #define DEFAULT_FIFO "/lib/cryptsetup/passfifo" #define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger" +#define SEM_GUI "/naskpass-gui" +#define SEM_INP "/naskpass-input" +#define MSQ_PWD "/naskpass-passwd" + #ifdef _VERSION #define VERSION _VERSION #else diff --git a/main.c b/main.c index 670503e..c40265d 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #include "ui_ani.h" #include "ui_input.h" @@ -16,7 +19,9 @@ #include "config.h" -static bool ui_active = true; +static sem_t *sp_ui, *sp_input; +static mqd_t mq_passwd; + static void usage(char *arg0) @@ -51,23 +56,6 @@ check_fifo(char *fifo_path) return (false); } -/* stolen from http://www.gnu.org/software/libc/manual/html_node/Waiting-for-I_002fO.html */ -static int -input_timeout(int filedes, unsigned int seconds) -{ - fd_set set; - struct timeval timeout; - - /* Initialize the file descriptor set. */ - FD_ZERO (&set); - FD_SET (filedes, &set); - /* Initialize the timeout data structure. */ - timeout.tv_sec = seconds; - timeout.tv_usec = 0; - /* select returns 0 if timeout, 1 if input available, -1 if error. */ - return TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &timeout)); -} - int run_cryptcreate(char *pass, char *crypt_cmd) { @@ -80,22 +68,65 @@ run_cryptcreate(char *pass, char *crypt_cmd) return (retval); } +void sigfunc(int signal) +{ + switch (signal) { + case SIGTERM: + case SIGKILL: + case SIGINT: + sem_trywait(sp_ui); + sem_trywait(sp_input); + break; + } +} + int main(int argc, char **argv) { - int ffd, c_status, opt; + int ret = EXIT_FAILURE, ffd = -1, c_status, opt, i_sval; pid_t child; char pbuf[MAX_PASSWD_LEN+1]; char *fifo_path = NULL; char *crypt_cmd = NULL; + struct timespec ts_sem_input; + struct mq_attr mq_attr; - memset(pbuf, '\0', MAX_PASSWD_LEN+1); + signal(SIGINT, sigfunc); + signal(SIGTERM, sigfunc); + signal(SIGKILL, sigfunc); + + if ( clock_gettime(CLOCK_REALTIME, &ts_sem_input) == -1 ) { + fprintf(stderr, "%s: clock get time error: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + if ( (sp_ui = sem_open(SEM_GUI, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED || + (sp_input = sem_open(SEM_INP, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED || +(mq_passwd = mq_open(MSQ_PWD, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, NULL)) == (mqd_t) -1 ) { + if ( errno == EEXIST ) { + fprintf(stderr, "%s: already started?\n", argv[0]); + } else { + fprintf(stderr, "%s: can not create semaphore: %d (%s)\n", argv[0], errno, strerror(errno)); + } + goto error; + } + if ( mq_getattr(mq_passwd, &mq_attr) == 0 ) { + mq_attr.mq_maxmsg = 2; + mq_attr.mq_msgsize = MAX_PASSWD_LEN; + if ( mq_setattr(mq_passwd, &mq_attr, NULL) != 0 ) { + fprintf(stderr, "%s: can not SET message queue attributes: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + } else { + fprintf(stderr, "%s: can not GET message queue attributes: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + memset(pbuf, '\0', MAX_PASSWD_LEN+1); while ((opt = getopt(argc, argv, "hf:c:")) != -1) { switch (opt) { case 'h': usage(argv[0]); - exit(EXIT_SUCCESS); + goto error; case 'f': fifo_path = strdup(optarg); break; @@ -104,56 +135,66 @@ main(int argc, char **argv) break; default: usage(argv[0]); - exit(EXIT_FAILURE); + goto error; } } if (optind < argc) { fprintf(stderr, "%s: I dont understand you.\n\n", argv[0]); usage(argv[0]); - exit(EXIT_FAILURE); + goto error; } if (fifo_path == NULL) fifo_path = strdup(DEFAULT_FIFO); if (check_fifo(fifo_path) == false) { usage(argv[0]); - exit(EXIT_FAILURE); + goto error; } if ((ffd = open(fifo_path, O_NONBLOCK | O_RDWR)) < 0) { - fprintf(stderr, "fifo: %s\n", fifo_path); - perror("open"); - exit(EXIT_FAILURE); + fprintf(stderr, "%s: fifo '%s' error: %d (%s)\n", argv[0], fifo_path, errno, strerror(errno)); + goto error; } if ((child = fork()) == 0) { /* child */ - ui_active = true; - do_ui(ffd); - ui_active = false; + fclose(stderr); + /* Slave process: TUI */ + sem_post(sp_ui); + do_ui(); } else if (child > 0) { /* parent */ fclose(stdin); - while (input_timeout(ffd, 1) == 0) { - usleep(100000); - if (ui_active == true) { - // TODO: smthng + fclose(stdout); + /* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */ + while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { + if ( sem_getvalue(sp_input, &i_sval) == 0 && i_sval > 0 ) { + if (read(ffd, pbuf, MAX_PASSWD_LEN) > 0) { + if (run_cryptcreate(pbuf, crypt_cmd) != 0) { + fprintf(stderr, "cryptcreate error\n"); + } + } + } else if ( mq_receive(mq_passwd, pbuf, MAX_PASSWD_LEN, NULL) > 0 ) { +exit(77); } + usleep(100000); } - stop_ui(); wait(&c_status); - if (read(ffd, pbuf, MAX_PASSWD_LEN) > 0) { - if (run_cryptcreate(pbuf, crypt_cmd) != 0) { - fprintf(stderr, "cryptcreate error\n"); - } - } memset(pbuf, '\0', MAX_PASSWD_LEN+1); } else { /* fork error */ perror("fork"); - exit(EXIT_FAILURE); + goto error; } - close(ffd); + ret = EXIT_SUCCESS; +error: + if (ffd >= 0) close(ffd); if (crypt_cmd != NULL) free(crypt_cmd); - free(fifo_path); - return (EXIT_SUCCESS); + if (fifo_path != NULL) free(fifo_path); + sem_close(sp_ui); + sem_close(sp_input); + mq_close(mq_passwd); + sem_unlink(SEM_GUI); + sem_unlink(SEM_INP); + mq_unlink(MSQ_PWD); + exit(ret); } diff --git a/ui.c b/ui.c index 58cc226..9d0abae 100644 --- a/ui.c +++ b/ui.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include #include "ui.h" @@ -30,17 +33,18 @@ #define STRLEN(s) (sizeof(s)/sizeof(s[0])) -static int ffd; static unsigned int max_x, max_y; static WINDOW *wnd_main; static struct nask_ui *nui = NULL; static pthread_t thrd; -static bool active, passwd_from_ui; +static bool active; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; static sem_t sem_rdy; +static sem_t *sp_ui, *sp_input; +static mqd_t mq_passwd; void @@ -183,7 +187,6 @@ static int run_ui_thrd(void) { pthread_mutex_lock(&mtx_busy); active = true; - passwd_from_ui = false; pthread_cond_signal(&cnd_update); pthread_mutex_unlock(&mtx_busy); return (pthread_create(&thrd, NULL, &ui_thrd, NULL)); @@ -205,15 +208,15 @@ stop_ui_thrd(void) } static int -send_passwd(int fifo_fd, char *passwd, size_t len) +mq_passwd_send(char *passwd, size_t len) { - if (write(fifo_fd, passwd, len) != len) { - memset(passwd, '\0', len); - return (errno); - } else { - memset(passwd, '\0', len); - return (0); + struct mq_attr m_attr; + + if (mq_send(mq_passwd, "hellomq", 7, 0) == 0 && mq_getattr(mq_passwd, &m_attr) == 0) { + return m_attr.mq_curmsgs; } + memset(passwd, '\0', len); + return -1; } static bool @@ -224,9 +227,9 @@ process_key(char key, struct input *a, WINDOW *win) atmout = APP_TIMEOUT; switch (key) { case UIKEY_ENTER: - send_passwd(ffd, a->input, a->input_len); - passwd_from_ui = true; - retval = false; + if ( mq_passwd_send(a->input, a->input_len) > 0 ) { + retval = false; + } else retval = true; break; case UIKEY_BACKSPACE: del_input(win, a); @@ -262,18 +265,21 @@ infownd_update(WINDOW *win, struct txtwindow *tw) } int -do_ui(int fifo_fd) +do_ui(void) { struct input *pw_input; struct anic *heartbeat; struct statusbar *higher, *lower; struct txtwindow *infownd; char key = '\0'; - char *title; + char *title = NULL; + int i_sval = -1, ret = DOUI_ERR; asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); - ffd = fifo_fd; - if (sem_init(&sem_rdy, 0, 0) == -1) { + sp_ui = sem_open(SEM_GUI, 0, 0, 0); + sp_input = sem_open(SEM_INP, 0, 0, 0); + mq_passwd = mq_open(MSQ_PWD, O_WRONLY, S_IWUSR, NULL); + if ( sem_init(&sem_rdy, 0, 0) == -1 || !sp_ui || !sp_input || mq_passwd == (mqd_t)-1 ) { perror("init semaphore"); goto error; } @@ -295,7 +301,7 @@ do_ui(int fifo_fd) } sem_wait(&sem_rdy); wtimeout(wnd_main, 1000); - while (active == true) { + while ( active && sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { if ((key = wgetch(wnd_main)) == '\0') { break; } @@ -308,6 +314,7 @@ do_ui(int fifo_fd) do_ui_update(false); pthread_mutex_unlock(&mtx_busy); } + ui_thrd_force_update(); stop_ui_thrd(); unregister_ui_elt(lower); unregister_ui_elt(higher); @@ -319,19 +326,16 @@ do_ui(int fifo_fd) free_statusbar(lower); free_txtwindow(infownd); free_ui(); - return (DOUI_OK); + ret = DOUI_OK; + sem_trywait(sp_ui); + mq_close(mq_passwd); error: - free(title); - return (DOUI_ERR); -} - -bool -is_passwd_from_ui(void) -{ - bool ret; - pthread_mutex_lock(&mtx_busy); - ret = passwd_from_ui; - pthread_mutex_unlock(&mtx_busy); - return (ret); + if (title) free(title); + if (sp_ui) sem_close(sp_ui); + if (sp_input) sem_close(sp_input); + title = NULL; + sp_ui = NULL; + sp_input = NULL; + return ret; } diff --git a/ui.h b/ui.h index 77a5bc9..a34ca10 100644 --- a/ui.h +++ b/ui.h @@ -52,12 +52,9 @@ void free_ui(void); int -do_ui(int fifo_fd); +do_ui(void); void stop_ui(void); -bool -is_passwd_from_ui(void); - #endif -- cgit v1.2.3 From d7071577be3f49b964c4d234024bf62328d0209d Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 13 Oct 2015 17:42:21 +0200 Subject: better ipc: using POSIX (semaphores && msg queues) --- .gitignore | 1 + Makefile | 2 +- config.h | 4 ++ main.c | 129 +++++++++++++++++++++++++++++++++++++-------------------- tests/Makefile | 2 +- tests/mqtest.c | 46 ++++++++++++++++++++ ui.c | 66 +++++++++++++++-------------- ui.h | 5 +-- 8 files changed, 174 insertions(+), 81 deletions(-) create mode 100644 tests/mqtest.c diff --git a/.gitignore b/.gitignore index 4b8fd3a..3e2ffcd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /tests/producer /tests/consumer /tests/semtest +/tests/mqtest *.swp diff --git a/Makefile b/Makefile index fe89038..1abb87a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 DBGFLAGS = -g -LDFLAGS ?= $(shell ncurses5-config --libs) -pthread +LDFLAGS ?= $(shell ncurses5-config --libs) -pthread -lrt CC ?= gcc INSTALL ?= install VERSION ?= $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) diff --git a/config.h b/config.h index 26fdb3c..a559771 100644 --- a/config.h +++ b/config.h @@ -5,6 +5,10 @@ #define DEFAULT_FIFO "/lib/cryptsetup/passfifo" #define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger" +#define SEM_GUI "/naskpass-gui" +#define SEM_INP "/naskpass-input" +#define MSQ_PWD "/naskpass-passwd" + #ifdef _VERSION #define VERSION _VERSION #else diff --git a/main.c b/main.c index 670503e..c40265d 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #include "ui_ani.h" #include "ui_input.h" @@ -16,7 +19,9 @@ #include "config.h" -static bool ui_active = true; +static sem_t *sp_ui, *sp_input; +static mqd_t mq_passwd; + static void usage(char *arg0) @@ -51,23 +56,6 @@ check_fifo(char *fifo_path) return (false); } -/* stolen from http://www.gnu.org/software/libc/manual/html_node/Waiting-for-I_002fO.html */ -static int -input_timeout(int filedes, unsigned int seconds) -{ - fd_set set; - struct timeval timeout; - - /* Initialize the file descriptor set. */ - FD_ZERO (&set); - FD_SET (filedes, &set); - /* Initialize the timeout data structure. */ - timeout.tv_sec = seconds; - timeout.tv_usec = 0; - /* select returns 0 if timeout, 1 if input available, -1 if error. */ - return TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &timeout)); -} - int run_cryptcreate(char *pass, char *crypt_cmd) { @@ -80,22 +68,65 @@ run_cryptcreate(char *pass, char *crypt_cmd) return (retval); } +void sigfunc(int signal) +{ + switch (signal) { + case SIGTERM: + case SIGKILL: + case SIGINT: + sem_trywait(sp_ui); + sem_trywait(sp_input); + break; + } +} + int main(int argc, char **argv) { - int ffd, c_status, opt; + int ret = EXIT_FAILURE, ffd = -1, c_status, opt, i_sval; pid_t child; char pbuf[MAX_PASSWD_LEN+1]; char *fifo_path = NULL; char *crypt_cmd = NULL; + struct timespec ts_sem_input; + struct mq_attr mq_attr; - memset(pbuf, '\0', MAX_PASSWD_LEN+1); + signal(SIGINT, sigfunc); + signal(SIGTERM, sigfunc); + signal(SIGKILL, sigfunc); + + if ( clock_gettime(CLOCK_REALTIME, &ts_sem_input) == -1 ) { + fprintf(stderr, "%s: clock get time error: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + if ( (sp_ui = sem_open(SEM_GUI, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED || + (sp_input = sem_open(SEM_INP, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED || +(mq_passwd = mq_open(MSQ_PWD, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, NULL)) == (mqd_t) -1 ) { + if ( errno == EEXIST ) { + fprintf(stderr, "%s: already started?\n", argv[0]); + } else { + fprintf(stderr, "%s: can not create semaphore: %d (%s)\n", argv[0], errno, strerror(errno)); + } + goto error; + } + if ( mq_getattr(mq_passwd, &mq_attr) == 0 ) { + mq_attr.mq_maxmsg = 2; + mq_attr.mq_msgsize = MAX_PASSWD_LEN; + if ( mq_setattr(mq_passwd, &mq_attr, NULL) != 0 ) { + fprintf(stderr, "%s: can not SET message queue attributes: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + } else { + fprintf(stderr, "%s: can not GET message queue attributes: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + memset(pbuf, '\0', MAX_PASSWD_LEN+1); while ((opt = getopt(argc, argv, "hf:c:")) != -1) { switch (opt) { case 'h': usage(argv[0]); - exit(EXIT_SUCCESS); + goto error; case 'f': fifo_path = strdup(optarg); break; @@ -104,56 +135,66 @@ main(int argc, char **argv) break; default: usage(argv[0]); - exit(EXIT_FAILURE); + goto error; } } if (optind < argc) { fprintf(stderr, "%s: I dont understand you.\n\n", argv[0]); usage(argv[0]); - exit(EXIT_FAILURE); + goto error; } if (fifo_path == NULL) fifo_path = strdup(DEFAULT_FIFO); if (check_fifo(fifo_path) == false) { usage(argv[0]); - exit(EXIT_FAILURE); + goto error; } if ((ffd = open(fifo_path, O_NONBLOCK | O_RDWR)) < 0) { - fprintf(stderr, "fifo: %s\n", fifo_path); - perror("open"); - exit(EXIT_FAILURE); + fprintf(stderr, "%s: fifo '%s' error: %d (%s)\n", argv[0], fifo_path, errno, strerror(errno)); + goto error; } if ((child = fork()) == 0) { /* child */ - ui_active = true; - do_ui(ffd); - ui_active = false; + fclose(stderr); + /* Slave process: TUI */ + sem_post(sp_ui); + do_ui(); } else if (child > 0) { /* parent */ fclose(stdin); - while (input_timeout(ffd, 1) == 0) { - usleep(100000); - if (ui_active == true) { - // TODO: smthng + fclose(stdout); + /* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */ + while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { + if ( sem_getvalue(sp_input, &i_sval) == 0 && i_sval > 0 ) { + if (read(ffd, pbuf, MAX_PASSWD_LEN) > 0) { + if (run_cryptcreate(pbuf, crypt_cmd) != 0) { + fprintf(stderr, "cryptcreate error\n"); + } + } + } else if ( mq_receive(mq_passwd, pbuf, MAX_PASSWD_LEN, NULL) > 0 ) { +exit(77); } + usleep(100000); } - stop_ui(); wait(&c_status); - if (read(ffd, pbuf, MAX_PASSWD_LEN) > 0) { - if (run_cryptcreate(pbuf, crypt_cmd) != 0) { - fprintf(stderr, "cryptcreate error\n"); - } - } memset(pbuf, '\0', MAX_PASSWD_LEN+1); } else { /* fork error */ perror("fork"); - exit(EXIT_FAILURE); + goto error; } - close(ffd); + ret = EXIT_SUCCESS; +error: + if (ffd >= 0) close(ffd); if (crypt_cmd != NULL) free(crypt_cmd); - free(fifo_path); - return (EXIT_SUCCESS); + if (fifo_path != NULL) free(fifo_path); + sem_close(sp_ui); + sem_close(sp_input); + mq_close(mq_passwd); + sem_unlink(SEM_GUI); + sem_unlink(SEM_INP); + mq_unlink(MSQ_PWD); + exit(ret); } diff --git a/tests/Makefile b/tests/Makefile index 22a5469..5a041e6 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,5 @@ CFLAGS = -Wall -LDFLAGS = -lpthread +LDFLAGS = -lpthread -lrt CC = gcc SOURCES = $(wildcard *.c) BINARIES = $(patsubst %.c,%,$(SOURCES)) diff --git a/tests/mqtest.c b/tests/mqtest.c new file mode 100644 index 0000000..56c2d2b --- /dev/null +++ b/tests/mqtest.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include + +#include + + +static mqd_t mq_test; +static const size_t bufsiz = 256; + +int main(int argc, char **argv) +{ + struct mq_attr m_attr; + char buf[bufsiz], recv[bufsiz]; + + memset(buf, '\0', bufsiz); + memset(buf, '\0', bufsiz); + if (argc > 1) + strncpy(buf, argv[1], bufsiz-1); + + mq_unlink("/testmq"); + assert( (mq_test = mq_open( "/testmq", O_CREAT | O_EXCL | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, NULL )) != (mqd_t)-1 ); + assert( mq_getattr(mq_test, &m_attr) == 0 ); + printf("flags.....: %ld\n" + "maxmsg....: %ld\n" + "msgsize...: %ld\n" + "curmsg....: %ld\n", + m_attr.mq_flags, m_attr.mq_maxmsg, m_attr.mq_msgsize, m_attr.mq_curmsgs); + + m_attr.mq_msgsize = bufsiz-1; + assert ( mq_setattr(mq_test, &m_attr, NULL) == 0 ); + assert ( mq_send(mq_test, buf, bufsiz-1, 0) == 0 ); + assert ( mq_getattr(mq_test, &m_attr) == 0 ); + printf("new msgsize...: %ld\n" + "new curmsg....: %ld\n", + m_attr.mq_msgsize, m_attr.mq_curmsgs); + assert ( mq_receive(mq_test, recv, bufsiz-1, 0) > 0 ); + + printf("RECV: %s\n", recv); + + return 0; +} + diff --git a/ui.c b/ui.c index 58cc226..9d0abae 100644 --- a/ui.c +++ b/ui.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include #include "ui.h" @@ -30,17 +33,18 @@ #define STRLEN(s) (sizeof(s)/sizeof(s[0])) -static int ffd; static unsigned int max_x, max_y; static WINDOW *wnd_main; static struct nask_ui *nui = NULL; static pthread_t thrd; -static bool active, passwd_from_ui; +static bool active; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; static sem_t sem_rdy; +static sem_t *sp_ui, *sp_input; +static mqd_t mq_passwd; void @@ -183,7 +187,6 @@ static int run_ui_thrd(void) { pthread_mutex_lock(&mtx_busy); active = true; - passwd_from_ui = false; pthread_cond_signal(&cnd_update); pthread_mutex_unlock(&mtx_busy); return (pthread_create(&thrd, NULL, &ui_thrd, NULL)); @@ -205,15 +208,15 @@ stop_ui_thrd(void) } static int -send_passwd(int fifo_fd, char *passwd, size_t len) +mq_passwd_send(char *passwd, size_t len) { - if (write(fifo_fd, passwd, len) != len) { - memset(passwd, '\0', len); - return (errno); - } else { - memset(passwd, '\0', len); - return (0); + struct mq_attr m_attr; + + if (mq_send(mq_passwd, "hellomq", 7, 0) == 0 && mq_getattr(mq_passwd, &m_attr) == 0) { + return m_attr.mq_curmsgs; } + memset(passwd, '\0', len); + return -1; } static bool @@ -224,9 +227,9 @@ process_key(char key, struct input *a, WINDOW *win) atmout = APP_TIMEOUT; switch (key) { case UIKEY_ENTER: - send_passwd(ffd, a->input, a->input_len); - passwd_from_ui = true; - retval = false; + if ( mq_passwd_send(a->input, a->input_len) > 0 ) { + retval = false; + } else retval = true; break; case UIKEY_BACKSPACE: del_input(win, a); @@ -262,18 +265,21 @@ infownd_update(WINDOW *win, struct txtwindow *tw) } int -do_ui(int fifo_fd) +do_ui(void) { struct input *pw_input; struct anic *heartbeat; struct statusbar *higher, *lower; struct txtwindow *infownd; char key = '\0'; - char *title; + char *title = NULL; + int i_sval = -1, ret = DOUI_ERR; asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); - ffd = fifo_fd; - if (sem_init(&sem_rdy, 0, 0) == -1) { + sp_ui = sem_open(SEM_GUI, 0, 0, 0); + sp_input = sem_open(SEM_INP, 0, 0, 0); + mq_passwd = mq_open(MSQ_PWD, O_WRONLY, S_IWUSR, NULL); + if ( sem_init(&sem_rdy, 0, 0) == -1 || !sp_ui || !sp_input || mq_passwd == (mqd_t)-1 ) { perror("init semaphore"); goto error; } @@ -295,7 +301,7 @@ do_ui(int fifo_fd) } sem_wait(&sem_rdy); wtimeout(wnd_main, 1000); - while (active == true) { + while ( active && sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { if ((key = wgetch(wnd_main)) == '\0') { break; } @@ -308,6 +314,7 @@ do_ui(int fifo_fd) do_ui_update(false); pthread_mutex_unlock(&mtx_busy); } + ui_thrd_force_update(); stop_ui_thrd(); unregister_ui_elt(lower); unregister_ui_elt(higher); @@ -319,19 +326,16 @@ do_ui(int fifo_fd) free_statusbar(lower); free_txtwindow(infownd); free_ui(); - return (DOUI_OK); + ret = DOUI_OK; + sem_trywait(sp_ui); + mq_close(mq_passwd); error: - free(title); - return (DOUI_ERR); -} - -bool -is_passwd_from_ui(void) -{ - bool ret; - pthread_mutex_lock(&mtx_busy); - ret = passwd_from_ui; - pthread_mutex_unlock(&mtx_busy); - return (ret); + if (title) free(title); + if (sp_ui) sem_close(sp_ui); + if (sp_input) sem_close(sp_input); + title = NULL; + sp_ui = NULL; + sp_input = NULL; + return ret; } diff --git a/ui.h b/ui.h index 77a5bc9..a34ca10 100644 --- a/ui.h +++ b/ui.h @@ -52,12 +52,9 @@ void free_ui(void); int -do_ui(int fifo_fd); +do_ui(void); void stop_ui(void); -bool -is_passwd_from_ui(void); - #endif -- cgit v1.2.3 From 659f5274b607a1d8da3e3b30662442de764e9d7a Mon Sep 17 00:00:00 2001 From: toni Date: Thu, 22 Oct 2015 15:14:02 +0200 Subject: export ui elements to an extra module --- .gitignore | 1 + Makefile | 27 +++++++++++++------ ui.c | 75 +++++++++++++++++------------------------------------ ui.h | 12 ++++++++- ui_elements.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ui_elements.h | 20 ++++++++++++++ ui_nwindow.c | 11 ++++---- ui_nwindow.h | 12 ++++----- 8 files changed, 168 insertions(+), 73 deletions(-) create mode 100644 ui_elements.c create mode 100644 ui_elements.h diff --git a/.gitignore b/.gitignore index cc27b30..d965c57 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /tests/mqtest /tests/strsep *.swp +*.o diff --git a/Makefile b/Makefile index 1abb87a..2780c74 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,28 @@ -CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 +CFLAGS = $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 -fPIC DBGFLAGS = -g -LDFLAGS ?= $(shell ncurses5-config --libs) -pthread -lrt -CC ?= gcc -INSTALL ?= install -VERSION ?= $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) +LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt +CC = gcc +INSTALL = install +STRIP = strip +VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) BIN = naskpass -SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c main.c +SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c ui_elements.c main.c +OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) -all: $(BIN) +all: $(OBJECTS) $(BIN) + +%.o: %.c + $(CC) $(CFLAGS) -D_VERSION=\"$(VERSION)\" -c $< -o $@ $(BIN): $(SOURCES) - $(CC) $(SOURCES) -D_VERSION=\"$(VERSION)\" $(CFLAGS) $(LDFLAGS) -o $(BIN) + $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all +strip: + $(STRIP) $(BIN) + +release: all strip + debug: $(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' $(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' @@ -31,6 +41,7 @@ uninstall: rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/naskpass clean: + rm -f $(OBJECTS) rm -f $(BIN) $(MAKE) -C tests clean diff --git a/ui.c b/ui.c index 55c7103..e5f4454 100644 --- a/ui.c +++ b/ui.c @@ -15,6 +15,7 @@ #include #include "ui.h" +#include "ui_elements.h" #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" @@ -40,11 +41,11 @@ static unsigned int max_x, max_y; static WINDOW *wnd_main; static struct nask_ui *nui = NULL; +static struct nask_input *nin = NULL; static pthread_t thrd; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; static sem_t sem_rdy; static sem_t /* TUI active? */ *sp_ui, /* Textfield input available? */ *sp_input; static mqd_t mq_passwd, mq_info; @@ -74,6 +75,12 @@ register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd) } } +void +register_input(ui_input_callback ipcb, void *data, WINDOW *wnd) +{ + +} + void unregister_ui_elt(void *data) { @@ -130,23 +137,21 @@ ui_thrd(void *arg) struct timeval now; struct timespec wait; - pthread_mutex_lock(&mtx_update); gettimeofday(&now, NULL); wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; wait.tv_nsec = now.tv_usec * 1000; do_ui_update(true); sem_post(&sem_rdy); while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { - pthread_mutex_unlock(&mtx_busy); + pthread_mutex_lock(&mtx_update); cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); + if (--atmout == 0) sem_trywait(sp_ui); + do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); if (cnd_ret == ETIMEDOUT) { wait.tv_sec += UILOOP_TIMEOUT; } - pthread_mutex_lock(&mtx_busy); - if (--atmout == 0) sem_trywait(sp_ui); - do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); + pthread_mutex_unlock(&mtx_update); } - pthread_mutex_unlock(&mtx_busy); pthread_mutex_unlock(&mtx_update); return (NULL); } @@ -154,7 +159,9 @@ ui_thrd(void *arg) void ui_thrd_force_update(void) { + pthread_mutex_lock(&mtx_update); pthread_cond_signal(&cnd_update); + pthread_mutex_unlock(&mtx_update); } WINDOW * @@ -225,7 +232,6 @@ process_key(char key, struct input *a, WINDOW *win) break; case UIKEY_ESC: retval = false; - ui_thrd_force_update(); break; case UIKEY_DOWN: case UIKEY_UP: @@ -238,28 +244,10 @@ process_key(char key, struct input *a, WINDOW *win) return (retval); } -static int -lower_statusbar_update(WINDOW *win, struct statusbar *bar) -{ - char *tmp = get_system_stat(); - set_statusbar_text(bar, tmp); - free(tmp); - return (0); -} - -static int -infownd_update(WINDOW *win, struct txtwindow *tw) -{ - return (0); -} - int do_ui(void) { - struct input *pw_input; struct anic *heartbeat; - struct statusbar *higher, *lower; - struct txtwindow *infownd; char key = '\0'; char *title = NULL, mq_msg[IPC_MQSIZ+1]; int i_sval = -1, ret = DOUI_ERR; @@ -273,22 +261,11 @@ do_ui(void) perror("init semaphore/messageq"); goto error; } + + /* init TUI and UI Elements (input field, status bar, etc) */ init_ui(); - pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); - heartbeat = init_anic(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); - higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), NULL); - lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update); - infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update); + init_ui_elements(wnd_main, max_x, max_y); - register_input(NULL, pw_input); - register_statusbar(higher); - register_statusbar(lower); - register_anic(heartbeat); - register_txtwindow(infownd); - set_txtwindow_title(infownd, "WARNING"); - set_txtwindow_text(infownd, "String0---------------#\nString1--------------------#\nString2-----#"); - activate_input(wnd_main, pw_input); - set_statusbar_text(higher, title); if (run_ui_thrd() != 0) { goto error; } @@ -301,28 +278,22 @@ do_ui(void) if (key == -1) { continue; } - pthread_mutex_lock(&mtx_busy); if ( process_key(key, pw_input, wnd_main) == false ) { + curs_set(0); memset(mq_msg, '\0', IPC_MQSIZ+1); mq_receive(mq_info, mq_msg, IPC_MQSIZ+1, 0); + set_txtwindow_text(infownd, mq_msg); + set_txtwindow_active(infownd, true); + sleep(3); sem_trywait(sp_ui); } activate_input(wnd_main, pw_input); do_ui_update(false); - pthread_mutex_unlock(&mtx_busy); } ui_thrd_force_update(); stop_ui_thrd(); - unregister_ui_elt(lower); - unregister_ui_elt(higher); - unregister_ui_elt(heartbeat); - unregister_ui_elt(pw_input); - free_input(pw_input); - free_anic(heartbeat); - free_statusbar(higher); - free_statusbar(lower); - free_txtwindow(infownd); - free_ui(); + free_ui_elements(); + ret = DOUI_OK; mq_close(mq_passwd); mq_close(mq_info); diff --git a/ui.h b/ui.h index fcca2a2..dc19fab 100644 --- a/ui.h +++ b/ui.h @@ -26,9 +26,16 @@ typedef int (*ui_callback)(WINDOW *, void *, bool); +typedef int (*ui_input_callback)(WINDOW *, void *, int); + + +union ui_type { + ui_callback ui_element; + ui_input_callback ui_input; +}; struct nask_ui { - ui_callback ui_elt_cb; + union ui_type type; WINDOW *wnd; void *data; struct nask_ui *next; @@ -37,6 +44,9 @@ struct nask_ui { void register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd); +void +register_ui_input(ui_input_callback ipcb, void *data, WINDOW *wnd); + void unregister_ui_elt(void *data); diff --git a/ui_elements.c b/ui_elements.c new file mode 100644 index 0000000..b5b56e2 --- /dev/null +++ b/ui_elements.c @@ -0,0 +1,83 @@ +#include +#include +#include + +#include "ui_elements.h" + + +#define PASSWD_WIDTH 35 +#define PASSWD_HEIGHT 5 +#define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) +#define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 +#define INFOWND_WIDTH 25 +#define INFOWND_HEIGHT 3 +#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) +#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 + +static struct input *pw_input; +static struct anic *heartbeat; +static struct statusbar *higher, *lower; +static struct txtwindow *infownd; +static char *title = NULL; + + +static int +lower_statusbar_update(WINDOW *win, struct statusbar *bar) +{ + char *tmp = get_system_stat(); + set_statusbar_text(bar, tmp); + free(tmp); + return (0); +} + +static int +infownd_update(WINDOW *win, struct txtwindow *tw) +{ + char *tmp = (char*)(tw->userptr); + size_t len = strlen(tmp); + + if (tw->active) { + if ( len == 3 ) { + memset(tmp+1, '\0', 2); + } else strcat(tmp, "."); + } else (*tmp) = '.'; + return (0); +} + +void +init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) +{ + asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); + pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); + heartbeat = init_anic(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); + higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), NULL); + lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update); + infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update); + infownd->userptr = calloc(4, sizeof(char)); + (*(char*)(infownd->userptr)) = '.'; + + register_input(NULL, pw_input); + register_statusbar(higher); + register_statusbar(lower); + register_anic(heartbeat); + register_txtwindow(infownd); + set_txtwindow_title(infownd, "WARNING"); + activate_input(wnd_main, pw_input); + set_statusbar_text(higher, title); +} + +void +free_ui_elements(void) +{ + unregister_ui_elt(lower); + unregister_ui_elt(higher); + unregister_ui_elt(heartbeat); + unregister_ui_elt(pw_input); + free_input(pw_input); + free_anic(heartbeat); + free_statusbar(higher); + free_statusbar(lower); + free(infownd->userptr); + free_txtwindow(infownd); + free_ui(); +} diff --git a/ui_elements.h b/ui_elements.h new file mode 100644 index 0000000..591e18b --- /dev/null +++ b/ui_elements.h @@ -0,0 +1,20 @@ +#ifndef UI_ELEMENTS_H +#define UI_ELEMENTS_H 1 + +#include "ui.h" +#include "ui_ani.h" +#include "ui_input.h" +#include "ui_statusbar.h" +#include "ui_nwindow.h" + +#include "status.h" +#include "config.h" + + +void +init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y); + +void +free_ui_elements(void); + +#endif diff --git a/ui_nwindow.c b/ui_nwindow.c index b21bb29..1b88319 100644 --- a/ui_nwindow.c +++ b/ui_nwindow.c @@ -1,7 +1,6 @@ #include #include -#include "ui.h" #include "ui_nwindow.h" @@ -14,7 +13,7 @@ init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int a->y = y; a->width = width; a->height = height; - a->scrollable = false; + a->active = false; a->title_len = INITIAL_TITLE_LEN; a->title = calloc(a->title_len+1, sizeof(char)); a->text = NULL; @@ -74,19 +73,21 @@ print_wnd(struct txtwindow *a) mvprintw(y-2, (float)x+(float)(w/2)-(float)(a->title_len*2/3), "[ %s ]", a->title); /* print windows text */ i = 0; - while ( a->text[i] ) { + while ( a->text && a->text[i] ) { mvprintw(y+i, x, a->text[i]); i++; } attroff(a->text_attrs); } -int +static int txtwindow_cb(WINDOW *win, void *data, bool timedout) { struct txtwindow *a = (struct txtwindow *) data; - print_wnd(a); + if (a->active == true) { + print_wnd(a); + } return (UICB_OK); } diff --git a/ui_nwindow.h b/ui_nwindow.h index ee2b810..198481b 100644 --- a/ui_nwindow.h +++ b/ui_nwindow.h @@ -3,21 +3,25 @@ #include +#include "ui.h" #define INITIAL_TITLE_LEN 32 +#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() + struct txtwindow { unsigned int y; unsigned int x; unsigned int width; unsigned int height; - bool scrollable; + bool active; char *title; size_t title_len; char **text; int (*window_func)(WINDOW *, struct txtwindow *); chtype attrs; chtype text_attrs; + void *userptr; }; typedef int (*window_func)(WINDOW *, struct txtwindow *); @@ -28,18 +32,12 @@ init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int he void free_txtwindow(struct txtwindow *a); -int -txtwindow_cb(WINDOW *win, void *data, bool timedout); - void register_txtwindow(struct txtwindow *a); void set_txtwindow_text(struct txtwindow *a, char *text); -void -set_txtwindow_scrollable(struct txtwindow *a, bool scrollable); - void set_txtwindow_title(struct txtwindow *a, const char *title); -- cgit v1.2.3 From 1c8d37efdba437ab42d3d14257b7eb6adf7f2e4f Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 26 Oct 2015 00:30:52 +0100 Subject: exported UI IPC --- .gitignore | 3 + Makefile | 8 ++- config.h | 1 + main.c | 81 +++++++-------------- tests/Makefile | 5 ++ tests/semconfig.h | 1 + tests/semtest.c | 8 ++- tests/semtest2.c | 43 +++++++++++ ui.c | 209 ++++++++++++++++++++++++++++-------------------------- ui.h | 19 ++++- ui_elements.c | 62 +++++++++++++++- ui_elements.h | 7 -- ui_input.c | 13 +++- ui_input.h | 6 +- ui_ipc.c | 139 ++++++++++++++++++++++++++++++++++++ ui_ipc.h | 52 ++++++++++++++ 16 files changed, 482 insertions(+), 175 deletions(-) create mode 100644 tests/semtest2.c create mode 100644 ui_ipc.c create mode 100644 ui_ipc.h diff --git a/.gitignore b/.gitignore index d965c57..dc4c5e9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,10 @@ /tests/producer /tests/consumer /tests/semtest +/tests/semtest2 /tests/mqtest /tests/strsep *.swp *.o +*.d + diff --git a/Makefile b/Makefile index 2780c74..1e581c9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 -fPIC +CFLAGS = $(shell ncurses5-config --cflags) -Wall -Wundef -Wshadow -D_GNU_SOURCE=1 -fPIC -fomit-frame-pointer -fno-inline -fstrength-reduce -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -fstrict-aliasing -Os -MD -MP DBGFLAGS = -g LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt CC = gcc @@ -6,8 +6,9 @@ INSTALL = install STRIP = strip VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) BIN = naskpass -SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c ui_elements.c main.c +SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c ui_elements.c ui_ipc.c main.c OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) +DEPS = $(patsubst %.c,%.d,$(SOURCES)) all: $(OBJECTS) $(BIN) @@ -18,7 +19,7 @@ $(BIN): $(SOURCES) $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all -strip: +strip: $(OBJECTS) $(BIN) $(STRIP) $(BIN) release: all strip @@ -41,6 +42,7 @@ uninstall: rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/naskpass clean: + rm -f $(DEPS) rm -f $(OBJECTS) rm -f $(BIN) $(MAKE) -C tests clean diff --git a/config.h b/config.h index 4f39307..a7fddc0 100644 --- a/config.h +++ b/config.h @@ -8,6 +8,7 @@ #define SEM_GUI "/naskpass-gui" #define SEM_INP "/naskpass-input" #define SEM_BSY "/naskpass-busy" +#define SEM_RDY "/naskpass-initialized" #define MSQ_PWD "/naskpass-passwd" #define MSQ_INF "/naskpass-info" diff --git a/main.c b/main.c index 6bcfb00..58c4baf 100644 --- a/main.c +++ b/main.c @@ -12,29 +12,32 @@ #include #include +#include "config.h" + +#include "ui.h" +#include "ui_ipc.h" #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" -#include "ui.h" -#include "config.h" #define MSG(msg_idx) msg_arr[msg_idx] -static sem_t *sp_ui, *sp_input, *sp_busy; -static mqd_t mq_passwd, mq_info; - enum msg_index { MSG_BUSY_FD = 0, - MSG_BUSY + MSG_BUSY, + MSG_NO_FIFO, + MSG_FIFO_ERR, + MSG_NUM }; -static const char *msg_arr[] = { "Please wait, got a piped password ..", "Please wait, busy .." }; +static const char *msg_arr[] = { "Please wait, got a piped password ..", "Please wait, busy ..", + "check_fifo: %s is not a FIFO\n", "check_fifo: %s error(%d): %s\n" }; static void usage(char *arg0) { - fprintf(stderr, "%s (%s)\n %s\n", PKGNAME, VERSION, PKGDESC); + fprintf(stderr, "\n%s (%s)\n %s\n", PKGNAME, VERSION, PKGDESC); fprintf(stderr, " Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL); fprintf(stderr, " License GPLv3+: GNU GPL version 3 or later .\n\n"); fprintf(stderr, " Command:\n\t%s [args]\n", arg0); @@ -54,13 +57,13 @@ check_fifo(char *fifo_path) if (S_ISFIFO(st.st_mode) == 1) { return (true); } else { - fprintf(stderr, "stat: %s is not a FIFO\n", fifo_path); + fprintf(stderr, MSG(MSG_NO_FIFO), fifo_path); return (false); } } } } - perror("check_fifo"); + fprintf(stderr, MSG(MSG_FIFO_ERR), fifo_path, errno, strerror(errno)); return (false); } @@ -81,11 +84,9 @@ void sigfunc(int signal) { switch (signal) { case SIGTERM: - case SIGKILL: case SIGINT: - sem_trywait(sp_ui); - sem_trywait(sp_input); - sem_trywait(sp_busy); + ui_ipc_semtrywait(SEM_UI); + ui_ipc_semtrywait(SEM_IN); break; } } @@ -93,42 +94,23 @@ void sigfunc(int signal) int main(int argc, char **argv) { - int ret = EXIT_FAILURE, ffd = -1, c_status, opt, i_sval; + int ret = EXIT_FAILURE, ffd = -1, c_status, opt; pid_t child; char pbuf[IPC_MQSIZ+1]; char *fifo_path = NULL; char *crypt_cmd = NULL; struct timespec ts_sem_input; - struct mq_attr mq_attr; signal(SIGINT, sigfunc); signal(SIGTERM, sigfunc); - signal(SIGKILL, sigfunc); - - mq_attr.mq_flags = 0; - mq_attr.mq_msgsize = IPC_MQSIZ; - mq_attr.mq_maxmsg = 3; - mq_attr.mq_curmsgs = 0; if ( clock_gettime(CLOCK_REALTIME, &ts_sem_input) == -1 ) { fprintf(stderr, "%s: clock get time error: %d (%s)\n", argv[0], errno, strerror(errno)); goto error; } - sp_ui = sem_open(SEM_GUI, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0); - sp_input = sem_open(SEM_INP, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0); - sp_busy = sem_open(SEM_BSY, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0); - mq_passwd = mq_open(MSQ_PWD, O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &mq_attr); - mq_info = mq_open(MSQ_INF, O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &mq_attr); - - if ( sp_ui == SEM_FAILED || sp_input == SEM_FAILED || sp_busy == SEM_FAILED || - mq_passwd == (mqd_t) -1 || mq_info == (mqd_t) -1 ) { - - if ( errno == EEXIST ) { - fprintf(stderr, "%s: already started?\n", argv[0]); - } else { - fprintf(stderr, "%s: can not create semaphore/message queue: %d (%s)\n", argv[0], errno, strerror(errno)); - } + if (ui_ipc_init(1) != 0) { + fprintf(stderr, "%s: can not create semaphore/message queue: %d (%s)\n", argv[0], errno, strerror(errno)); goto error; } @@ -165,7 +147,7 @@ main(int argc, char **argv) goto error; } - sem_post(sp_ui); + ui_ipc_sempost(SEM_UI); if ((child = fork()) == 0) { /* child */ fclose(stderr); @@ -176,20 +158,20 @@ main(int argc, char **argv) fclose(stdin); fclose(stdout); /* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */ - while ( (sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0) || (sem_getvalue(sp_input, &i_sval) == 0 && i_sval > 0) ) { + while ( ui_ipc_getvalue(SEM_UI) > 0 || ui_ipc_getvalue(SEM_IN) > 0 ) { if (read(ffd, pbuf, IPC_MQSIZ) >= 0) { - sem_post(sp_busy); - mq_send(mq_info, MSG(MSG_BUSY_FD), strlen(MSG(MSG_BUSY_FD)), 0); + ui_ipc_sempost(SEM_BS); + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD), strlen(MSG(MSG_BUSY_FD))); if (run_cryptcreate(pbuf, crypt_cmd) != 0) { fprintf(stderr, "cryptcreate error\n"); } - } else if ( mq_receive(mq_passwd, pbuf, IPC_MQSIZ, NULL) >= 0 ) { - sem_post(sp_busy); - mq_send(mq_info, MSG(MSG_BUSY), strlen(MSG(MSG_BUSY)), 0); + } else if ( ui_ipc_msgrecv(MQ_PW, pbuf, IPC_MQSIZ) > 0 ) { + ui_ipc_sempost(SEM_BS); + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY), strlen(MSG(MSG_BUSY))); if (run_cryptcreate(pbuf, crypt_cmd) != 0) { fprintf(stderr, "cryptcreate error\n"); } - sem_wait(sp_input); + ui_ipc_semwait(SEM_IN); } usleep(100000); } @@ -206,15 +188,6 @@ error: if (ffd >= 0) close(ffd); if (crypt_cmd != NULL) free(crypt_cmd); if (fifo_path != NULL) free(fifo_path); - sem_close(sp_ui); - sem_close(sp_input); - sem_close(sp_busy); - mq_close(mq_passwd); - mq_close(mq_info); - sem_unlink(SEM_GUI); - sem_unlink(SEM_INP); - sem_unlink(SEM_BSY); - mq_unlink(MSQ_PWD); - mq_unlink(MSQ_INF); + ui_ipc_free(1); exit(ret); } diff --git a/tests/Makefile b/tests/Makefile index 5a041e6..436d00f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -3,13 +3,18 @@ LDFLAGS = -lpthread -lrt CC = gcc SOURCES = $(wildcard *.c) BINARIES = $(patsubst %.c,%,$(SOURCES)) +DEPS = $(patsubst %.c,%.d,$(SOURCES)) all: $(BINARIES) +semtest2: + $(CC) $(CFLAGS) $(LDFLAGS) ../ui_ipc.o semtest2.c -o semtest2 + %: %.c $(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<) clean: + rm -f $(DEPS) rm -f $(BINARIES) .PHONY: all install clean diff --git a/tests/semconfig.h b/tests/semconfig.h index e34984d..0ad08b3 100644 --- a/tests/semconfig.h +++ b/tests/semconfig.h @@ -2,6 +2,7 @@ #define CONFIG_H 1 #define LOG(text) fprintf(stderr, "%s\n", text); +#define CMD(cmd) LOG(cmd); cmd; #define TESTSEM "/testsem" #define CNTSEM "/testcnt" diff --git a/tests/semtest.c b/tests/semtest.c index 7968127..f462a6e 100644 --- a/tests/semtest.c +++ b/tests/semtest.c @@ -7,15 +7,16 @@ #include #include +#include "semconfig.h" + sem_t *mysem = NULL; pid_t child; -#define LOG(cmd) fprintf(stderr, "%s\n", cmd); int main(int argc, char **argv) { - sem_unlink("/mysem"); - if ( (mysem = sem_open("/mysem", O_CREAT, S_IRUSR | S_IWUSR, 1)) != NULL ) { + sem_unlink(TESTSEM); + if ( (mysem = sem_open(TESTSEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) != NULL ) { if ( (child = fork()) == 0 ) { /* child */ sleep(1); @@ -37,6 +38,7 @@ int main(int argc, char **argv) { sem_close(mysem); exit(1); } + sem_unlink(TESTSEM); exit(0); } diff --git a/tests/semtest2.c b/tests/semtest2.c new file mode 100644 index 0000000..c015df8 --- /dev/null +++ b/tests/semtest2.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../ui_ipc.h" + +pid_t child; + +#define LOG(cmd) fprintf(stderr, "%s\n", cmd); + +int main(int argc, char **argv) { + if (ui_ipc_init(1) != 0) { + perror("ui_ipc_init"); + exit(1); + } + if ( (child = fork()) == 0 ) { + /* child */ + sleep(1); + LOG("child: sempost"); + ui_ipc_sempost(SEM_RD); + LOG("child: done"); + sleep(1); + exit(0); + } else if (child > 0) { + /* parent */ + LOG("parent: semwait"); + ui_ipc_semwait(SEM_RD); + LOG("parent: waitpid"); + waitpid(child, NULL, 0); + } else if (child < 0) { + perror("fork"); + exit(1); + } + ui_ipc_free(1); + + exit(0); +} + diff --git a/ui.c b/ui.c index e5f4454..dc16b96 100644 --- a/ui.c +++ b/ui.c @@ -15,6 +15,7 @@ #include #include "ui.h" +#include "ui_ipc.h" #include "ui_elements.h" #include "ui_ani.h" #include "ui_input.h" @@ -40,52 +41,83 @@ static unsigned int max_x, max_y; static WINDOW *wnd_main; -static struct nask_ui *nui = NULL; -static struct nask_input *nin = NULL; +static struct nask_ui /* simple linked list to all GUI objects */ *nui = NULL, + /* simple linked list to all INPUT objects */ *nin = NULL, /* current active input */ *active = NULL; static pthread_t thrd; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; -static sem_t sem_rdy; -static sem_t /* TUI active? */ *sp_ui, /* Textfield input available? */ *sp_input; -static mqd_t mq_passwd, mq_info; -void -register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd) +static void +register_basic(enum ui_type utype, union ui_data uicb, void *data, WINDOW *wnd) { - struct nask_ui *tmp, *new; + struct nask_ui *tmp, *new, *ui = NULL; - if (nui != NULL) { - tmp = nui; - while (tmp->next != NULL) { - tmp = tmp->next; - } + switch (utype) { + case UI_ELEMENT: + ui = nui; + break; + case UI_INPUT: + ui = nin; + break; } new = calloc(1, sizeof(struct nask_ui)); - new->ui_elt_cb = uicb; + new->type = utype; + new->callback = uicb; new->wnd = wnd; new->data = data; new->next = NULL; - if (nui == NULL) { - nui = new; - nui->next = NULL; + if (ui == NULL) { + ui = new; + ui->next = NULL; + switch (utype) { + case UI_ELEMENT: + nui = ui; + break; + case UI_INPUT: + nin = ui; + break; + } } else { + tmp = ui; + while (tmp->next != NULL) { + tmp = tmp->next; + } tmp->next = new; } } void -register_input(ui_input_callback ipcb, void *data, WINDOW *wnd) +register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd) { - + union ui_data cb; + cb.ui_element = uicb; + register_basic(UI_ELEMENT, cb, data, wnd); } void -unregister_ui_elt(void *data) +register_ui_input(ui_input_callback ipcb, void *data, WINDOW *wnd) +{ + union ui_data cb; + cb.ui_input = ipcb; + register_basic(UI_INPUT, cb, data, wnd); +} + +static void +unregister_basic(enum ui_type utype, void *data) { - struct nask_ui *cur = nui, *next, *before = NULL; + struct nask_ui *cur, *next, *before = NULL, **ui = NULL; + switch (utype) { + case UI_ELEMENT: + ui = &nui; + break; + case UI_INPUT: + ui = &nin; + break; + } + cur = *ui; while (cur != NULL) { next = cur->next; if (cur->data != NULL && cur->data == data) { @@ -93,7 +125,7 @@ unregister_ui_elt(void *data) if (before != NULL) { before->next = next; } else { - nui = next; + *ui = next; } } before = cur; @@ -101,6 +133,46 @@ unregister_ui_elt(void *data) } } +void +unregister_ui_elt(void *data) +{ + unregister_basic(UI_ELEMENT, data); +} + +void +unregister_ui_input(void *data) +{ + unregister_basic(UI_INPUT, data); +} + +int +activate_ui_input(void *data) +{ + struct nask_ui *cur = nin; + + if (cur == NULL || data == NULL) return DOUI_NINIT; + while ( cur != NULL ) { + if ( cur == data && cur->type == UI_INPUT ) { + if ( cur->callback.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { + active = cur; + return DOUI_OK; + } + } + cur = cur->next; + } + return DOUI_ERR; +} + +static bool +process_key(char key) +{ + atmout = APP_TIMEOUT; + if ( active != NULL ) { + return ( active->callback.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); + } + return false; +} + static int do_ui_update(bool timed_out) { @@ -112,8 +184,8 @@ do_ui_update(bool timed_out) /* call all draw callback's */ erase(); while (cur != NULL) { - if (cur->ui_elt_cb != NULL) { - cur->ui_elt_cb(cur->wnd, cur->data, timed_out); + if (cur->type == UI_ELEMENT && cur->callback.ui_element != NULL) { + cur->callback.ui_element(cur->wnd, cur->data, timed_out); doupdate(); } else { retval = UICB_ERR_CB; @@ -133,7 +205,7 @@ do_ui_update(bool timed_out) static void * ui_thrd(void *arg) { - int cnd_ret, i_sval; + int cnd_ret; struct timeval now; struct timespec wait; @@ -141,11 +213,11 @@ ui_thrd(void *arg) wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; wait.tv_nsec = now.tv_usec * 1000; do_ui_update(true); - sem_post(&sem_rdy); - while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { + ui_ipc_sempost(SEM_RD); + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { pthread_mutex_lock(&mtx_update); cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - if (--atmout == 0) sem_trywait(sp_ui); + if (--atmout == 0) ui_ipc_semtrywait(SEM_UI); do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); if (cnd_ret == ETIMEDOUT) { wait.tv_sec += UILOOP_TIMEOUT; @@ -202,66 +274,14 @@ stop_ui_thrd(void) return (pthread_join(thrd, NULL)); } -static int -mq_passwd_send(char *passwd, size_t len) -{ - struct mq_attr m_attr; - - sem_post(sp_input); - if (mq_send(mq_passwd, passwd, len, 0) == 0 && mq_getattr(mq_passwd, &m_attr) == 0) { - return m_attr.mq_curmsgs; - } - memset(passwd, '\0', len); - return -1; -} - -static bool -process_key(char key, struct input *a, WINDOW *win) -{ - bool retval = true; - - atmout = APP_TIMEOUT; - switch (key) { - case UIKEY_ENTER: - if ( mq_passwd_send(a->input, a->input_len) > 0 ) { - retval = false; - } else retval = true; - break; - case UIKEY_BACKSPACE: - del_input(win, a); - break; - case UIKEY_ESC: - retval = false; - break; - case UIKEY_DOWN: - case UIKEY_UP: - case UIKEY_LEFT: - case UIKEY_RIGHT: - break; - default: - add_input(win, a, key); - } - return (retval); -} - int do_ui(void) { - struct anic *heartbeat; char key = '\0'; - char *title = NULL, mq_msg[IPC_MQSIZ+1]; - int i_sval = -1, ret = DOUI_ERR; + char *title = NULL; + int ret = DOUI_ERR; asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); - sp_ui = sem_open(SEM_GUI, 0, 0, 0); - sp_input = sem_open(SEM_INP, 0, 0, 0); - mq_passwd = mq_open(MSQ_PWD, O_WRONLY, 0, NULL); - mq_info = mq_open(MSQ_INF, O_RDONLY, 0, NULL); - if ( sem_init(&sem_rdy, 0, 0) == -1 || !sp_ui || !sp_input || mq_passwd == (mqd_t)-1 || mq_info == (mqd_t)-1 ) { - perror("init semaphore/messageq"); - goto error; - } - /* init TUI and UI Elements (input field, status bar, etc) */ init_ui(); init_ui_elements(wnd_main, max_x, max_y); @@ -269,25 +289,20 @@ do_ui(void) if (run_ui_thrd() != 0) { goto error; } - sem_wait(&sem_rdy); - wtimeout(wnd_main, 1000); - while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) { + ui_ipc_semwait(SEM_RD); + wtimeout(wnd_main, 10); + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { if ((key = wgetch(wnd_main)) == '\0') { break; } if (key == -1) { continue; } - if ( process_key(key, pw_input, wnd_main) == false ) { - curs_set(0); - memset(mq_msg, '\0', IPC_MQSIZ+1); - mq_receive(mq_info, mq_msg, IPC_MQSIZ+1, 0); - set_txtwindow_text(infownd, mq_msg); - set_txtwindow_active(infownd, true); - sleep(3); - sem_trywait(sp_ui); + if ( process_key(key) != true ) { +printf("BLABL\n"); + break; } - activate_input(wnd_main, pw_input); +printf("BLUBB\n"); do_ui_update(false); } ui_thrd_force_update(); @@ -295,15 +310,9 @@ do_ui(void) free_ui_elements(); ret = DOUI_OK; - mq_close(mq_passwd); - mq_close(mq_info); error: if (title) free(title); - if (sp_ui) sem_close(sp_ui); - if (sp_input) sem_close(sp_input); title = NULL; - sp_ui = NULL; - sp_input = NULL; return ret; } diff --git a/ui.h b/ui.h index dc19fab..d5a9123 100644 --- a/ui.h +++ b/ui.h @@ -12,10 +12,11 @@ #define DOUI_OK 0 #define DOUI_ERR 1 #define DOUI_TMOUT 2 -#define DOUI_PASSWD 3 +#define DOUI_NINIT 3 #define UILOOP_TIMEOUT 1 +#define UIKEY_ACTIVATE 0 #define UIKEY_ENTER 10 #define UIKEY_BACKSPACE 7 #define UIKEY_ESC 27 @@ -29,13 +30,19 @@ typedef int (*ui_callback)(WINDOW *, void *, bool); typedef int (*ui_input_callback)(WINDOW *, void *, int); -union ui_type { +enum ui_type { + UI_ELEMENT, + UI_INPUT +}; + +union ui_data { ui_callback ui_element; ui_input_callback ui_input; }; struct nask_ui { - union ui_type type; + enum ui_type type; + union ui_data callback; WINDOW *wnd; void *data; struct nask_ui *next; @@ -50,6 +57,12 @@ register_ui_input(ui_input_callback ipcb, void *data, WINDOW *wnd); void unregister_ui_elt(void *data); +void +unregister_ui_input(void *data); + +int +activate_ui_input(void *data); + void ui_thrd_force_update(void); diff --git a/ui_elements.c b/ui_elements.c index b5b56e2..b9885a0 100644 --- a/ui_elements.c +++ b/ui_elements.c @@ -2,8 +2,15 @@ #include #include +#include "ui.h" +#include "ui_ipc.h" +#include "ui_ani.h" +#include "ui_input.h" +#include "ui_statusbar.h" +#include "ui_nwindow.h" #include "ui_elements.h" +#include "status.h" #define PASSWD_WIDTH 35 #define PASSWD_HEIGHT 5 @@ -44,6 +51,57 @@ infownd_update(WINDOW *win, struct txtwindow *tw) return (0); } +static int +mq_passwd_send(char *passwd, size_t len) +{ + int ret; + + ui_ipc_sempost(SEM_IN); + ret = ui_ipc_msgsend(MQ_PW, passwd, len); + memset(passwd, '\0', len); + return ret; +} + +static int +passwd_input_cb(WINDOW *wnd, void *data, int key) +{ + struct input *a = (struct input *) data; + +/* + * if ( process_key(key, pw_input, wnd_main) == false ) { + * curs_set(0); + * memset(mq_msg, '\0', IPC_MQSIZ+1); + * mq_receive(mq_info, mq_msg, IPC_MQSIZ+1, 0); + * set_txtwindow_text(infownd, mq_msg); + * set_txtwindow_active(infownd, true); + * sleep(3); + * sem_trywait(sp_ui); + * } + * activate_input(wnd_main, pw_input); + */ + switch (key) { + case UIKEY_ENTER: + if ( mq_passwd_send(a->input, a->input_len) > 0 ) { + return DOUI_OK; + } else return DOUI_ERR; + break; + case UIKEY_BACKSPACE: + del_input(wnd, a); + break; + case UIKEY_ESC: + return DOUI_ERR; + break; + case UIKEY_DOWN: + case UIKEY_UP: + case UIKEY_LEFT: + case UIKEY_RIGHT: + break; + default: + add_input(wnd, a, key); + } + return DOUI_OK; +} + void init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) { @@ -56,7 +114,7 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) infownd->userptr = calloc(4, sizeof(char)); (*(char*)(infownd->userptr)) = '.'; - register_input(NULL, pw_input); + register_input(NULL, pw_input, passwd_input_cb); register_statusbar(higher); register_statusbar(lower); register_anic(heartbeat); @@ -72,7 +130,7 @@ free_ui_elements(void) unregister_ui_elt(lower); unregister_ui_elt(higher); unregister_ui_elt(heartbeat); - unregister_ui_elt(pw_input); + unregister_input(pw_input); free_input(pw_input); free_anic(heartbeat); free_statusbar(higher); diff --git a/ui_elements.h b/ui_elements.h index 591e18b..0cf8826 100644 --- a/ui_elements.h +++ b/ui_elements.h @@ -1,13 +1,6 @@ #ifndef UI_ELEMENTS_H #define UI_ELEMENTS_H 1 -#include "ui.h" -#include "ui_ani.h" -#include "ui_input.h" -#include "ui_statusbar.h" -#include "ui_nwindow.h" - -#include "status.h" #include "config.h" diff --git a/ui_input.c b/ui_input.c index 63d8bee..43836f1 100644 --- a/ui_input.c +++ b/ui_input.c @@ -125,7 +125,7 @@ activate_input(WINDOW *win, struct input *a) } else { wmove(win, a->y, a->x + p_len + a->cur_pos); } - return (UICB_OK); + return (activate_ui_input( (void *) a )); } int @@ -174,7 +174,16 @@ input_cb(WINDOW *win, void *data, bool timed_out) } void -register_input(WINDOW *win, struct input *a) +register_input(WINDOW *win, struct input *a, ui_input_callback uin) { + a->cb_input = uin; register_ui_elt(input_cb, (void *) a, win); + register_ui_input(uin, (void *) a, win); +} + +void +unregister_input(struct input *a) +{ + unregister_ui_input( (void *) a ); + unregister_ui_elt( (void *) a ); } diff --git a/ui_input.h b/ui_input.h index e65d560..be93864 100644 --- a/ui_input.h +++ b/ui_input.h @@ -16,6 +16,7 @@ struct input { char *prompt; chtype attrs; chtype shadow; + ui_input_callback cb_input; }; struct input * @@ -37,6 +38,9 @@ int input_cb(WINDOW *win, void *data, bool timed_out); void -register_input(WINDOW *win, struct input *a); +register_input(WINDOW *win, struct input *a, ui_input_callback uin); + +void +unregister_input(struct input *a); #endif diff --git a/ui_ipc.c b/ui_ipc.c new file mode 100644 index 0000000..60da7cb --- /dev/null +++ b/ui_ipc.c @@ -0,0 +1,139 @@ +#include +#include +#include +#ifdef SEM_TIMEDWAIT +#include +#endif +#include +#include +#include +#include + +#include "ui_ipc.h" + +#define JMP_IF(cmd, retval, jmplabel) if ( (cmd) == retval ) { printf("(%s) == %p\n", #cmd, (void*)retval); goto jmplabel; } + + +static sem_t *sems[SEM_NUM]; +static mqd_t msqs[MSQ_NUM]; +static unsigned char initialized = 0; + + +int +ui_ipc_init(int is_master) +{ + volatile int sp_oflags, mq_oflags; + mode_t crt_flags; + struct mq_attr m_attr; + + if (initialized) { + return -1; + } + bzero(sems, sizeof(sem_t*)*SEM_NUM); + bzero(msqs, sizeof(mqd_t)*MSQ_NUM); + m_attr.mq_flags = 0; + m_attr.mq_msgsize = IPC_MQSIZ; + m_attr.mq_maxmsg = 3; + m_attr.mq_curmsgs = 0; + if (is_master) { + sp_oflags = O_CREAT | O_EXCL; + mq_oflags = O_NONBLOCK | O_CREAT | O_EXCL; + crt_flags = S_IRUSR | S_IWUSR; + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + } else { + sp_oflags = 0; + mq_oflags = 0; + crt_flags = 0; + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + } + JMP_IF( sems[SEM_UI] = sem_open(SEM_GUI, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + JMP_IF( sems[SEM_IN] = sem_open(SEM_INP, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + JMP_IF( sems[SEM_BS] = sem_open(SEM_BSY, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + JMP_IF( sems[SEM_RD] = sem_open(SEM_RDY, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + initialized = 1; + return 0; +error: + return errno; +} + +void +ui_ipc_free(int is_master) +{ + int i; + + if (!initialized) { + return; + } + for (i = 0; i < SEM_NUM; i++) { + if (sems[i]) sem_close(sems[i]); + } + for (i = 0; i < MSQ_NUM; i++) { + if (msqs[i]) mq_close(msqs[i]); + } + if (is_master > 0) { + sem_unlink(SEM_BSY); + sem_unlink(SEM_GUI); + sem_unlink(SEM_INP); + sem_unlink(SEM_RDY); + mq_unlink(MSQ_PWD); + mq_unlink(MSQ_INF); + } + initialized = 0; +} + +int +ui_ipc_sempost(enum UI_IPC_SEM e_sp) +{ + return ( sem_post(sems[e_sp]) ); +} + +int +ui_ipc_semwait(enum UI_IPC_SEM e_sp) +{ + return ( sem_wait(sems[e_sp]) ); +} + +int +ui_ipc_semtrywait(enum UI_IPC_SEM e_sp) +{ + return ( sem_trywait(sems[e_sp]) ); +} + +int +ui_ipc_getvalue(enum UI_IPC_SEM e_sp) +{ + int sp_val = 0; + + if (sem_getvalue(sems[e_sp], &sp_val) != 0) { + return -1; + } + return sp_val; +} + +#ifdef SEM_TIMEDWAIT +int +ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, int timeout) +{ + struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { + return -1; + } + ts.tc_sec += timeout; + return ( sem_timedwait(sems[q_mq], &ts) ); +} +#endif + +int +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len) +{ + return ( mq_send(msqs[e_mq], msg_ptr, msg_len, 0) ); +} + +ssize_t +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len) +{ + return ( mq_receive(msqs[e_mq], msg_ptr, msg_len, NULL) ); +} + diff --git a/ui_ipc.h b/ui_ipc.h new file mode 100644 index 0000000..2c5bcb5 --- /dev/null +++ b/ui_ipc.h @@ -0,0 +1,52 @@ +#ifndef UI_IPC_H +#define UI_IPC_H 1 + +#include "status.h" +#include "config.h" + + +enum UI_IPC_SEM { + SEM_RD = 0, /* UI Init done? */ + SEM_UI, /* TUI active? */ + SEM_IN, /* Textfield has input avail */ + SEM_BS, /* Master process busy */ + SEM_NUM +}; + +enum UI_IPC_MSQ { + MQ_PW = 0, + MQ_IF, + MSQ_NUM +}; + + +int +ui_ipc_init(int is_master); + +void +ui_ipc_free(int is_master); + +int +ui_ipc_sempost(enum UI_IPC_SEM e_sp); + +int +ui_ipc_semwait(enum UI_IPC_SEM e_sp); + +int +ui_ipc_semtrywait(enum UI_IPC_SEM e_sp); + +int +ui_ipc_getvalue(enum UI_IPC_SEM e_sp); + +#ifdef SEM_TIMEDWAIT +int +ui_ipc_semtimedwait(enum UI_IPC_MSQ e_sp, int timeout); +#endif + +int +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len); + +ssize_t +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len); + +#endif -- cgit v1.2.3 From 295ca9cb2bd2ebf58e9904a75312e3d17904bb51 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 3 Nov 2015 00:14:06 +0100 Subject: some MV(C) stuff --- main.c | 4 ++++ ui.c | 5 ++--- ui.h | 7 +------ ui_elements.c | 3 ++- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 58c4baf..721be77 100644 --- a/main.c +++ b/main.c @@ -150,9 +150,13 @@ main(int argc, char **argv) ui_ipc_sempost(SEM_UI); if ((child = fork()) == 0) { /* child */ + if (ffd >= 0) close(ffd); + if (crypt_cmd != NULL) free(crypt_cmd); + if (fifo_path != NULL) free(fifo_path); fclose(stderr); /* Slave process: TUI */ do_ui(); + exit(0); } else if (child > 0) { /* parent */ fclose(stdin); diff --git a/ui.c b/ui.c index dc16b96..15d55e4 100644 --- a/ui.c +++ b/ui.c @@ -168,6 +168,7 @@ process_key(char key) { atmout = APP_TIMEOUT; if ( active != NULL ) { +printf("XXXXXXXX\n"); return ( active->callback.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); } return false; @@ -299,10 +300,8 @@ do_ui(void) continue; } if ( process_key(key) != true ) { -printf("BLABL\n"); break; - } -printf("BLUBB\n"); + } else printf("XXXXX\n"); do_ui_update(false); } ui_thrd_force_update(); diff --git a/ui.h b/ui.h index d5a9123..7273a15 100644 --- a/ui.h +++ b/ui.h @@ -30,12 +30,7 @@ typedef int (*ui_callback)(WINDOW *, void *, bool); typedef int (*ui_input_callback)(WINDOW *, void *, int); -enum ui_type { - UI_ELEMENT, - UI_INPUT -}; - -union ui_data { +struct ui_data { ui_callback ui_element; ui_input_callback ui_input; }; diff --git a/ui_elements.c b/ui_elements.c index b9885a0..4b5c8cb 100644 --- a/ui_elements.c +++ b/ui_elements.c @@ -66,7 +66,8 @@ static int passwd_input_cb(WINDOW *wnd, void *data, int key) { struct input *a = (struct input *) data; - +printf("XXXXXXX\n"); +return DOUI_OK; /* * if ( process_key(key, pw_input, wnd_main) == false ) { * curs_set(0); -- cgit v1.2.3 From 2e288bb56e9a15c4fb0fb33b4823a2d8650f9d2e Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 3 Nov 2015 12:49:31 +0100 Subject: some MV(C) stuff --- ui.c | 102 +++++++++++++++------------------------------------------ ui.h | 21 ++++-------- ui_ani.c | 5 ++- ui_elements.c | 4 +-- ui_input.c | 17 ++++------ ui_input.h | 7 ++-- ui_nwindow.c | 5 ++- ui_statusbar.c | 5 ++- 8 files changed, 55 insertions(+), 111 deletions(-) diff --git a/ui.c b/ui.c index 15d55e4..ef15d57 100644 --- a/ui.c +++ b/ui.c @@ -41,46 +41,29 @@ static unsigned int max_x, max_y; static WINDOW *wnd_main; -static struct nask_ui /* simple linked list to all GUI objects */ *nui = NULL, - /* simple linked list to all INPUT objects */ *nin = NULL, /* current active input */ *active = NULL; +static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, + /* current active input */ *active = NULL; static pthread_t thrd; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; -static void -register_basic(enum ui_type utype, union ui_data uicb, void *data, WINDOW *wnd) +void +register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd) { - struct nask_ui *tmp, *new, *ui = NULL; + struct nask_ui *tmp, *new; - switch (utype) { - case UI_ELEMENT: - ui = nui; - break; - case UI_INPUT: - ui = nin; - break; - } new = calloc(1, sizeof(struct nask_ui)); - new->type = utype; - new->callback = uicb; + new->cbs = *cbs; new->wnd = wnd; new->data = data; new->next = NULL; - if (ui == NULL) { - ui = new; - ui->next = NULL; - switch (utype) { - case UI_ELEMENT: - nui = ui; - break; - case UI_INPUT: - nin = ui; - break; - } + if (nui == NULL) { + nui = new; + nui->next = NULL; } else { - tmp = ui; + tmp = nui; while (tmp->next != NULL) { tmp = tmp->next; } @@ -89,35 +72,11 @@ register_basic(enum ui_type utype, union ui_data uicb, void *data, WINDOW *wnd) } void -register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd) -{ - union ui_data cb; - cb.ui_element = uicb; - register_basic(UI_ELEMENT, cb, data, wnd); -} - -void -register_ui_input(ui_input_callback ipcb, void *data, WINDOW *wnd) -{ - union ui_data cb; - cb.ui_input = ipcb; - register_basic(UI_INPUT, cb, data, wnd); -} - -static void -unregister_basic(enum ui_type utype, void *data) +unregister_ui_elt(void *data) { - struct nask_ui *cur, *next, *before = NULL, **ui = NULL; + struct nask_ui *cur, *next, *before = NULL; - switch (utype) { - case UI_ELEMENT: - ui = &nui; - break; - case UI_INPUT: - ui = &nin; - break; - } - cur = *ui; + cur = nui; while (cur != NULL) { next = cur->next; if (cur->data != NULL && cur->data == data) { @@ -125,7 +84,7 @@ unregister_basic(enum ui_type utype, void *data) if (before != NULL) { before->next = next; } else { - *ui = next; + nui = next; } } before = cur; @@ -133,27 +92,16 @@ unregister_basic(enum ui_type utype, void *data) } } -void -unregister_ui_elt(void *data) -{ - unregister_basic(UI_ELEMENT, data); -} - -void -unregister_ui_input(void *data) -{ - unregister_basic(UI_INPUT, data); -} - int activate_ui_input(void *data) { - struct nask_ui *cur = nin; + struct nask_ui *cur = nui; if (cur == NULL || data == NULL) return DOUI_NINIT; while ( cur != NULL ) { - if ( cur == data && cur->type == UI_INPUT ) { - if ( cur->callback.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { + if ( cur == data ) { + if ( cur->cbs.ui_input != NULL && cur->cbs.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { +printf("************\n"); active = cur; return DOUI_OK; } @@ -168,8 +116,7 @@ process_key(char key) { atmout = APP_TIMEOUT; if ( active != NULL ) { -printf("XXXXXXXX\n"); - return ( active->callback.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); + return ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); } return false; } @@ -185,8 +132,8 @@ do_ui_update(bool timed_out) /* call all draw callback's */ erase(); while (cur != NULL) { - if (cur->type == UI_ELEMENT && cur->callback.ui_element != NULL) { - cur->callback.ui_element(cur->wnd, cur->data, timed_out); + if (cur->cbs.ui_element != NULL) { + cur->cbs.ui_element(cur->wnd, cur->data, timed_out); doupdate(); } else { retval = UICB_ERR_CB; @@ -287,11 +234,13 @@ do_ui(void) init_ui(); init_ui_elements(wnd_main, max_x, max_y); + pthread_mutex_lock(&mtx_update); if (run_ui_thrd() != 0) { goto error; } ui_ipc_semwait(SEM_RD); wtimeout(wnd_main, 10); + pthread_mutex_unlock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { if ((key = wgetch(wnd_main)) == '\0') { break; @@ -299,9 +248,12 @@ do_ui(void) if (key == -1) { continue; } +/* if ( process_key(key) != true ) { break; - } else printf("XXXXX\n"); + } +*/ +process_key(key); do_ui_update(false); } ui_thrd_force_update(); diff --git a/ui.h b/ui.h index 7273a15..91d5403 100644 --- a/ui.h +++ b/ui.h @@ -26,35 +26,28 @@ #define UIKEY_RIGHT 5 -typedef int (*ui_callback)(WINDOW *, void *, bool); -typedef int (*ui_input_callback)(WINDOW *, void *, int); +typedef int (*uicb_base)(WINDOW *, void *, bool); +typedef int (*uicb_input)(WINDOW *, void *, int); -struct ui_data { - ui_callback ui_element; - ui_input_callback ui_input; +struct ui_callbacks { + uicb_base ui_element; + uicb_input ui_input; }; struct nask_ui { - enum ui_type type; - union ui_data callback; + struct ui_callbacks cbs; WINDOW *wnd; void *data; struct nask_ui *next; }; void -register_ui_elt(ui_callback uicb, void *data, WINDOW *wnd); - -void -register_ui_input(ui_input_callback ipcb, void *data, WINDOW *wnd); +register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd); void unregister_ui_elt(void *data); -void -unregister_ui_input(void *data); - int activate_ui_input(void *data); diff --git a/ui_ani.c b/ui_ani.c index d38e090..c18ad4b 100644 --- a/ui_ani.c +++ b/ui_ani.c @@ -69,5 +69,8 @@ anic_cb(WINDOW *win, void *data, bool timed_out) void register_anic(struct anic *a) { - register_ui_elt(anic_cb, (void *) a, NULL); + struct ui_callbacks cbs; + cbs.ui_element = anic_cb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); } diff --git a/ui_elements.c b/ui_elements.c index 4b5c8cb..dc24d2f 100644 --- a/ui_elements.c +++ b/ui_elements.c @@ -66,8 +66,6 @@ static int passwd_input_cb(WINDOW *wnd, void *data, int key) { struct input *a = (struct input *) data; -printf("XXXXXXX\n"); -return DOUI_OK; /* * if ( process_key(key, pw_input, wnd_main) == false ) { * curs_set(0); @@ -131,7 +129,7 @@ free_ui_elements(void) unregister_ui_elt(lower); unregister_ui_elt(higher); unregister_ui_elt(heartbeat); - unregister_input(pw_input); + unregister_ui_elt(pw_input); free_input(pw_input); free_anic(heartbeat); free_statusbar(higher); diff --git a/ui_input.c b/ui_input.c index 43836f1..21d2620 100644 --- a/ui_input.c +++ b/ui_input.c @@ -162,7 +162,7 @@ del_input(WINDOW *win, struct input *a) return (UICB_OK); } -int +static int input_cb(WINDOW *win, void *data, bool timed_out) { struct input *a = (struct input *) data; @@ -174,16 +174,11 @@ input_cb(WINDOW *win, void *data, bool timed_out) } void -register_input(WINDOW *win, struct input *a, ui_input_callback uin) +register_input(WINDOW *win, struct input *a, uicb_input ipcb) { - a->cb_input = uin; - register_ui_elt(input_cb, (void *) a, win); - register_ui_input(uin, (void *) a, win); + struct ui_callbacks cbs; + cbs.ui_element = input_cb; + cbs.ui_input = ipcb; + register_ui_elt(&cbs, (void *) a, win); } -void -unregister_input(struct input *a) -{ - unregister_ui_input( (void *) a ); - unregister_ui_elt( (void *) a ); -} diff --git a/ui_input.h b/ui_input.h index be93864..5407616 100644 --- a/ui_input.h +++ b/ui_input.h @@ -16,7 +16,7 @@ struct input { char *prompt; chtype attrs; chtype shadow; - ui_input_callback cb_input; + uicb_input cb_input; }; struct input * @@ -34,11 +34,8 @@ add_input(WINDOW *win, struct input *a, int key); int del_input(WINDOW *win, struct input *a); -int -input_cb(WINDOW *win, void *data, bool timed_out); - void -register_input(WINDOW *win, struct input *a, ui_input_callback uin); +register_input(WINDOW *win, struct input *a, uicb_input ipcb); void unregister_input(struct input *a); diff --git a/ui_nwindow.c b/ui_nwindow.c index 1b88319..aa1812d 100644 --- a/ui_nwindow.c +++ b/ui_nwindow.c @@ -94,7 +94,10 @@ txtwindow_cb(WINDOW *win, void *data, bool timedout) void inline register_txtwindow(struct txtwindow *a) { - register_ui_elt(txtwindow_cb, (void *) a, NULL); + struct ui_callbacks cbs; + cbs.ui_element = txtwindow_cb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); } static size_t diff --git a/ui_statusbar.c b/ui_statusbar.c index 8172433..8fcfeb4 100644 --- a/ui_statusbar.c +++ b/ui_statusbar.c @@ -63,7 +63,10 @@ statusbar_cb(WINDOW *win, void *data, bool timed_out) void register_statusbar(struct statusbar *a) { - register_ui_elt(statusbar_cb, (void *) a, NULL); + struct ui_callbacks cbs; + cbs.ui_element = statusbar_cb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); } inline void -- cgit v1.2.3 From 5b73c45d46f33610fa1d99c6467e24fa7861075d Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 7 Nov 2015 14:43:14 +0100 Subject: some MV(C) stuff --- Makefile | 2 +- ui.c | 23 +++++++++++------------ ui_elements.c | 23 +++++++++++++++++------ ui_input.c | 19 ++++++++++++------- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 1e581c9..738afd3 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ release: all strip debug: $(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' - $(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' + @$(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' install: $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass diff --git a/ui.c b/ui.c index ef15d57..457d76d 100644 --- a/ui.c +++ b/ui.c @@ -47,6 +47,7 @@ static pthread_t thrd; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; void @@ -98,10 +99,9 @@ activate_ui_input(void *data) struct nask_ui *cur = nui; if (cur == NULL || data == NULL) return DOUI_NINIT; - while ( cur != NULL ) { - if ( cur == data ) { + while ( cur->data != NULL ) { + if ( cur->data == data ) { if ( cur->cbs.ui_input != NULL && cur->cbs.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { -printf("************\n"); active = cur; return DOUI_OK; } @@ -162,15 +162,16 @@ ui_thrd(void *arg) wait.tv_nsec = now.tv_usec * 1000; do_ui_update(true); ui_ipc_sempost(SEM_RD); + pthread_mutex_lock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - pthread_mutex_lock(&mtx_update); + pthread_mutex_unlock(&mtx_busy); cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); + pthread_mutex_lock(&mtx_busy); if (--atmout == 0) ui_ipc_semtrywait(SEM_UI); do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); if (cnd_ret == ETIMEDOUT) { wait.tv_sec += UILOOP_TIMEOUT; } - pthread_mutex_unlock(&mtx_update); } pthread_mutex_unlock(&mtx_update); return (NULL); @@ -180,7 +181,9 @@ void ui_thrd_force_update(void) { pthread_mutex_lock(&mtx_update); + pthread_mutex_lock(&mtx_busy); pthread_cond_signal(&cnd_update); + pthread_mutex_unlock(&mtx_busy); pthread_mutex_unlock(&mtx_update); } @@ -239,7 +242,7 @@ do_ui(void) goto error; } ui_ipc_semwait(SEM_RD); - wtimeout(wnd_main, 10); + wtimeout(wnd_main, 1000); pthread_mutex_unlock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { if ((key = wgetch(wnd_main)) == '\0') { @@ -248,15 +251,11 @@ do_ui(void) if (key == -1) { continue; } -/* if ( process_key(key) != true ) { - break; +// break; } -*/ -process_key(key); - do_ui_update(false); +// do_ui_update(false); } - ui_thrd_force_update(); stop_ui_thrd(); free_ui_elements(); diff --git a/ui_elements.c b/ui_elements.c index dc24d2f..0633b42 100644 --- a/ui_elements.c +++ b/ui_elements.c @@ -34,7 +34,13 @@ lower_statusbar_update(WINDOW *win, struct statusbar *bar) char *tmp = get_system_stat(); set_statusbar_text(bar, tmp); free(tmp); - return (0); + return 0; +} + +static int +higher_statusbar_update(WINDOW *win, struct statusbar *bar) +{ + return 0; } static int @@ -48,7 +54,7 @@ infownd_update(WINDOW *win, struct txtwindow *tw) memset(tmp+1, '\0', 2); } else strcat(tmp, "."); } else (*tmp) = '.'; - return (0); + return 0; } static int @@ -80,9 +86,12 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) */ switch (key) { case UIKEY_ENTER: - if ( mq_passwd_send(a->input, a->input_len) > 0 ) { - return DOUI_OK; - } else return DOUI_ERR; + mq_passwd_send(a->input, a->input_len); + memset(a->input, '\0', a->input_len); + a->input_len = 0; + a->input_pos = 0; + a->cur_pos = 0; + ui_thrd_force_update(); break; case UIKEY_BACKSPACE: del_input(wnd, a); @@ -95,6 +104,8 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) case UIKEY_LEFT: case UIKEY_RIGHT: break; + case UIKEY_ACTIVATE: + break; default: add_input(wnd, a, key); } @@ -107,7 +118,7 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); heartbeat = init_anic(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); - higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), NULL); + higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), higher_statusbar_update); lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update); infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update); infownd->userptr = calloc(4, sizeof(char)); diff --git a/ui_input.c b/ui_input.c index 21d2620..9b70f81 100644 --- a/ui_input.c +++ b/ui_input.c @@ -101,16 +101,21 @@ print_input(WINDOW *win, struct input *a) print_wnd(3, a); attron(a->attrs); - if (win == NULL) { + if (win) { + mvwprintw(win, a->y, a->x, a->prompt); + } else { mvprintw(a->y, a->x, a->prompt); - tmp = calloc(a->width+1, sizeof(char)); - for (i = 0; i < a->width; i++) { - *(tmp + i) = '_'; - } - mvprintw(a->y, a->x + p_len, tmp); - free(tmp); + } + tmp = calloc(a->width+1, sizeof(char)); + for (i = 0; i < a->width; i++) { + *(tmp + i) = '_'; + } + if (win) { + mvwprintw(win, a->y, a->x + p_len, tmp); } else { + mvprintw(a->y, a->x + p_len, tmp); } + free(tmp); print_input_text(win, a); attroff(a->attrs); } -- cgit v1.2.3 From f2f6ea5029c6c43dc43d714978daca38c03a8a83 Mon Sep 17 00:00:00 2001 From: toni Date: Sun, 8 Nov 2015 20:28:30 +0100 Subject: - changed dir structure - fixed ipc semaphore/mq stuff --- Makefile | 4 +- config.h | 21 ----- main.c | 197 -------------------------------------- src/config.h | 21 +++++ src/main.c | 176 ++++++++++++++++++++++++++++++++++ src/opt.c | 46 +++++++++ src/opt.h | 35 +++++++ src/status.c | 29 ++++++ src/status.h | 7 ++ src/ui.c | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui.h | 66 +++++++++++++ src/ui_ani.c | 105 +++++++++++++++++++++ src/ui_ani.h | 43 +++++++++ src/ui_elements.c | 151 ++++++++++++++++++++++++++++++ src/ui_elements.h | 13 +++ src/ui_input.c | 201 +++++++++++++++++++++++++++++++++++++++ src/ui_input.h | 46 +++++++++ src/ui_ipc.c | 130 ++++++++++++++++++++++++++ src/ui_ipc.h | 52 +++++++++++ src/ui_nwindow.c | 157 +++++++++++++++++++++++++++++++ src/ui_nwindow.h | 44 +++++++++ src/ui_statusbar.c | 78 ++++++++++++++++ src/ui_statusbar.h | 32 +++++++ status.c | 29 ------ status.h | 7 -- tests/Makefile | 2 +- tests/semtest2.c | 2 +- ui.c | 268 ---------------------------------------------------- ui.h | 66 ------------- ui_ani.c | 76 --------------- ui_ani.h | 27 ------ ui_elements.c | 151 ------------------------------ ui_elements.h | 13 --- ui_input.c | 189 ------------------------------------- ui_input.h | 43 --------- ui_ipc.c | 139 --------------------------- ui_ipc.h | 52 ----------- ui_nwindow.c | 157 ------------------------------- ui_nwindow.h | 44 --------- ui_statusbar.c | 78 ---------------- ui_statusbar.h | 32 ------- 41 files changed, 1706 insertions(+), 1593 deletions(-) delete mode 100644 config.h delete mode 100644 main.c create mode 100644 src/config.h create mode 100644 src/main.c create mode 100644 src/opt.c create mode 100644 src/opt.h create mode 100644 src/status.c create mode 100644 src/status.h create mode 100644 src/ui.c create mode 100644 src/ui.h create mode 100644 src/ui_ani.c create mode 100644 src/ui_ani.h create mode 100644 src/ui_elements.c create mode 100644 src/ui_elements.h create mode 100644 src/ui_input.c create mode 100644 src/ui_input.h create mode 100644 src/ui_ipc.c create mode 100644 src/ui_ipc.h create mode 100644 src/ui_nwindow.c create mode 100644 src/ui_nwindow.h create mode 100644 src/ui_statusbar.c create mode 100644 src/ui_statusbar.h delete mode 100644 status.c delete mode 100644 status.h delete mode 100644 ui.c delete mode 100644 ui.h delete mode 100644 ui_ani.c delete mode 100644 ui_ani.h delete mode 100644 ui_elements.c delete mode 100644 ui_elements.h delete mode 100644 ui_input.c delete mode 100644 ui_input.h delete mode 100644 ui_ipc.c delete mode 100644 ui_ipc.h delete mode 100644 ui_nwindow.c delete mode 100644 ui_nwindow.h delete mode 100644 ui_statusbar.c delete mode 100644 ui_statusbar.h diff --git a/Makefile b/Makefile index 738afd3..9efaec9 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ INSTALL = install STRIP = strip VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) BIN = naskpass -SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui_nwindow.c ui.c ui_elements.c ui_ipc.c main.c +SOURCES = $(wildcard src/*.c) OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) DEPS = $(patsubst %.c,%.d,$(SOURCES)) @@ -25,7 +25,7 @@ strip: $(OBJECTS) $(BIN) release: all strip debug: - $(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' + @$(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' @$(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' install: diff --git a/config.h b/config.h deleted file mode 100644 index a7fddc0..0000000 --- a/config.h +++ /dev/null @@ -1,21 +0,0 @@ -#define AUTHOR "Toni Uhlig" -#define AUTHOR_EMAIL "matzeton@googlemail.com" -#define PKGNAME "naskpass" -#define PKGDESC "A NCurses replacement for cryptsetup's askpass." -#define DEFAULT_FIFO "/lib/cryptsetup/passfifo" -#define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger" - -#define SEM_GUI "/naskpass-gui" -#define SEM_INP "/naskpass-input" -#define SEM_BSY "/naskpass-busy" -#define SEM_RDY "/naskpass-initialized" -#define MSQ_PWD "/naskpass-passwd" -#define MSQ_INF "/naskpass-info" - -#define IPC_MQSIZ 128 - -#ifdef _VERSION -#define VERSION _VERSION -#else -#define VERSION "unknown" -#endif diff --git a/main.c b/main.c deleted file mode 100644 index 721be77..0000000 --- a/main.c +++ /dev/null @@ -1,197 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#include "ui.h" -#include "ui_ipc.h" -#include "ui_ani.h" -#include "ui_input.h" -#include "ui_statusbar.h" - -#define MSG(msg_idx) msg_arr[msg_idx] - - -enum msg_index { - MSG_BUSY_FD = 0, - MSG_BUSY, - MSG_NO_FIFO, - MSG_FIFO_ERR, - MSG_NUM -}; -static const char *msg_arr[] = { "Please wait, got a piped password ..", "Please wait, busy ..", - "check_fifo: %s is not a FIFO\n", "check_fifo: %s error(%d): %s\n" }; - - -static void -usage(char *arg0) -{ - fprintf(stderr, "\n%s (%s)\n %s\n", PKGNAME, VERSION, PKGDESC); - fprintf(stderr, " Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL); - fprintf(stderr, " License GPLv3+: GNU GPL version 3 or later .\n\n"); - fprintf(stderr, " Command:\n\t%s [args]\n", arg0); - fprintf(stderr, " Arguments:\n\t-h this\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", DEFAULT_FIFO); -} - -static bool -check_fifo(char *fifo_path) -{ - struct stat st; - - if (mkfifo(fifo_path, S_IRUSR | S_IWUSR) == 0) { - return (true); - } else { - if (errno == EEXIST) { - if (stat(fifo_path, &st) == 0) { - if (S_ISFIFO(st.st_mode) == 1) { - return (true); - } else { - fprintf(stderr, MSG(MSG_NO_FIFO), fifo_path); - return (false); - } - } - } - } - fprintf(stderr, MSG(MSG_FIFO_ERR), fifo_path, errno, strerror(errno)); - return (false); -} - -int -run_cryptcreate(char *pass, char *crypt_cmd) -{ - int retval; - char *cmd; - - if (crypt_cmd == NULL || pass == NULL) return (-1); - asprintf(&cmd, "echo '%s' | %s", pass, crypt_cmd); - retval = system(cmd); - free(cmd); - return (retval); -} - -void sigfunc(int signal) -{ - switch (signal) { - case SIGTERM: - case SIGINT: - ui_ipc_semtrywait(SEM_UI); - ui_ipc_semtrywait(SEM_IN); - break; - } -} - -int -main(int argc, char **argv) -{ - int ret = EXIT_FAILURE, ffd = -1, c_status, opt; - pid_t child; - char pbuf[IPC_MQSIZ+1]; - char *fifo_path = NULL; - char *crypt_cmd = NULL; - struct timespec ts_sem_input; - - signal(SIGINT, sigfunc); - signal(SIGTERM, sigfunc); - - if ( clock_gettime(CLOCK_REALTIME, &ts_sem_input) == -1 ) { - fprintf(stderr, "%s: clock get time error: %d (%s)\n", argv[0], errno, strerror(errno)); - goto error; - } - - if (ui_ipc_init(1) != 0) { - fprintf(stderr, "%s: can not create semaphore/message queue: %d (%s)\n", argv[0], errno, strerror(errno)); - goto error; - } - - memset(pbuf, '\0', IPC_MQSIZ+1); - while ((opt = getopt(argc, argv, "hf:c:")) != -1) { - switch (opt) { - case 'h': - usage(argv[0]); - goto error; - case 'f': - fifo_path = strdup(optarg); - break; - case 'c': - crypt_cmd = strdup(optarg); - break; - default: - usage(argv[0]); - goto error; - } - } - if (optind < argc) { - fprintf(stderr, "%s: I dont understand you.\n\n", argv[0]); - usage(argv[0]); - goto error; - } - if (fifo_path == NULL) fifo_path = strdup(DEFAULT_FIFO); - - if (check_fifo(fifo_path) == false) { - usage(argv[0]); - goto error; - } - if ((ffd = open(fifo_path, O_NONBLOCK | O_RDWR)) < 0) { - fprintf(stderr, "%s: fifo '%s' error: %d (%s)\n", argv[0], fifo_path, errno, strerror(errno)); - goto error; - } - - ui_ipc_sempost(SEM_UI); - if ((child = fork()) == 0) { - /* child */ - if (ffd >= 0) close(ffd); - if (crypt_cmd != NULL) free(crypt_cmd); - if (fifo_path != NULL) free(fifo_path); - fclose(stderr); - /* Slave process: TUI */ - do_ui(); - exit(0); - } else if (child > 0) { - /* parent */ - fclose(stdin); - fclose(stdout); - /* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */ - while ( ui_ipc_getvalue(SEM_UI) > 0 || ui_ipc_getvalue(SEM_IN) > 0 ) { - if (read(ffd, pbuf, IPC_MQSIZ) >= 0) { - ui_ipc_sempost(SEM_BS); - ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD), strlen(MSG(MSG_BUSY_FD))); - if (run_cryptcreate(pbuf, crypt_cmd) != 0) { - fprintf(stderr, "cryptcreate error\n"); - } - } else if ( ui_ipc_msgrecv(MQ_PW, pbuf, IPC_MQSIZ) > 0 ) { - ui_ipc_sempost(SEM_BS); - ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY), strlen(MSG(MSG_BUSY))); - if (run_cryptcreate(pbuf, crypt_cmd) != 0) { - fprintf(stderr, "cryptcreate error\n"); - } - ui_ipc_semwait(SEM_IN); - } - usleep(100000); - } - wait(&c_status); - memset(pbuf, '\0', IPC_MQSIZ+1); - } else { - /* fork error */ - perror("fork"); - goto error; - } - - ret = EXIT_SUCCESS; -error: - if (ffd >= 0) close(ffd); - if (crypt_cmd != NULL) free(crypt_cmd); - if (fifo_path != NULL) free(fifo_path); - ui_ipc_free(1); - exit(ret); -} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..a7fddc0 --- /dev/null +++ b/src/config.h @@ -0,0 +1,21 @@ +#define AUTHOR "Toni Uhlig" +#define AUTHOR_EMAIL "matzeton@googlemail.com" +#define PKGNAME "naskpass" +#define PKGDESC "A NCurses replacement for cryptsetup's askpass." +#define DEFAULT_FIFO "/lib/cryptsetup/passfifo" +#define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger" + +#define SEM_GUI "/naskpass-gui" +#define SEM_INP "/naskpass-input" +#define SEM_BSY "/naskpass-busy" +#define SEM_RDY "/naskpass-initialized" +#define MSQ_PWD "/naskpass-passwd" +#define MSQ_INF "/naskpass-info" + +#define IPC_MQSIZ 128 + +#ifdef _VERSION +#define VERSION _VERSION +#else +#define VERSION "unknown" +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..5f0d6e9 --- /dev/null +++ b/src/main.c @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "opt.h" + +#include "ui.h" +#include "ui_ipc.h" +#include "ui_ani.h" +#include "ui_input.h" +#include "ui_statusbar.h" + +#define MSG(msg_idx) msg_arr[msg_idx] + + +enum msg_index { + MSG_BUSY_FD = 0, + MSG_BUSY, + MSG_NO_FIFO, + MSG_FIFO_ERR, + MSG_FIFO_BUSY, + MSG_CRYPTCMD_ERR, + MSG_NUM +}; +static const char *msg_arr[] = { "Please wait, got a piped password ..", + "Please wait, busy ..", + "check_fifo: %s is not a FIFO\n", + "check_fifo: %s error(%d): %s\n", + "fifo: cryptcreate busy", + "cryptcreate error" + }; + + +static bool +check_fifo(char *fifo_path) +{ + struct stat st; + + if (mkfifo(fifo_path, S_IRUSR | S_IWUSR) == 0) { + return (true); + } else { + if (errno == EEXIST) { + if (stat(fifo_path, &st) == 0) { + if (S_ISFIFO(st.st_mode) == 1) { + return (true); + } else { + fprintf(stderr, MSG(MSG_NO_FIFO), fifo_path); + return (false); + } + } + } + } + fprintf(stderr, MSG(MSG_FIFO_ERR), fifo_path, errno, strerror(errno)); + return (false); +} + +int +run_cryptcreate(char *pass, char *crypt_cmd) +{ + int retval; + char *cmd; + + if (crypt_cmd == NULL || pass == NULL) return (-1); + asprintf(&cmd, "echo '%s' | %s", pass, crypt_cmd); + retval = system(cmd); + free(cmd); + return (retval); +} + +void sigfunc(int signal) +{ + switch (signal) { + case SIGTERM: + case SIGINT: + ui_ipc_semtrywait(SEM_UI); + ui_ipc_semtrywait(SEM_IN); + break; + } +} + +int +main(int argc, char **argv) +{ + int ret = EXIT_FAILURE, ffd = -1, c_status; + pid_t child; + char pbuf[IPC_MQSIZ+1]; + struct timespec ts_sem_input; + + signal(SIGINT, sigfunc); + signal(SIGTERM, sigfunc); + + if ( clock_gettime(CLOCK_REALTIME, &ts_sem_input) == -1 ) { + fprintf(stderr, "%s: clock get time error: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + + if (ui_ipc_init(1) != 0) { + fprintf(stderr, "%s: can not create semaphore/message queue: %d (%s)\n", argv[0], errno, strerror(errno)); + goto error; + } + + memset(pbuf, '\0', IPC_MQSIZ+1); + parse_cmd(argc, argv); + if (check_fifo(GETOPT(FIFO_PATH).str) == false) { + usage(argv[0]); + goto error; + } + if ((ffd = open(GETOPT(FIFO_PATH).str, O_NONBLOCK | O_RDWR)) < 0) { + fprintf(stderr, "%s: fifo '%s' error: %d (%s)\n", argv[0], GETOPT(FIFO_PATH).str, errno, strerror(errno)); + goto error; + } + + ui_ipc_sempost(SEM_UI); + if ((child = fork()) == 0) { + /* child */ + if (ffd >= 0) close(ffd); + fclose(stderr); + /* Slave process: TUI */ + if (ui_ipc_init(0) == 0) { + ui_ipc_semwait(SEM_BS); + do_ui(); + } + exit(0); + } else if (child > 0) { + /* parent */ + fclose(stdin); + fclose(stdout); + ui_ipc_sempost(SEM_BS); + ui_ipc_sempost(SEM_UI); + /* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */ + while ( ui_ipc_getvalue(SEM_UI) > 0 || ui_ipc_getvalue(SEM_IN) > 0 ) { + ui_ipc_sempost(SEM_BS); + if (read(ffd, pbuf, IPC_MQSIZ) >= 0) { + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD), strlen(MSG(MSG_BUSY_FD))); + if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { + ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR), strlen(MSG(MSG_CRYPTCMD_ERR))); + } + } else if ( ui_ipc_msgrecv(MQ_PW, pbuf, IPC_MQSIZ) != (int)-1 ) { + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY), strlen(MSG(MSG_BUSY))); + if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { + ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR), strlen(MSG(MSG_CRYPTCMD_ERR))); + } + ui_ipc_semwait(SEM_IN); + } + ui_ipc_semwait(SEM_BS); + usleep(100000); + waitpid(child, &c_status, WNOHANG); + if ( WIFSIGNALED(c_status) != 0 ) { + break; + } + } + wait(&c_status); + memset(pbuf, '\0', IPC_MQSIZ+1); + } else { + /* fork error */ + perror("fork"); + goto error; + } + + ret = EXIT_SUCCESS; +error: + if (ffd >= 0) close(ffd); + ui_ipc_free(1); + exit(ret); +} diff --git a/src/opt.c b/src/opt.c new file mode 100644 index 0000000..7b6d083 --- /dev/null +++ b/src/opt.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +#include "config.h" +#include "opt.h" + +#define CONFIG_OPT(default_val) { {0},0,{default_val} } + +struct opt config_opts[] = { CONFIG_OPT(DEFAULT_FIFO), CONFIG_OPT(NULL) }; +const int opt_siz = ( sizeof(config_opts)/sizeof(config_opts[0]) ); + + +void +usage(char *arg0) +{ + fprintf(stderr, "\n%s (%s)\n %s\n", PKGNAME, VERSION, PKGDESC); + fprintf(stderr, " Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL); + fprintf(stderr, " License GPLv3+: GNU GPL version 3 or later .\n\n"); + fprintf(stderr, " Command:\n\t%s [args]\n", arg0); + fprintf(stderr, " Arguments:\n\t-h this\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", DEFAULT_FIFO); +} + +int +parse_cmd(int argc, char **argv) +{ + int opt; + + while ((opt = getopt(argc, argv, "hf:c:")) != -1) { + switch (opt) { + case 'h': + usage(argv[0]); + return 1; + case 'f': + s_OPT(FIFO_PATH, strdup(optarg)); + break; + case 'c': + s_OPT(CRYPT_CMD, strdup(optarg)); + break; + } + } + return 0; +} + diff --git a/src/opt.h b/src/opt.h new file mode 100644 index 0000000..6d66101 --- /dev/null +++ b/src/opt.h @@ -0,0 +1,35 @@ +#ifndef OPT_H +#define OPT_H 1 + +#define OPT(opt_index) config_opts[opt_index] +#define GETOPT(opt_index) OPT(opt_index).opt +#define OPT_USED(opt_index, uvalue) OPT(opt_index).found = uvalue; +#define d_OPT(opt_index, rvalue) OPT(opt_index).opt.dec = rvalue; OPT_USED(opt_index, 1); +#define s_OPT(opt_index, rvalue) OPT(opt_index).opt.str = rvalue; OPT_USED(opt_index, 1); + +union opt_entry { + char *str; + int *dec; +}; + +struct opt { + union opt_entry opt; + unsigned char found; + const union opt_entry def; +}; + +enum opt_index { + FIFO_PATH = 0, + CRYPT_CMD +}; + + +extern struct opt config_opts[]; + +void +usage(char *arg0); + +int +parse_cmd(int argc, char **argv); + +#endif diff --git a/src/status.c b/src/status.c new file mode 100644 index 0000000..5286ddf --- /dev/null +++ b/src/status.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +#include "status.h" + + +char * +get_system_stat(void) +{ + char *retstr = NULL; + int ncpu; + struct sysinfo inf; + + if (sysinfo(&inf) == EFAULT) { + return ("[SYSINFO ERROR]"); + } + ncpu = get_nprocs(); + + if (asprintf(&retstr, "u:%04ld - l:%3.2f,%3.2f,%3.2f - %dcore%s - mem:%lu/%lumb - procs:%02d", + inf.uptime, ((float)inf.loads[0]/10000), ((float)inf.loads[1]/10000), ((float)inf.loads[2]/10000), + ncpu, (ncpu > 1 ? "s" : ""), + (unsigned long)((inf.freeram/1024)/1024), (unsigned long)((inf.totalram/1024)/1024), inf.procs) == -1) { + return ("[ASPRINTF ERROR]"); + } + return (retstr); +} diff --git a/src/status.h b/src/status.h new file mode 100644 index 0000000..995d08a --- /dev/null +++ b/src/status.h @@ -0,0 +1,7 @@ +#ifndef STATUS_H +#define STATUS_H 1 + +char * +get_system_stat(void); + +#endif diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..2fb5a1f --- /dev/null +++ b/src/ui.c @@ -0,0 +1,270 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ui.h" +#include "ui_ipc.h" +#include "ui_elements.h" +#include "ui_ani.h" +#include "ui_input.h" +#include "ui_statusbar.h" +#include "ui_nwindow.h" + +#include "status.h" +#include "config.h" + +#define APP_TIMEOUT 60 +#define APP_TIMEOUT_FMT "%02d" +#define PASSWD_WIDTH 35 +#define PASSWD_HEIGHT 5 +#define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) +#define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 +#define INFOWND_WIDTH 25 +#define INFOWND_HEIGHT 3 +#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) +#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 + +#define STRLEN(s) (sizeof(s)/sizeof(s[0])) + + +static unsigned int max_x, max_y; +static WINDOW *wnd_main; +static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, + /* current active input */ *active = NULL; +static pthread_t thrd; +static unsigned int atmout = APP_TIMEOUT; +static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; + + +void +register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd) +{ + struct nask_ui *tmp, *new; + + new = calloc(1, sizeof(struct nask_ui)); + new->cbs = *cbs; + new->wnd = wnd; + new->data = data; + new->next = NULL; + if (nui == NULL) { + nui = new; + nui->next = NULL; + } else { + tmp = nui; + while (tmp->next != NULL) { + tmp = tmp->next; + } + tmp->next = new; + } +} + +void +unregister_ui_elt(void *data) +{ + struct nask_ui *cur, *next, *before = NULL; + + cur = nui; + while (cur != NULL) { + next = cur->next; + if (cur->data != NULL && cur->data == data) { + free(cur); + if (before != NULL) { + before->next = next; + } else { + nui = next; + } + } + before = cur; + cur = next; + } +} + +int +activate_ui_input(void *data) +{ + struct nask_ui *cur = nui; + + if (cur == NULL || data == NULL) return DOUI_NINIT; + while ( cur->data != NULL ) { + if ( cur->data == data ) { + if ( cur->cbs.ui_input != NULL && cur->cbs.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { + active = cur; + return DOUI_OK; + } + } + cur = cur->next; + } + return DOUI_ERR; +} + +int +reactivate_ui_input(void) +{ + if (active) { + if (active->cbs.ui_element) { + } + return ( active->cbs.ui_input(active->wnd, active->data, UIKEY_ACTIVATE) ); + } else return DOUI_ERR; +} + +static bool +process_key(char key) +{ + atmout = APP_TIMEOUT; + if ( active != NULL ) { + return ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); + } + return false; +} + +static int +do_ui_update(bool timed_out) +{ + int retval = UICB_OK; + int curx = getcurx(wnd_main); + int cury = getcury(wnd_main); + struct nask_ui *cur = nui; + + /* call all draw callback's */ + erase(); + while (cur != NULL) { + if (cur->cbs.ui_element != NULL) { + cur->cbs.ui_element(cur->wnd, cur->data, timed_out); + doupdate(); + } else { + retval = UICB_ERR_CB; + } + cur = cur->next; + } + /* TODO: Maybe export to an extra module? */ + attron(COLOR_PAIR(1)); + mvprintw(0, max_x - STRLEN(APP_TIMEOUT_FMT), "[" APP_TIMEOUT_FMT "]", atmout); + attroff(COLOR_PAIR(1)); + /* EoT (End of Todo) */ + wmove(wnd_main, cury, curx); + wrefresh(wnd_main); + return (retval); +} + +static void * +ui_thrd(void *arg) +{ + int cnd_ret; + struct timeval now; + struct timespec wait; + + gettimeofday(&now, NULL); + wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; + wait.tv_nsec = now.tv_usec * 1000; + do_ui_update(true); + ui_ipc_sempost(SEM_RD); + pthread_mutex_lock(&mtx_update); + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { + pthread_mutex_unlock(&mtx_busy); + cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); + pthread_mutex_lock(&mtx_busy); + if (--atmout == 0) ui_ipc_semtrywait(SEM_UI); + do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); + if (cnd_ret == ETIMEDOUT) { + wait.tv_sec += UILOOP_TIMEOUT; + } + } + pthread_mutex_unlock(&mtx_update); + return (NULL); +} + +void +ui_thrd_force_update(void) +{ + pthread_mutex_lock(&mtx_busy); + pthread_cond_signal(&cnd_update); + pthread_mutex_unlock(&mtx_busy); +} + +WINDOW * +init_ui(void) +{ + wnd_main = initscr(); + max_x = getmaxx(wnd_main); + max_y = getmaxy(wnd_main); + start_color(); + init_pair(1, COLOR_RED, COLOR_WHITE); + init_pair(2, COLOR_WHITE, COLOR_BLACK); + init_pair(3, COLOR_BLACK, COLOR_WHITE); + init_pair(4, COLOR_YELLOW, COLOR_RED); + raw(); + keypad(stdscr, TRUE); + noecho(); + cbreak(); + return (wnd_main); +} + +void +free_ui(void) +{ + delwin(wnd_main); + endwin(); + clear(); + printf(" \033[2J\n"); +} + +static int +run_ui_thrd(void) { + return (pthread_create(&thrd, NULL, &ui_thrd, NULL)); +} + +static int +stop_ui_thrd(void) +{ + return (pthread_join(thrd, NULL)); +} + +int +do_ui(void) +{ + char key = '\0'; + int ret = DOUI_ERR; + + /* init TUI and UI Elements (input field, status bar, etc) */ + init_ui(); + init_ui_elements(wnd_main, max_x, max_y); + + pthread_mutex_lock(&mtx_update); + if (run_ui_thrd() != 0) { + return ret; + } + ui_ipc_semwait(SEM_RD); + wtimeout(wnd_main, 1000); + pthread_mutex_unlock(&mtx_update); + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { + if ((key = wgetch(wnd_main)) == '\0') { + break; + } + if (key == -1) { + continue; + } + if ( process_key(key) != true ) { + ui_ipc_semtrywait(SEM_UI); + ui_thrd_force_update(); + } + } + stop_ui_thrd(); + free_ui_elements(); + + return DOUI_OK; +} + diff --git a/src/ui.h b/src/ui.h new file mode 100644 index 0000000..91d5403 --- /dev/null +++ b/src/ui.h @@ -0,0 +1,66 @@ +#ifndef UI_H +#define UI_H 1 + +#include +#include + +#define UICB_OK 0 +#define UICB_ERR_UNDEF 1 +#define UICB_ERR_CB 2 +#define UICB_ERR_BUF 3 + +#define DOUI_OK 0 +#define DOUI_ERR 1 +#define DOUI_TMOUT 2 +#define DOUI_NINIT 3 + +#define UILOOP_TIMEOUT 1 + +#define UIKEY_ACTIVATE 0 +#define UIKEY_ENTER 10 +#define UIKEY_BACKSPACE 7 +#define UIKEY_ESC 27 +#define UIKEY_DOWN 2 +#define UIKEY_UP 3 +#define UIKEY_LEFT 4 +#define UIKEY_RIGHT 5 + + +typedef int (*uicb_base)(WINDOW *, void *, bool); +typedef int (*uicb_input)(WINDOW *, void *, int); + + +struct ui_callbacks { + uicb_base ui_element; + uicb_input ui_input; +}; + +struct nask_ui { + struct ui_callbacks cbs; + WINDOW *wnd; + void *data; + struct nask_ui *next; +}; + +void +register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd); + +void +unregister_ui_elt(void *data); + +int +activate_ui_input(void *data); + +void +ui_thrd_force_update(void); + +WINDOW * +init_ui(void); + +void +free_ui(void); + +int +do_ui(void); + +#endif diff --git a/src/ui_ani.c b/src/ui_ani.c new file mode 100644 index 0000000..d1f1073 --- /dev/null +++ b/src/ui_ani.c @@ -0,0 +1,105 @@ +#include +#include + +#include "ui.h" +#include "ui_ani.h" + +#define ANIC_INITSTATE '|' + + +struct anic * +init_anic_default(unsigned int x, unsigned int y, chtype attrs, char *fmt) +{ + struct anic *a = init_anic(x, y, attrs, anic_cb); + struct anic_default *b = calloc(1, sizeof(struct anic_default)); + + a->data = (void *) b; + b->state = ANIC_INITSTATE; + if (fmt != NULL) { + b->fmt = strdup(fmt); + } + return (a); +} + +struct anic * +init_anic(unsigned int x, unsigned int y, chtype attrs, uicb_anic uicb) +{ + struct anic *a = calloc(1, sizeof(struct anic)); + + a->x = x; + a->y = y; + a->uicb = uicb; + a->attrs = attrs; + return (a); +} + +void +free_anic(struct anic *a) +{ + free(a); +} + +void +free_anic_default(struct anic *a) +{ + struct anic_default *b; + + if (a->data != NULL) { + b = (struct anic_default *) a->data; + free(b->fmt); + free(b); + } + free_anic(a); +} + +int +anic_cb(WINDOW *win, void *data, bool timed_out) +{ + struct anic *a = (struct anic *) data; + struct anic_default *b; + char *tmp; + int retval = UICB_OK; + + if (a == NULL) return (UICB_ERR_UNDEF); + b = (struct anic_default *) a->data; + if (timed_out == true) { + switch (b->state) { + default: + case '|': b->state = '/'; break; + case '/': b->state = '-'; break; + case '-': b->state = '\\'; break; + case '\\': b->state = '|'; break; + } + } + attron(a->attrs); + if (b->fmt != NULL) { + if (asprintf(&tmp, b->fmt, b->state) <= 0) { + retval = UICB_ERR_BUF; + } + } else { + asprintf(&tmp, "%c", b->state); + } + if (win != NULL) { + mvwprintw(win, a->y, a->x, tmp); + } else { + mvprintw(a->y, a->x, tmp); + } + free(tmp); + attroff(a->attrs); + return (retval); +} + +void +register_anic(struct anic *a, uicb_anic uicb) +{ + struct ui_callbacks cbs; + cbs.ui_element = uicb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); +} + +void +register_anic_default(struct anic *a) +{ + register_anic(a, anic_cb); +} diff --git a/src/ui_ani.h b/src/ui_ani.h new file mode 100644 index 0000000..3d6ece2 --- /dev/null +++ b/src/ui_ani.h @@ -0,0 +1,43 @@ +#ifndef UI_ANIC_H +#define UI_ANIC_H 1 + +#include + + +typedef int (*uicb_anic)(WINDOW *, void *, bool); + +struct anic_default { + char state; + char *fmt; +}; + +struct anic { + unsigned int x; + unsigned int y; + uicb_anic uicb; + void *data; + chtype attrs; +}; + +struct anic * +init_anic_default(unsigned int x, unsigned int y, chtype attrs, char *fmt); + +struct anic * +init_anic(unsigned int x, unsigned int y, chtype attrs, uicb_anic uicb); + +void +free_anic_default(struct anic *a); + +void +free_anic(struct anic *a); + +int +anic_cb(WINDOW *win, void *data, bool timed_out); + +void +register_anic(struct anic *a, uicb_anic uicb); + +void +register_anic_default(struct anic *a); + +#endif diff --git a/src/ui_elements.c b/src/ui_elements.c new file mode 100644 index 0000000..29c2e32 --- /dev/null +++ b/src/ui_elements.c @@ -0,0 +1,151 @@ +#include +#include +#include + +#include "ui.h" +#include "ui_ipc.h" +#include "ui_ani.h" +#include "ui_input.h" +#include "ui_statusbar.h" +#include "ui_nwindow.h" +#include "ui_elements.h" + +#include "status.h" + +#define PASSWD_WIDTH 35 +#define PASSWD_HEIGHT 5 +#define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) +#define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 +#define INFOWND_WIDTH 25 +#define INFOWND_HEIGHT 3 +#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) +#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 + +static struct input *pw_input; +static struct anic *heartbeat; +static struct statusbar *higher, *lower; +static struct txtwindow *infownd; +static char *title = NULL; + + +static int +lower_statusbar_update(WINDOW *win, struct statusbar *bar) +{ + char *tmp = get_system_stat(); + set_statusbar_text(bar, tmp); + free(tmp); + return 0; +} + +static int +higher_statusbar_update(WINDOW *win, struct statusbar *bar) +{ + return 0; +} + +static int +infownd_update(WINDOW *win, struct txtwindow *tw) +{ + char *tmp = (char*)(tw->userptr); + size_t len = strlen(tmp); + + if (tw->active) { + if ( len == 3 ) { + memset(tmp+1, '\0', 2); + } else strcat(tmp, "."); + } else (*tmp) = '.'; + return 0; +} + +static int +mq_passwd_send(char *passwd, size_t len) +{ + int ret; + + ui_ipc_sempost(SEM_IN); + ret = ui_ipc_msgsend(MQ_PW, passwd, len); + return ret; +} + +static int +passwd_input_cb(WINDOW *wnd, void *data, int key) +{ + struct input *a = (struct input *) data; +/* + * if ( process_key(key, pw_input, wnd_main) == false ) { + * curs_set(0); + * memset(mq_msg, '\0', IPC_MQSIZ+1); + * mq_receive(mq_info, mq_msg, IPC_MQSIZ+1, 0); + * set_txtwindow_text(infownd, mq_msg); + * set_txtwindow_active(infownd, true); + * sleep(3); + * sem_trywait(sp_ui); + * } + * activate_input(wnd_main, pw_input); + */ + switch (key) { + case UIKEY_ENTER: + mq_passwd_send(a->input, a->input_len); + clear_input(wnd, a); + set_txtwindow_text(infownd, "BLA"); + set_txtwindow_active(infownd, true); + break; + case UIKEY_BACKSPACE: + del_input(wnd, a); + break; + case UIKEY_ESC: + return DOUI_ERR; + case UIKEY_DOWN: + case UIKEY_UP: + case UIKEY_LEFT: + case UIKEY_RIGHT: + break; + case UIKEY_ACTIVATE: + break; + default: + add_input(wnd, a, key); + } + return DOUI_OK; +} + +void +init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) +{ + asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); + pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); + heartbeat = init_anic_default(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); + higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), higher_statusbar_update); + lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update); + infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update); + infownd->userptr = calloc(4, sizeof(char)); + (*(char*)(infownd->userptr)) = '.'; + + register_input(NULL, pw_input, passwd_input_cb); + register_statusbar(higher); + register_statusbar(lower); + register_anic_default(heartbeat); + register_txtwindow(infownd); + set_txtwindow_title(infownd, "WARNING"); + activate_input(wnd_main, pw_input); + set_statusbar_text(higher, title); +} + +void +free_ui_elements(void) +{ + unregister_ui_elt(lower); + unregister_ui_elt(higher); + unregister_ui_elt(heartbeat); + unregister_ui_elt(pw_input); + free_input(pw_input); + free_anic_default(heartbeat); + free_statusbar(higher); + free_statusbar(lower); + free(infownd->userptr); + free_txtwindow(infownd); + free_ui(); + if (title) { + free(title); + title = NULL; + } +} diff --git a/src/ui_elements.h b/src/ui_elements.h new file mode 100644 index 0000000..0cf8826 --- /dev/null +++ b/src/ui_elements.h @@ -0,0 +1,13 @@ +#ifndef UI_ELEMENTS_H +#define UI_ELEMENTS_H 1 + +#include "config.h" + + +void +init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y); + +void +free_ui_elements(void); + +#endif diff --git a/src/ui_input.c b/src/ui_input.c new file mode 100644 index 0000000..5f9a679 --- /dev/null +++ b/src/ui_input.c @@ -0,0 +1,201 @@ +#include +#include + +#include "ui.h" +#include "ui_input.h" + + +struct input * +init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs, chtype shadow) +{ + struct input *a = calloc(1, sizeof(struct input)); + + a->x = x; + a->y = y; + a->width = width; + a->cur_pos = 0; + a->input = calloc(input_len+1, sizeof(char)); + a->input_max = input_len; + a->input_len = 0; + a->input_pos = 0; + a->prompt = strdup(prompt); + a->attrs = attrs; + a->shadow = shadow; + return (a); +} + +void +free_input(struct input *a) +{ + if (a->input != NULL) { + free(a->input); + } + free(a->prompt); + free(a); +} + +static void +print_wnd(size_t addwidth, struct input *a) +{ + int i, x = a->x, y = a->y; + size_t relwidth = addwidth*2, len = strlen(a->prompt) + a->width; + char tmp[len+relwidth+1]; + + attron(a->attrs); + memset(tmp, ' ', len+relwidth); + tmp[len+relwidth] = '\0'; + for (i = -1; i <= 1; i++) + mvprintw(y+i, x-addwidth, tmp); + + mvhline(y-2, x-addwidth, 0, len+relwidth); + mvhline(y+2, x-addwidth, 0, len+relwidth); + mvvline(y-1, x-addwidth-1, 0, 3); + mvvline(y-1, x+len+addwidth, 0, 3); + mvaddch(y-2, x-addwidth-1, ACS_ULCORNER); + mvaddch(y+2, x-addwidth-1, ACS_LLCORNER); + mvaddch(y-2, x+len+addwidth, ACS_URCORNER); + mvaddch(y+2, x+len+addwidth, ACS_LRCORNER); + attroff(a->attrs); + + attron(a->shadow); + for (i = x-addwidth+1; i < x+len+relwidth; i++) + mvaddch(y+3, i, ACS_CKBOARD); + for (i = -1; i < 3; i++) { + mvaddch(y+i, x+len+relwidth-2, ACS_CKBOARD); + mvaddch(y+i, x+len+relwidth-1, ACS_CKBOARD); + } + attroff(a->shadow); +} + +static void +print_input_text(WINDOW *win, struct input *a) +{ + size_t start = 0; + size_t p_len = strlen(a->prompt); + + char tmp[a->width + 1]; + memset(tmp, '\0', a->width + 1); + if (a->input_pos >= a->width) { + start = a->input_pos - a->width; + } + + strncpy(tmp, (char *)(a->input + start), a->width); + int i; + for (i = 0; i < strlen(tmp); i++) { + tmp[i] = '*'; + } + + if (win == NULL) { + mvprintw(a->y, a->x + p_len, "%s", tmp); + } else { + mvwprintw(win, a->y, a->x + p_len, "%s", tmp); + } +} + +static void +print_input(WINDOW *win, struct input *a) +{ + char *tmp; + int i; + size_t p_len = strlen(a->prompt); + + print_wnd(3, a); + attron(a->attrs); + if (win) { + mvwprintw(win, a->y, a->x, a->prompt); + } else { + mvprintw(a->y, a->x, a->prompt); + } + tmp = calloc(a->width+1, sizeof(char)); + for (i = 0; i < a->width; i++) { + *(tmp + i) = '_'; + } + if (win) { + mvwprintw(win, a->y, a->x + p_len, tmp); + } else { + mvprintw(a->y, a->x + p_len, tmp); + } + free(tmp); + print_input_text(win, a); + attroff(a->attrs); +} + +int +activate_input(WINDOW *win, struct input *a) +{ + if (a == NULL) return (UICB_ERR_UNDEF); + size_t p_len = strlen(a->prompt); + if (win == NULL) { + move(a->y, a->x + p_len + a->cur_pos); + } else { + wmove(win, a->y, a->x + p_len + a->cur_pos); + } + return (activate_ui_input( (void *) a )); +} + +int +add_input(WINDOW *win, struct input *a, int key) +{ + if (a == NULL) return (UICB_ERR_UNDEF); + if (a->input_len >= a->input_max) return (UICB_ERR_BUF); + *(a->input + a->input_pos) = (char) key; + ++a->input_pos; + ++a->input_len; + a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); + print_input(win, a); + return (UICB_OK); +} + +int +del_input(WINDOW *win, struct input *a) +{ + if (a == NULL) return (UICB_ERR_UNDEF); + if (a->input_len == 0) return (UICB_ERR_BUF); + memmove((a->input + a->input_pos - 1), (a->input + a->input_pos), a->input_max - a->input_pos); + --a->input_len; + *(a->input + a->input_len) = '\0'; + if (a->input_pos-1 == a->input_len) { + --a->input_pos; + } + if (a->cur_pos+1 < a->width && a->cur_pos > 0) { + --a->cur_pos; + } else if (a->cur_pos-1 == a->input_pos) { + --a->cur_pos; + } + mvwprintw(win, a->y, a->x + a->cur_pos + strlen(a->prompt), "_"); + print_input(win, a); + return (UICB_OK); +} + +int +clear_input(WINDOW *win, struct input *a) +{ + if (a == NULL) return (UICB_ERR_UNDEF); + memset(a->input, '\0', a->input_max); + a->input_len = 0; + a->input_pos = 0; + a->cur_pos = 0; + print_input(win, a); + return (UICB_OK); +} + +static int +input_cb(WINDOW *win, void *data, bool timed_out) +{ + struct input *a = (struct input *) data; + + if (a == NULL) return (UICB_ERR_UNDEF); + if (win != NULL && is_wintouched(win) == false) return (UICB_OK); + print_input(win, a); + return (UICB_OK); +} + +void +register_input(WINDOW *win, struct input *a, uicb_input ipcb) +{ + struct ui_callbacks cbs; + cbs.ui_element = input_cb; + cbs.ui_input = ipcb; + register_ui_elt(&cbs, (void *) a, win); +} + diff --git a/src/ui_input.h b/src/ui_input.h new file mode 100644 index 0000000..657c926 --- /dev/null +++ b/src/ui_input.h @@ -0,0 +1,46 @@ +#ifndef UI_INPUT_H +#define UI_INPUT_H 1 + +#include + + +struct input { + unsigned int x; + unsigned int y; + unsigned int width; + unsigned int cur_pos; + char *input; + size_t input_max; + size_t input_len; + size_t input_pos; + char *prompt; + chtype attrs; + chtype shadow; + uicb_input cb_input; +}; + +struct input * +init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs, chtype shadow); + +void +free_input(struct input *a); + +int +activate_input(WINDOW *win, struct input *a); + +int +add_input(WINDOW *win, struct input *a, int key); + +int +del_input(WINDOW *win, struct input *a); + +int +clear_input(WINDOW *win, struct input *a); + +void +register_input(WINDOW *win, struct input *a, uicb_input ipcb); + +void +unregister_input(struct input *a); + +#endif diff --git a/src/ui_ipc.c b/src/ui_ipc.c new file mode 100644 index 0000000..9eac79e --- /dev/null +++ b/src/ui_ipc.c @@ -0,0 +1,130 @@ +#include +#include +#include +#ifdef SEM_TIMEDWAIT +#include +#endif +#include +#include +#include +#include + +#include "ui_ipc.h" + +#define JMP_IF(cmd, retval, jmplabel) if ( (cmd) == retval ) { printf("(%s) == %p\n", #cmd, (void*)retval); goto jmplabel; } + + +static sem_t *sems[SEM_NUM]; +static mqd_t msqs[MSQ_NUM]; + + +int +ui_ipc_init(int is_master) +{ + volatile int sp_oflags, mq_oflags; + mode_t crt_flags; + struct mq_attr m_attr; + + bzero(sems, sizeof(sem_t*)*SEM_NUM); + bzero(msqs, sizeof(mqd_t)*MSQ_NUM); + m_attr.mq_flags = 0; + m_attr.mq_msgsize = IPC_MQSIZ; + m_attr.mq_maxmsg = 3; + m_attr.mq_curmsgs = 0; + if (is_master) { + sp_oflags = O_CREAT | O_EXCL; + mq_oflags = O_NONBLOCK | O_CREAT | O_EXCL; + crt_flags = S_IRUSR | S_IWUSR; + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + } else { + sp_oflags = 0; + mq_oflags = 0; + crt_flags = 0; + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + } + JMP_IF( sems[SEM_UI] = sem_open(SEM_GUI, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + JMP_IF( sems[SEM_IN] = sem_open(SEM_INP, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + JMP_IF( sems[SEM_BS] = sem_open(SEM_BSY, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + JMP_IF( sems[SEM_RD] = sem_open(SEM_RDY, sp_oflags, crt_flags, 0), SEM_FAILED, error ); + return 0; +error: + return errno; +} + +void +ui_ipc_free(int is_master) +{ + int i; + + for (i = 0; i < SEM_NUM; i++) { + if (sems[i]) sem_close(sems[i]); + } + for (i = 0; i < MSQ_NUM; i++) { + if (msqs[i]) mq_close(msqs[i]); + } + if (is_master > 0) { + sem_unlink(SEM_BSY); + sem_unlink(SEM_GUI); + sem_unlink(SEM_INP); + sem_unlink(SEM_RDY); + mq_unlink(MSQ_PWD); + mq_unlink(MSQ_INF); + } +} + +int +ui_ipc_sempost(enum UI_IPC_SEM e_sp) +{ + return ( sem_post(sems[e_sp]) ); +} + +int +ui_ipc_semwait(enum UI_IPC_SEM e_sp) +{ + return ( sem_wait(sems[e_sp]) ); +} + +int +ui_ipc_semtrywait(enum UI_IPC_SEM e_sp) +{ + return ( sem_trywait(sems[e_sp]) ); +} + +int +ui_ipc_getvalue(enum UI_IPC_SEM e_sp) +{ + int sp_val = 0; + + if (sem_getvalue(sems[e_sp], &sp_val) != 0) { + return -1; + } + return sp_val; +} + +#ifdef SEM_TIMEDWAIT +int +ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, int timeout) +{ + struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { + return -1; + } + ts.tc_sec += timeout; + return ( sem_timedwait(sems[q_mq], &ts) ); +} +#endif + +int +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len) +{ + return ( mq_send(msqs[e_mq], msg_ptr, msg_len, 0) ); +} + +ssize_t +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len) +{ + return ( mq_receive(msqs[e_mq], msg_ptr, msg_len, NULL) ); +} + diff --git a/src/ui_ipc.h b/src/ui_ipc.h new file mode 100644 index 0000000..2c5bcb5 --- /dev/null +++ b/src/ui_ipc.h @@ -0,0 +1,52 @@ +#ifndef UI_IPC_H +#define UI_IPC_H 1 + +#include "status.h" +#include "config.h" + + +enum UI_IPC_SEM { + SEM_RD = 0, /* UI Init done? */ + SEM_UI, /* TUI active? */ + SEM_IN, /* Textfield has input avail */ + SEM_BS, /* Master process busy */ + SEM_NUM +}; + +enum UI_IPC_MSQ { + MQ_PW = 0, + MQ_IF, + MSQ_NUM +}; + + +int +ui_ipc_init(int is_master); + +void +ui_ipc_free(int is_master); + +int +ui_ipc_sempost(enum UI_IPC_SEM e_sp); + +int +ui_ipc_semwait(enum UI_IPC_SEM e_sp); + +int +ui_ipc_semtrywait(enum UI_IPC_SEM e_sp); + +int +ui_ipc_getvalue(enum UI_IPC_SEM e_sp); + +#ifdef SEM_TIMEDWAIT +int +ui_ipc_semtimedwait(enum UI_IPC_MSQ e_sp, int timeout); +#endif + +int +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len); + +ssize_t +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len); + +#endif diff --git a/src/ui_nwindow.c b/src/ui_nwindow.c new file mode 100644 index 0000000..aa1812d --- /dev/null +++ b/src/ui_nwindow.c @@ -0,0 +1,157 @@ +#include +#include + +#include "ui_nwindow.h" + + +struct txtwindow * +init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update) +{ + struct txtwindow *a = calloc(1, sizeof(struct txtwindow)); + + a->x = x; + a->y = y; + a->width = width; + a->height = height; + a->active = false; + a->title_len = INITIAL_TITLE_LEN; + a->title = calloc(a->title_len+1, sizeof(char)); + a->text = NULL; + a->attrs = attrs; + a->text_attrs = text_attrs; + a->window_func = cb_update; + return (a); +} + +static void +__free_text(struct txtwindow *a) +{ + if (a->text) { + if (a->text[0]) { + free(a->text[0]); + } + free(a->text); + a->text = NULL; + } +} + +void +free_txtwindow(struct txtwindow *a) +{ + __free_text(a); + if (a->title) { + free(a->title); + } + free(a); +} + +static void +print_wnd(struct txtwindow *a) +{ + int i, x = a->x, y = a->y, w = a->width, h = a->height; + char tmp[a->width+1]; + + attron(a->attrs); + /* print window surface */ + memset(tmp, ' ', a->width); + tmp[a->width] = '\0'; + for (i = y-1; i < y+h+2; i++) + mvprintw(i, x, tmp); + /* print window border */ + mvhline(y-2, x, 0, w); + mvhline(y+h+2, x, 0, w); + mvvline(y-1, x-1, 0, h+3); + mvvline(y-1, x+w, 0, h+3); + /* print window border edges */ + mvaddch(y-2, x-1, ACS_ULCORNER); + mvaddch(y+2+h, x-1, ACS_LLCORNER); + mvaddch(y-2, x+w, ACS_URCORNER); + mvaddch(y+2+h, x+w, ACS_LRCORNER); + /* print window title */ + attroff(a->attrs); + attron(a->text_attrs); + mvprintw(y-2, (float)x+(float)(w/2)-(float)(a->title_len*2/3), "[ %s ]", a->title); + /* print windows text */ + i = 0; + while ( a->text && a->text[i] ) { + mvprintw(y+i, x, a->text[i]); + i++; + } + attroff(a->text_attrs); +} + +static int +txtwindow_cb(WINDOW *win, void *data, bool timedout) +{ + struct txtwindow *a = (struct txtwindow *) data; + + if (a->active == true) { + print_wnd(a); + } + return (UICB_OK); +} + +void inline +register_txtwindow(struct txtwindow *a) +{ + struct ui_callbacks cbs; + cbs.ui_element = txtwindow_cb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); +} + +static size_t +__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src) +{ + size_t cursiz = sz_dest; + + if (sz_src > sz_dest) { + *p_dest = (char *) realloc(*p_dest, (sz_src+1) * sizeof(char)); + cursiz = sz_src; + } + memset(*p_dest, '\0', (cursiz+1) * sizeof(char)); + memcpy(*p_dest, p_src, (cursiz+1) * sizeof(char)); + return sz_src; +} + +/* seperate a String with NEWLINES into an array */ +static char ** +__do_textadjust(struct txtwindow *a, char *text) +{ + int i = 0, rows = (int)(strlen(text) / a->width)+1; + char **adj_text = calloc(rows+1, sizeof(char *)); + char *p_strtok, *tok; + const char sep[] = "\n"; + + if (rows > a->height) goto error; + p_strtok = strdup(text); + while ( (tok = strsep(&p_strtok, sep)) && rows-- >= 0 ) { + if (strlen(tok) > a->width) { + strcpy(tok+a->width-3, "..."); + *(tok+a->width) = '\0'; + } + adj_text[i] = tok; + i++; + } + return adj_text; +error: + free(adj_text); + return NULL; +} + +void +set_txtwindow_text(struct txtwindow *a, char *text) +{ + char **fmt_text = __do_textadjust(a, text); + + if (fmt_text) { + __free_text(a); + a->text = fmt_text; + } +} + +void +set_txtwindow_title(struct txtwindow *a, const char *title) +{ + a->title_len = __do_textcpy(&a->title, a->title_len, title, strlen(title)); +} diff --git a/src/ui_nwindow.h b/src/ui_nwindow.h new file mode 100644 index 0000000..198481b --- /dev/null +++ b/src/ui_nwindow.h @@ -0,0 +1,44 @@ +#ifndef UI_TXTWINDOW_H +#define UI_TXTWINDOW_H 1 + +#include + +#include "ui.h" + +#define INITIAL_TITLE_LEN 32 + +#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() + +struct txtwindow { + unsigned int y; + unsigned int x; + unsigned int width; + unsigned int height; + bool active; + char *title; + size_t title_len; + char **text; + int (*window_func)(WINDOW *, struct txtwindow *); + chtype attrs; + chtype text_attrs; + void *userptr; +}; + +typedef int (*window_func)(WINDOW *, struct txtwindow *); + +struct txtwindow * +init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update); + +void +free_txtwindow(struct txtwindow *a); + +void +register_txtwindow(struct txtwindow *a); + +void +set_txtwindow_text(struct txtwindow *a, char *text); + +void +set_txtwindow_title(struct txtwindow *a, const char *title); + +#endif diff --git a/src/ui_statusbar.c b/src/ui_statusbar.c new file mode 100644 index 0000000..8fcfeb4 --- /dev/null +++ b/src/ui_statusbar.c @@ -0,0 +1,78 @@ +#include +#include + +#include "ui.h" +#include "ui_statusbar.h" + + +struct statusbar * +init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update) +{ + struct statusbar *a = calloc(1, sizeof(struct statusbar)); + + a->y = y; + a->width = width; + a->text = calloc(a->width, sizeof(char)); + a->attrs = attrs; + a->status_func = cb_update; + return (a); +} + +void +free_statusbar(struct statusbar *a) +{ + if (a->text) { + free(a->text); + } + free(a); +} + +int +statusbar_cb(WINDOW *win, void *data, bool timed_out) +{ + struct statusbar *a = (struct statusbar *) data; + char *tmp; + unsigned int diff_pos = 0; + size_t len; + + if (a == NULL) return (UICB_ERR_UNDEF); + if (timed_out == true) { + if (a->status_func != NULL) { + a->status_func(win, a); + } + } + attron(a->attrs); + len = strnlen(a->text, a->width); + if (len < a->width) { + diff_pos = (unsigned int) (a->width - len)/2; + } + tmp = (char *) malloc(a->width + 1); + memset(tmp, ' ', a->width); + tmp[a->width] = '\0'; + strncpy((tmp + diff_pos), a->text, len); + if (win != NULL) { + mvwprintw(win, a->y, 0, tmp); + } else { + mvprintw(a->y, 0, tmp); + } + free(tmp); + attroff(a->attrs); + return (UICB_OK); +} + +void +register_statusbar(struct statusbar *a) +{ + struct ui_callbacks cbs; + cbs.ui_element = statusbar_cb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); +} + +inline void +set_statusbar_text(struct statusbar *a, const char *text) +{ + size_t len = strlen(text); + + strncpy(a->text, text, (len > a->width ? a->width : len)); +} diff --git a/src/ui_statusbar.h b/src/ui_statusbar.h new file mode 100644 index 0000000..5139c14 --- /dev/null +++ b/src/ui_statusbar.h @@ -0,0 +1,32 @@ +#ifndef UI_STATUSBAR_H +#define UI_STATUSBAR_H 1 + +#include + + +struct statusbar { + unsigned int y; + unsigned int width; + char *text; + int (*status_func)(WINDOW *, struct statusbar *); + chtype attrs; +}; + +typedef int (*status_func)(WINDOW *, struct statusbar *); + +struct statusbar * +init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update); + +void +free_statusbar(struct statusbar *a); + +int +statusbar_cb(WINDOW *win, void *data, bool timed_out); + +void +register_statusbar(struct statusbar *a); + +void +set_statusbar_text(struct statusbar *a, const char *text); + +#endif diff --git a/status.c b/status.c deleted file mode 100644 index 5286ddf..0000000 --- a/status.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include -#include -#include - -#include "status.h" - - -char * -get_system_stat(void) -{ - char *retstr = NULL; - int ncpu; - struct sysinfo inf; - - if (sysinfo(&inf) == EFAULT) { - return ("[SYSINFO ERROR]"); - } - ncpu = get_nprocs(); - - if (asprintf(&retstr, "u:%04ld - l:%3.2f,%3.2f,%3.2f - %dcore%s - mem:%lu/%lumb - procs:%02d", - inf.uptime, ((float)inf.loads[0]/10000), ((float)inf.loads[1]/10000), ((float)inf.loads[2]/10000), - ncpu, (ncpu > 1 ? "s" : ""), - (unsigned long)((inf.freeram/1024)/1024), (unsigned long)((inf.totalram/1024)/1024), inf.procs) == -1) { - return ("[ASPRINTF ERROR]"); - } - return (retstr); -} diff --git a/status.h b/status.h deleted file mode 100644 index 995d08a..0000000 --- a/status.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef STATUS_H -#define STATUS_H 1 - -char * -get_system_stat(void); - -#endif diff --git a/tests/Makefile b/tests/Makefile index 436d00f..20b86ba 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -8,7 +8,7 @@ DEPS = $(patsubst %.c,%.d,$(SOURCES)) all: $(BINARIES) semtest2: - $(CC) $(CFLAGS) $(LDFLAGS) ../ui_ipc.o semtest2.c -o semtest2 + $(CC) $(CFLAGS) $(LDFLAGS) -I ../src ../src/ui_ipc.o semtest2.c -o semtest2 %: %.c $(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<) diff --git a/tests/semtest2.c b/tests/semtest2.c index c015df8..1724cf1 100644 --- a/tests/semtest2.c +++ b/tests/semtest2.c @@ -7,7 +7,7 @@ #include #include -#include "../ui_ipc.h" +#include "ui_ipc.h" pid_t child; diff --git a/ui.c b/ui.c deleted file mode 100644 index 457d76d..0000000 --- a/ui.c +++ /dev/null @@ -1,268 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ui.h" -#include "ui_ipc.h" -#include "ui_elements.h" -#include "ui_ani.h" -#include "ui_input.h" -#include "ui_statusbar.h" -#include "ui_nwindow.h" - -#include "status.h" -#include "config.h" - -#define APP_TIMEOUT 60 -#define APP_TIMEOUT_FMT "%02d" -#define PASSWD_WIDTH 35 -#define PASSWD_HEIGHT 5 -#define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) -#define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 -#define INFOWND_WIDTH 25 -#define INFOWND_HEIGHT 3 -#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) -#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 - -#define STRLEN(s) (sizeof(s)/sizeof(s[0])) - - -static unsigned int max_x, max_y; -static WINDOW *wnd_main; -static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, - /* current active input */ *active = NULL; -static pthread_t thrd; -static unsigned int atmout = APP_TIMEOUT; -static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; -static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; - - -void -register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd) -{ - struct nask_ui *tmp, *new; - - new = calloc(1, sizeof(struct nask_ui)); - new->cbs = *cbs; - new->wnd = wnd; - new->data = data; - new->next = NULL; - if (nui == NULL) { - nui = new; - nui->next = NULL; - } else { - tmp = nui; - while (tmp->next != NULL) { - tmp = tmp->next; - } - tmp->next = new; - } -} - -void -unregister_ui_elt(void *data) -{ - struct nask_ui *cur, *next, *before = NULL; - - cur = nui; - while (cur != NULL) { - next = cur->next; - if (cur->data != NULL && cur->data == data) { - free(cur); - if (before != NULL) { - before->next = next; - } else { - nui = next; - } - } - before = cur; - cur = next; - } -} - -int -activate_ui_input(void *data) -{ - struct nask_ui *cur = nui; - - if (cur == NULL || data == NULL) return DOUI_NINIT; - while ( cur->data != NULL ) { - if ( cur->data == data ) { - if ( cur->cbs.ui_input != NULL && cur->cbs.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { - active = cur; - return DOUI_OK; - } - } - cur = cur->next; - } - return DOUI_ERR; -} - -static bool -process_key(char key) -{ - atmout = APP_TIMEOUT; - if ( active != NULL ) { - return ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); - } - return false; -} - -static int -do_ui_update(bool timed_out) -{ - int retval = UICB_OK; - int curx = getcurx(wnd_main); - int cury = getcury(wnd_main); - struct nask_ui *cur = nui; - - /* call all draw callback's */ - erase(); - while (cur != NULL) { - if (cur->cbs.ui_element != NULL) { - cur->cbs.ui_element(cur->wnd, cur->data, timed_out); - doupdate(); - } else { - retval = UICB_ERR_CB; - } - cur = cur->next; - } - /* TODO: Maybe export to an extra module? */ - attron(COLOR_PAIR(1)); - mvprintw(0, max_x - STRLEN(APP_TIMEOUT_FMT), "[" APP_TIMEOUT_FMT "]", atmout); - attroff(COLOR_PAIR(1)); - /* EoT (End of Todo) */ - wmove(wnd_main, cury, curx); - wrefresh(wnd_main); - return (retval); -} - -static void * -ui_thrd(void *arg) -{ - int cnd_ret; - struct timeval now; - struct timespec wait; - - gettimeofday(&now, NULL); - wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; - wait.tv_nsec = now.tv_usec * 1000; - do_ui_update(true); - ui_ipc_sempost(SEM_RD); - pthread_mutex_lock(&mtx_update); - while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - pthread_mutex_unlock(&mtx_busy); - cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - pthread_mutex_lock(&mtx_busy); - if (--atmout == 0) ui_ipc_semtrywait(SEM_UI); - do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); - if (cnd_ret == ETIMEDOUT) { - wait.tv_sec += UILOOP_TIMEOUT; - } - } - pthread_mutex_unlock(&mtx_update); - return (NULL); -} - -void -ui_thrd_force_update(void) -{ - pthread_mutex_lock(&mtx_update); - pthread_mutex_lock(&mtx_busy); - pthread_cond_signal(&cnd_update); - pthread_mutex_unlock(&mtx_busy); - pthread_mutex_unlock(&mtx_update); -} - -WINDOW * -init_ui(void) -{ - wnd_main = initscr(); - max_x = getmaxx(wnd_main); - max_y = getmaxy(wnd_main); - start_color(); - init_pair(1, COLOR_RED, COLOR_WHITE); - init_pair(2, COLOR_WHITE, COLOR_BLACK); - init_pair(3, COLOR_BLACK, COLOR_WHITE); - init_pair(4, COLOR_YELLOW, COLOR_RED); - raw(); - keypad(stdscr, TRUE); - noecho(); - cbreak(); - return (wnd_main); -} - -void -free_ui(void) -{ - delwin(wnd_main); - endwin(); - clear(); - printf(" \033[2J\n"); -} - -static int -run_ui_thrd(void) { - return (pthread_create(&thrd, NULL, &ui_thrd, NULL)); -} - -static int -stop_ui_thrd(void) -{ - return (pthread_join(thrd, NULL)); -} - -int -do_ui(void) -{ - char key = '\0'; - char *title = NULL; - int ret = DOUI_ERR; - - asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); - /* init TUI and UI Elements (input field, status bar, etc) */ - init_ui(); - init_ui_elements(wnd_main, max_x, max_y); - - pthread_mutex_lock(&mtx_update); - if (run_ui_thrd() != 0) { - goto error; - } - ui_ipc_semwait(SEM_RD); - wtimeout(wnd_main, 1000); - pthread_mutex_unlock(&mtx_update); - while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - if ((key = wgetch(wnd_main)) == '\0') { - break; - } - if (key == -1) { - continue; - } - if ( process_key(key) != true ) { -// break; - } -// do_ui_update(false); - } - stop_ui_thrd(); - free_ui_elements(); - - ret = DOUI_OK; -error: - if (title) free(title); - title = NULL; - return ret; -} - diff --git a/ui.h b/ui.h deleted file mode 100644 index 91d5403..0000000 --- a/ui.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef UI_H -#define UI_H 1 - -#include -#include - -#define UICB_OK 0 -#define UICB_ERR_UNDEF 1 -#define UICB_ERR_CB 2 -#define UICB_ERR_BUF 3 - -#define DOUI_OK 0 -#define DOUI_ERR 1 -#define DOUI_TMOUT 2 -#define DOUI_NINIT 3 - -#define UILOOP_TIMEOUT 1 - -#define UIKEY_ACTIVATE 0 -#define UIKEY_ENTER 10 -#define UIKEY_BACKSPACE 7 -#define UIKEY_ESC 27 -#define UIKEY_DOWN 2 -#define UIKEY_UP 3 -#define UIKEY_LEFT 4 -#define UIKEY_RIGHT 5 - - -typedef int (*uicb_base)(WINDOW *, void *, bool); -typedef int (*uicb_input)(WINDOW *, void *, int); - - -struct ui_callbacks { - uicb_base ui_element; - uicb_input ui_input; -}; - -struct nask_ui { - struct ui_callbacks cbs; - WINDOW *wnd; - void *data; - struct nask_ui *next; -}; - -void -register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd); - -void -unregister_ui_elt(void *data); - -int -activate_ui_input(void *data); - -void -ui_thrd_force_update(void); - -WINDOW * -init_ui(void); - -void -free_ui(void); - -int -do_ui(void); - -#endif diff --git a/ui_ani.c b/ui_ani.c deleted file mode 100644 index c18ad4b..0000000 --- a/ui_ani.c +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include - -#include "ui.h" -#include "ui_ani.h" - -#define ANIC_INITSTATE '|' - - -struct anic * -init_anic(unsigned int x, unsigned int y, chtype attrs, char *fmt) -{ - struct anic *a = calloc(1, sizeof(struct anic)); - - a->x = x; - a->y = y; - a->state = ANIC_INITSTATE; - a->attrs = attrs; - if (fmt != NULL) { - a->fmt = strdup(fmt); - } - return (a); -} - -void -free_anic(struct anic *a) -{ - if (a->fmt != NULL) { - free(a->fmt); - } - free(a); -} - -int -anic_cb(WINDOW *win, void *data, bool timed_out) -{ - struct anic *a = (struct anic *) data; - char *tmp; - int retval = UICB_OK; - - if (a == NULL) return (UICB_ERR_UNDEF); - if (timed_out == true) { - switch (a->state) { - default: - case '|': a->state = '/'; break; - case '/': a->state = '-'; break; - case '-': a->state = '\\'; break; - case '\\': a->state = '|'; break; - } - } - attron(a->attrs); - if (a->fmt != NULL) { - if (asprintf(&tmp, a->fmt, a->state) <= 0) { - retval = UICB_ERR_BUF; - } - } else { - asprintf(&tmp, "%c", a->state); - } - if (win != NULL) { - mvwprintw(win, a->y, a->x, tmp); - } else { - mvprintw(a->y, a->x, tmp); - } - free(tmp); - attroff(a->attrs); - return (retval); -} - -void -register_anic(struct anic *a) -{ - struct ui_callbacks cbs; - cbs.ui_element = anic_cb; - cbs.ui_input = NULL; - register_ui_elt(&cbs, (void *) a, NULL); -} diff --git a/ui_ani.h b/ui_ani.h deleted file mode 100644 index 15962ae..0000000 --- a/ui_ani.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef UI_ANIC_H -#define UI_ANIC_H 1 - -#include - - -struct anic { - unsigned int x; - unsigned int y; - char state; - char *fmt; - chtype attrs; -}; - -struct anic * -init_anic(unsigned int x, unsigned int y, chtype attrs, char *fmt); - -void -free_anic(struct anic *a); - -int -anic_cb(WINDOW *win, void *data, bool timed_out); - -void -register_anic(struct anic *a); - -#endif diff --git a/ui_elements.c b/ui_elements.c deleted file mode 100644 index 0633b42..0000000 --- a/ui_elements.c +++ /dev/null @@ -1,151 +0,0 @@ -#include -#include -#include - -#include "ui.h" -#include "ui_ipc.h" -#include "ui_ani.h" -#include "ui_input.h" -#include "ui_statusbar.h" -#include "ui_nwindow.h" -#include "ui_elements.h" - -#include "status.h" - -#define PASSWD_WIDTH 35 -#define PASSWD_HEIGHT 5 -#define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) -#define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 -#define INFOWND_WIDTH 25 -#define INFOWND_HEIGHT 3 -#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) -#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 - -static struct input *pw_input; -static struct anic *heartbeat; -static struct statusbar *higher, *lower; -static struct txtwindow *infownd; -static char *title = NULL; - - -static int -lower_statusbar_update(WINDOW *win, struct statusbar *bar) -{ - char *tmp = get_system_stat(); - set_statusbar_text(bar, tmp); - free(tmp); - return 0; -} - -static int -higher_statusbar_update(WINDOW *win, struct statusbar *bar) -{ - return 0; -} - -static int -infownd_update(WINDOW *win, struct txtwindow *tw) -{ - char *tmp = (char*)(tw->userptr); - size_t len = strlen(tmp); - - if (tw->active) { - if ( len == 3 ) { - memset(tmp+1, '\0', 2); - } else strcat(tmp, "."); - } else (*tmp) = '.'; - return 0; -} - -static int -mq_passwd_send(char *passwd, size_t len) -{ - int ret; - - ui_ipc_sempost(SEM_IN); - ret = ui_ipc_msgsend(MQ_PW, passwd, len); - memset(passwd, '\0', len); - return ret; -} - -static int -passwd_input_cb(WINDOW *wnd, void *data, int key) -{ - struct input *a = (struct input *) data; -/* - * if ( process_key(key, pw_input, wnd_main) == false ) { - * curs_set(0); - * memset(mq_msg, '\0', IPC_MQSIZ+1); - * mq_receive(mq_info, mq_msg, IPC_MQSIZ+1, 0); - * set_txtwindow_text(infownd, mq_msg); - * set_txtwindow_active(infownd, true); - * sleep(3); - * sem_trywait(sp_ui); - * } - * activate_input(wnd_main, pw_input); - */ - switch (key) { - case UIKEY_ENTER: - mq_passwd_send(a->input, a->input_len); - memset(a->input, '\0', a->input_len); - a->input_len = 0; - a->input_pos = 0; - a->cur_pos = 0; - ui_thrd_force_update(); - break; - case UIKEY_BACKSPACE: - del_input(wnd, a); - break; - case UIKEY_ESC: - return DOUI_ERR; - break; - case UIKEY_DOWN: - case UIKEY_UP: - case UIKEY_LEFT: - case UIKEY_RIGHT: - break; - case UIKEY_ACTIVATE: - break; - default: - add_input(wnd, a, key); - } - return DOUI_OK; -} - -void -init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) -{ - asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); - pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); - heartbeat = init_anic(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); - higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), higher_statusbar_update); - lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update); - infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update); - infownd->userptr = calloc(4, sizeof(char)); - (*(char*)(infownd->userptr)) = '.'; - - register_input(NULL, pw_input, passwd_input_cb); - register_statusbar(higher); - register_statusbar(lower); - register_anic(heartbeat); - register_txtwindow(infownd); - set_txtwindow_title(infownd, "WARNING"); - activate_input(wnd_main, pw_input); - set_statusbar_text(higher, title); -} - -void -free_ui_elements(void) -{ - unregister_ui_elt(lower); - unregister_ui_elt(higher); - unregister_ui_elt(heartbeat); - unregister_ui_elt(pw_input); - free_input(pw_input); - free_anic(heartbeat); - free_statusbar(higher); - free_statusbar(lower); - free(infownd->userptr); - free_txtwindow(infownd); - free_ui(); -} diff --git a/ui_elements.h b/ui_elements.h deleted file mode 100644 index 0cf8826..0000000 --- a/ui_elements.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef UI_ELEMENTS_H -#define UI_ELEMENTS_H 1 - -#include "config.h" - - -void -init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y); - -void -free_ui_elements(void); - -#endif diff --git a/ui_input.c b/ui_input.c deleted file mode 100644 index 9b70f81..0000000 --- a/ui_input.c +++ /dev/null @@ -1,189 +0,0 @@ -#include -#include - -#include "ui.h" -#include "ui_input.h" - - -struct input * -init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs, chtype shadow) -{ - struct input *a = calloc(1, sizeof(struct input)); - - a->x = x; - a->y = y; - a->width = width; - a->cur_pos = 0; - a->input = calloc(input_len+1, sizeof(char)); - a->input_max = input_len; - a->input_len = 0; - a->input_pos = 0; - a->prompt = strdup(prompt); - a->attrs = attrs; - a->shadow = shadow; - return (a); -} - -void -free_input(struct input *a) -{ - if (a->input != NULL) { - free(a->input); - } - free(a->prompt); - free(a); -} - -static void -print_wnd(size_t addwidth, struct input *a) -{ - int i, x = a->x, y = a->y; - size_t relwidth = addwidth*2, len = strlen(a->prompt) + a->width; - char tmp[len+relwidth+1]; - - attron(a->attrs); - memset(tmp, ' ', len+relwidth); - tmp[len+relwidth] = '\0'; - for (i = -1; i <= 1; i++) - mvprintw(y+i, x-addwidth, tmp); - - mvhline(y-2, x-addwidth, 0, len+relwidth); - mvhline(y+2, x-addwidth, 0, len+relwidth); - mvvline(y-1, x-addwidth-1, 0, 3); - mvvline(y-1, x+len+addwidth, 0, 3); - mvaddch(y-2, x-addwidth-1, ACS_ULCORNER); - mvaddch(y+2, x-addwidth-1, ACS_LLCORNER); - mvaddch(y-2, x+len+addwidth, ACS_URCORNER); - mvaddch(y+2, x+len+addwidth, ACS_LRCORNER); - attroff(a->attrs); - - attron(a->shadow); - for (i = x-addwidth+1; i < x+len+relwidth; i++) - mvaddch(y+3, i, ACS_CKBOARD); - for (i = -1; i < 3; i++) { - mvaddch(y+i, x+len+relwidth-2, ACS_CKBOARD); - mvaddch(y+i, x+len+relwidth-1, ACS_CKBOARD); - } - attroff(a->shadow); -} - -static void -print_input_text(WINDOW *win, struct input *a) -{ - size_t start = 0; - size_t p_len = strlen(a->prompt); - - char tmp[a->width + 1]; - memset(tmp, '\0', a->width + 1); - if (a->input_pos >= a->width) { - start = a->input_pos - a->width; - } - - strncpy(tmp, (char *)(a->input + start), a->width); - int i; - for (i = 0; i < strlen(tmp); i++) { - tmp[i] = '*'; - } - - if (win == NULL) { - mvprintw(a->y, a->x + p_len, "%s", tmp); - } else { - mvwprintw(win, a->y, a->x + p_len, "%s", tmp); - } -} - -static void -print_input(WINDOW *win, struct input *a) -{ - char *tmp; - int i; - size_t p_len = strlen(a->prompt); - - print_wnd(3, a); - attron(a->attrs); - if (win) { - mvwprintw(win, a->y, a->x, a->prompt); - } else { - mvprintw(a->y, a->x, a->prompt); - } - tmp = calloc(a->width+1, sizeof(char)); - for (i = 0; i < a->width; i++) { - *(tmp + i) = '_'; - } - if (win) { - mvwprintw(win, a->y, a->x + p_len, tmp); - } else { - mvprintw(a->y, a->x + p_len, tmp); - } - free(tmp); - print_input_text(win, a); - attroff(a->attrs); -} - -int -activate_input(WINDOW *win, struct input *a) -{ - if (a == NULL) return (UICB_ERR_UNDEF); - size_t p_len = strlen(a->prompt); - if (win == NULL) { - move(a->y, a->x + p_len + a->cur_pos); - } else { - wmove(win, a->y, a->x + p_len + a->cur_pos); - } - return (activate_ui_input( (void *) a )); -} - -int -add_input(WINDOW *win, struct input *a, int key) -{ - if (a == NULL) return (UICB_ERR_UNDEF); - if (a->input_len >= a->input_max) return (UICB_ERR_BUF); - *(a->input + a->input_pos) = (char) key; - ++a->input_pos; - ++a->input_len; - a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); - print_input(win, a); - return (UICB_OK); -} - -int -del_input(WINDOW *win, struct input *a) -{ - if (a == NULL) return (UICB_ERR_UNDEF); - if (a->input_len == 0) return (UICB_ERR_BUF); - memmove((a->input + a->input_pos - 1), (a->input + a->input_pos), a->input_max - a->input_pos); - --a->input_len; - *(a->input + a->input_len) = '\0'; - if (a->input_pos-1 == a->input_len) { - --a->input_pos; - } - if (a->cur_pos+1 < a->width && a->cur_pos > 0) { - --a->cur_pos; - } else if (a->cur_pos-1 == a->input_pos) { - --a->cur_pos; - } - mvwprintw(win, a->y, a->x + a->cur_pos + strlen(a->prompt), "_"); - print_input(win, a); - return (UICB_OK); -} - -static int -input_cb(WINDOW *win, void *data, bool timed_out) -{ - struct input *a = (struct input *) data; - - if (a == NULL) return (UICB_ERR_UNDEF); - if (win != NULL && is_wintouched(win) == false) return (UICB_OK); - print_input(win, a); - return (UICB_OK); -} - -void -register_input(WINDOW *win, struct input *a, uicb_input ipcb) -{ - struct ui_callbacks cbs; - cbs.ui_element = input_cb; - cbs.ui_input = ipcb; - register_ui_elt(&cbs, (void *) a, win); -} - diff --git a/ui_input.h b/ui_input.h deleted file mode 100644 index 5407616..0000000 --- a/ui_input.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef UI_INPUT_H -#define UI_INPUT_H 1 - -#include - - -struct input { - unsigned int x; - unsigned int y; - unsigned int width; - unsigned int cur_pos; - char *input; - size_t input_max; - size_t input_len; - size_t input_pos; - char *prompt; - chtype attrs; - chtype shadow; - uicb_input cb_input; -}; - -struct input * -init_input(unsigned int x, unsigned int y, unsigned int width, char *prompt, size_t input_len, chtype attrs, chtype shadow); - -void -free_input(struct input *a); - -int -activate_input(WINDOW *win, struct input *a); - -int -add_input(WINDOW *win, struct input *a, int key); - -int -del_input(WINDOW *win, struct input *a); - -void -register_input(WINDOW *win, struct input *a, uicb_input ipcb); - -void -unregister_input(struct input *a); - -#endif diff --git a/ui_ipc.c b/ui_ipc.c deleted file mode 100644 index 60da7cb..0000000 --- a/ui_ipc.c +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include -#include -#ifdef SEM_TIMEDWAIT -#include -#endif -#include -#include -#include -#include - -#include "ui_ipc.h" - -#define JMP_IF(cmd, retval, jmplabel) if ( (cmd) == retval ) { printf("(%s) == %p\n", #cmd, (void*)retval); goto jmplabel; } - - -static sem_t *sems[SEM_NUM]; -static mqd_t msqs[MSQ_NUM]; -static unsigned char initialized = 0; - - -int -ui_ipc_init(int is_master) -{ - volatile int sp_oflags, mq_oflags; - mode_t crt_flags; - struct mq_attr m_attr; - - if (initialized) { - return -1; - } - bzero(sems, sizeof(sem_t*)*SEM_NUM); - bzero(msqs, sizeof(mqd_t)*MSQ_NUM); - m_attr.mq_flags = 0; - m_attr.mq_msgsize = IPC_MQSIZ; - m_attr.mq_maxmsg = 3; - m_attr.mq_curmsgs = 0; - if (is_master) { - sp_oflags = O_CREAT | O_EXCL; - mq_oflags = O_NONBLOCK | O_CREAT | O_EXCL; - crt_flags = S_IRUSR | S_IWUSR; - JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - } else { - sp_oflags = 0; - mq_oflags = 0; - crt_flags = 0; - JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - } - JMP_IF( sems[SEM_UI] = sem_open(SEM_GUI, sp_oflags, crt_flags, 0), SEM_FAILED, error ); - JMP_IF( sems[SEM_IN] = sem_open(SEM_INP, sp_oflags, crt_flags, 0), SEM_FAILED, error ); - JMP_IF( sems[SEM_BS] = sem_open(SEM_BSY, sp_oflags, crt_flags, 0), SEM_FAILED, error ); - JMP_IF( sems[SEM_RD] = sem_open(SEM_RDY, sp_oflags, crt_flags, 0), SEM_FAILED, error ); - initialized = 1; - return 0; -error: - return errno; -} - -void -ui_ipc_free(int is_master) -{ - int i; - - if (!initialized) { - return; - } - for (i = 0; i < SEM_NUM; i++) { - if (sems[i]) sem_close(sems[i]); - } - for (i = 0; i < MSQ_NUM; i++) { - if (msqs[i]) mq_close(msqs[i]); - } - if (is_master > 0) { - sem_unlink(SEM_BSY); - sem_unlink(SEM_GUI); - sem_unlink(SEM_INP); - sem_unlink(SEM_RDY); - mq_unlink(MSQ_PWD); - mq_unlink(MSQ_INF); - } - initialized = 0; -} - -int -ui_ipc_sempost(enum UI_IPC_SEM e_sp) -{ - return ( sem_post(sems[e_sp]) ); -} - -int -ui_ipc_semwait(enum UI_IPC_SEM e_sp) -{ - return ( sem_wait(sems[e_sp]) ); -} - -int -ui_ipc_semtrywait(enum UI_IPC_SEM e_sp) -{ - return ( sem_trywait(sems[e_sp]) ); -} - -int -ui_ipc_getvalue(enum UI_IPC_SEM e_sp) -{ - int sp_val = 0; - - if (sem_getvalue(sems[e_sp], &sp_val) != 0) { - return -1; - } - return sp_val; -} - -#ifdef SEM_TIMEDWAIT -int -ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, int timeout) -{ - struct timespec ts; - if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { - return -1; - } - ts.tc_sec += timeout; - return ( sem_timedwait(sems[q_mq], &ts) ); -} -#endif - -int -ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len) -{ - return ( mq_send(msqs[e_mq], msg_ptr, msg_len, 0) ); -} - -ssize_t -ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len) -{ - return ( mq_receive(msqs[e_mq], msg_ptr, msg_len, NULL) ); -} - diff --git a/ui_ipc.h b/ui_ipc.h deleted file mode 100644 index 2c5bcb5..0000000 --- a/ui_ipc.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef UI_IPC_H -#define UI_IPC_H 1 - -#include "status.h" -#include "config.h" - - -enum UI_IPC_SEM { - SEM_RD = 0, /* UI Init done? */ - SEM_UI, /* TUI active? */ - SEM_IN, /* Textfield has input avail */ - SEM_BS, /* Master process busy */ - SEM_NUM -}; - -enum UI_IPC_MSQ { - MQ_PW = 0, - MQ_IF, - MSQ_NUM -}; - - -int -ui_ipc_init(int is_master); - -void -ui_ipc_free(int is_master); - -int -ui_ipc_sempost(enum UI_IPC_SEM e_sp); - -int -ui_ipc_semwait(enum UI_IPC_SEM e_sp); - -int -ui_ipc_semtrywait(enum UI_IPC_SEM e_sp); - -int -ui_ipc_getvalue(enum UI_IPC_SEM e_sp); - -#ifdef SEM_TIMEDWAIT -int -ui_ipc_semtimedwait(enum UI_IPC_MSQ e_sp, int timeout); -#endif - -int -ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len); - -ssize_t -ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len); - -#endif diff --git a/ui_nwindow.c b/ui_nwindow.c deleted file mode 100644 index aa1812d..0000000 --- a/ui_nwindow.c +++ /dev/null @@ -1,157 +0,0 @@ -#include -#include - -#include "ui_nwindow.h" - - -struct txtwindow * -init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update) -{ - struct txtwindow *a = calloc(1, sizeof(struct txtwindow)); - - a->x = x; - a->y = y; - a->width = width; - a->height = height; - a->active = false; - a->title_len = INITIAL_TITLE_LEN; - a->title = calloc(a->title_len+1, sizeof(char)); - a->text = NULL; - a->attrs = attrs; - a->text_attrs = text_attrs; - a->window_func = cb_update; - return (a); -} - -static void -__free_text(struct txtwindow *a) -{ - if (a->text) { - if (a->text[0]) { - free(a->text[0]); - } - free(a->text); - a->text = NULL; - } -} - -void -free_txtwindow(struct txtwindow *a) -{ - __free_text(a); - if (a->title) { - free(a->title); - } - free(a); -} - -static void -print_wnd(struct txtwindow *a) -{ - int i, x = a->x, y = a->y, w = a->width, h = a->height; - char tmp[a->width+1]; - - attron(a->attrs); - /* print window surface */ - memset(tmp, ' ', a->width); - tmp[a->width] = '\0'; - for (i = y-1; i < y+h+2; i++) - mvprintw(i, x, tmp); - /* print window border */ - mvhline(y-2, x, 0, w); - mvhline(y+h+2, x, 0, w); - mvvline(y-1, x-1, 0, h+3); - mvvline(y-1, x+w, 0, h+3); - /* print window border edges */ - mvaddch(y-2, x-1, ACS_ULCORNER); - mvaddch(y+2+h, x-1, ACS_LLCORNER); - mvaddch(y-2, x+w, ACS_URCORNER); - mvaddch(y+2+h, x+w, ACS_LRCORNER); - /* print window title */ - attroff(a->attrs); - attron(a->text_attrs); - mvprintw(y-2, (float)x+(float)(w/2)-(float)(a->title_len*2/3), "[ %s ]", a->title); - /* print windows text */ - i = 0; - while ( a->text && a->text[i] ) { - mvprintw(y+i, x, a->text[i]); - i++; - } - attroff(a->text_attrs); -} - -static int -txtwindow_cb(WINDOW *win, void *data, bool timedout) -{ - struct txtwindow *a = (struct txtwindow *) data; - - if (a->active == true) { - print_wnd(a); - } - return (UICB_OK); -} - -void inline -register_txtwindow(struct txtwindow *a) -{ - struct ui_callbacks cbs; - cbs.ui_element = txtwindow_cb; - cbs.ui_input = NULL; - register_ui_elt(&cbs, (void *) a, NULL); -} - -static size_t -__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src) -{ - size_t cursiz = sz_dest; - - if (sz_src > sz_dest) { - *p_dest = (char *) realloc(*p_dest, (sz_src+1) * sizeof(char)); - cursiz = sz_src; - } - memset(*p_dest, '\0', (cursiz+1) * sizeof(char)); - memcpy(*p_dest, p_src, (cursiz+1) * sizeof(char)); - return sz_src; -} - -/* seperate a String with NEWLINES into an array */ -static char ** -__do_textadjust(struct txtwindow *a, char *text) -{ - int i = 0, rows = (int)(strlen(text) / a->width)+1; - char **adj_text = calloc(rows+1, sizeof(char *)); - char *p_strtok, *tok; - const char sep[] = "\n"; - - if (rows > a->height) goto error; - p_strtok = strdup(text); - while ( (tok = strsep(&p_strtok, sep)) && rows-- >= 0 ) { - if (strlen(tok) > a->width) { - strcpy(tok+a->width-3, "..."); - *(tok+a->width) = '\0'; - } - adj_text[i] = tok; - i++; - } - return adj_text; -error: - free(adj_text); - return NULL; -} - -void -set_txtwindow_text(struct txtwindow *a, char *text) -{ - char **fmt_text = __do_textadjust(a, text); - - if (fmt_text) { - __free_text(a); - a->text = fmt_text; - } -} - -void -set_txtwindow_title(struct txtwindow *a, const char *title) -{ - a->title_len = __do_textcpy(&a->title, a->title_len, title, strlen(title)); -} diff --git a/ui_nwindow.h b/ui_nwindow.h deleted file mode 100644 index 198481b..0000000 --- a/ui_nwindow.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef UI_TXTWINDOW_H -#define UI_TXTWINDOW_H 1 - -#include - -#include "ui.h" - -#define INITIAL_TITLE_LEN 32 - -#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() - -struct txtwindow { - unsigned int y; - unsigned int x; - unsigned int width; - unsigned int height; - bool active; - char *title; - size_t title_len; - char **text; - int (*window_func)(WINDOW *, struct txtwindow *); - chtype attrs; - chtype text_attrs; - void *userptr; -}; - -typedef int (*window_func)(WINDOW *, struct txtwindow *); - -struct txtwindow * -init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update); - -void -free_txtwindow(struct txtwindow *a); - -void -register_txtwindow(struct txtwindow *a); - -void -set_txtwindow_text(struct txtwindow *a, char *text); - -void -set_txtwindow_title(struct txtwindow *a, const char *title); - -#endif diff --git a/ui_statusbar.c b/ui_statusbar.c deleted file mode 100644 index 8fcfeb4..0000000 --- a/ui_statusbar.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include - -#include "ui.h" -#include "ui_statusbar.h" - - -struct statusbar * -init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update) -{ - struct statusbar *a = calloc(1, sizeof(struct statusbar)); - - a->y = y; - a->width = width; - a->text = calloc(a->width, sizeof(char)); - a->attrs = attrs; - a->status_func = cb_update; - return (a); -} - -void -free_statusbar(struct statusbar *a) -{ - if (a->text) { - free(a->text); - } - free(a); -} - -int -statusbar_cb(WINDOW *win, void *data, bool timed_out) -{ - struct statusbar *a = (struct statusbar *) data; - char *tmp; - unsigned int diff_pos = 0; - size_t len; - - if (a == NULL) return (UICB_ERR_UNDEF); - if (timed_out == true) { - if (a->status_func != NULL) { - a->status_func(win, a); - } - } - attron(a->attrs); - len = strnlen(a->text, a->width); - if (len < a->width) { - diff_pos = (unsigned int) (a->width - len)/2; - } - tmp = (char *) malloc(a->width + 1); - memset(tmp, ' ', a->width); - tmp[a->width] = '\0'; - strncpy((tmp + diff_pos), a->text, len); - if (win != NULL) { - mvwprintw(win, a->y, 0, tmp); - } else { - mvprintw(a->y, 0, tmp); - } - free(tmp); - attroff(a->attrs); - return (UICB_OK); -} - -void -register_statusbar(struct statusbar *a) -{ - struct ui_callbacks cbs; - cbs.ui_element = statusbar_cb; - cbs.ui_input = NULL; - register_ui_elt(&cbs, (void *) a, NULL); -} - -inline void -set_statusbar_text(struct statusbar *a, const char *text) -{ - size_t len = strlen(text); - - strncpy(a->text, text, (len > a->width ? a->width : len)); -} diff --git a/ui_statusbar.h b/ui_statusbar.h deleted file mode 100644 index 5139c14..0000000 --- a/ui_statusbar.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef UI_STATUSBAR_H -#define UI_STATUSBAR_H 1 - -#include - - -struct statusbar { - unsigned int y; - unsigned int width; - char *text; - int (*status_func)(WINDOW *, struct statusbar *); - chtype attrs; -}; - -typedef int (*status_func)(WINDOW *, struct statusbar *); - -struct statusbar * -init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update); - -void -free_statusbar(struct statusbar *a); - -int -statusbar_cb(WINDOW *win, void *data, bool timed_out); - -void -register_statusbar(struct statusbar *a); - -void -set_statusbar_text(struct statusbar *a, const char *text); - -#endif -- cgit v1.2.3 From 6f7ce363ee56f96fe797433ed48b18a0787ed110 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 14 Nov 2015 15:28:48 +0100 Subject: trying to fix ncurses problerms .. fixed --- src/ui.c | 35 +++++++++++++++++++++++------------ src/ui.h | 3 +++ src/ui_elements.c | 16 +++------------- src/ui_input.c | 11 ++++++++++- src/ui_input.h | 3 +++ 5 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/ui.c b/src/ui.c index 2fb5a1f..780452d 100644 --- a/src/ui.c +++ b/src/ui.c @@ -47,6 +47,7 @@ static pthread_t thrd; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t mtx_input = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; @@ -96,39 +97,49 @@ unregister_ui_elt(void *data) int activate_ui_input(void *data) { + int ret = DOUI_ERR; struct nask_ui *cur = nui; if (cur == NULL || data == NULL) return DOUI_NINIT; + pthread_mutex_lock(&mtx_input); while ( cur->data != NULL ) { if ( cur->data == data ) { if ( cur->cbs.ui_input != NULL && cur->cbs.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { active = cur; - return DOUI_OK; + ret = DOUI_OK; + break; } } cur = cur->next; } - return DOUI_ERR; + pthread_mutex_unlock(&mtx_input); + return ret; } int -reactivate_ui_input(void) +deactivate_ui_input(void *data) { - if (active) { - if (active->cbs.ui_element) { - } - return ( active->cbs.ui_input(active->wnd, active->data, UIKEY_ACTIVATE) ); - } else return DOUI_ERR; + int ret = DOUI_ERR; + + pthread_mutex_lock(&mtx_input); + if (active != NULL && data == active->data) { + active = NULL; + ret = DOUI_OK; + } + pthread_mutex_unlock(&mtx_input); + return ret; } static bool process_key(char key) { + bool ret = false; + atmout = APP_TIMEOUT; if ( active != NULL ) { - return ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); + ret = ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); } - return false; + return ret; } static int @@ -174,11 +185,11 @@ ui_thrd(void *arg) ui_ipc_sempost(SEM_RD); pthread_mutex_lock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - pthread_mutex_unlock(&mtx_busy); cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - pthread_mutex_lock(&mtx_busy); if (--atmout == 0) ui_ipc_semtrywait(SEM_UI); + pthread_mutex_lock(&mtx_busy); do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); + pthread_mutex_unlock(&mtx_busy); if (cnd_ret == ETIMEDOUT) { wait.tv_sec += UILOOP_TIMEOUT; } diff --git a/src/ui.h b/src/ui.h index 91d5403..fa895ca 100644 --- a/src/ui.h +++ b/src/ui.h @@ -51,6 +51,9 @@ unregister_ui_elt(void *data); int activate_ui_input(void *data); +int +deactivate_ui_input(void *data); + void ui_thrd_force_update(void); diff --git a/src/ui_elements.c b/src/ui_elements.c index 29c2e32..27ccea0 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -71,22 +71,13 @@ static int passwd_input_cb(WINDOW *wnd, void *data, int key) { struct input *a = (struct input *) data; -/* - * if ( process_key(key, pw_input, wnd_main) == false ) { - * curs_set(0); - * memset(mq_msg, '\0', IPC_MQSIZ+1); - * mq_receive(mq_info, mq_msg, IPC_MQSIZ+1, 0); - * set_txtwindow_text(infownd, mq_msg); - * set_txtwindow_active(infownd, true); - * sleep(3); - * sem_trywait(sp_ui); - * } - * activate_input(wnd_main, pw_input); - */ + switch (key) { case UIKEY_ENTER: + deactivate_input(pw_input); mq_passwd_send(a->input, a->input_len); clear_input(wnd, a); + set_txtwindow_title(infownd, "BUSY"); set_txtwindow_text(infownd, "BLA"); set_txtwindow_active(infownd, true); break; @@ -125,7 +116,6 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) register_statusbar(lower); register_anic_default(heartbeat); register_txtwindow(infownd); - set_txtwindow_title(infownd, "WARNING"); activate_input(wnd_main, pw_input); set_statusbar_text(higher, title); } diff --git a/src/ui_input.c b/src/ui_input.c index 5f9a679..3065f1d 100644 --- a/src/ui_input.c +++ b/src/ui_input.c @@ -125,6 +125,7 @@ activate_input(WINDOW *win, struct input *a) { if (a == NULL) return (UICB_ERR_UNDEF); size_t p_len = strlen(a->prompt); + curs_set(1); if (win == NULL) { move(a->y, a->x + p_len + a->cur_pos); } else { @@ -133,6 +134,13 @@ activate_input(WINDOW *win, struct input *a) return (activate_ui_input( (void *) a )); } +int +deactivate_input(struct input *a) +{ + curs_set(0); + return (deactivate_ui_input(a)); +} + int add_input(WINDOW *win, struct input *a, int key) { @@ -142,7 +150,8 @@ add_input(WINDOW *win, struct input *a, int key) ++a->input_pos; ++a->input_len; a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); - print_input(win, a); + //print_input(win, a); + ui_thrd_force_update(); return (UICB_OK); } diff --git a/src/ui_input.h b/src/ui_input.h index 657c926..8ed7fc3 100644 --- a/src/ui_input.h +++ b/src/ui_input.h @@ -28,6 +28,9 @@ free_input(struct input *a); int activate_input(WINDOW *win, struct input *a); +int +deactivate_input(struct input *a); + int add_input(WINDOW *win, struct input *a, int key); -- cgit v1.2.3 From 7bceed7feae9e9abd58cc653596f6a3678b0ce37 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 14 Nov 2015 22:09:53 +0100 Subject: ui thread does now the cursor mgmt --- src/ui.c | 26 +++++++++++++++++++++++--- src/ui.h | 9 +++++++++ src/ui_input.c | 15 ++++++--------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/ui.c b/src/ui.c index 780452d..bb12936 100644 --- a/src/ui.c +++ b/src/ui.c @@ -40,6 +40,7 @@ static unsigned int max_x, max_y; +static unsigned int cur_x, cur_y; static WINDOW *wnd_main; static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, /* current active input */ *active = NULL; @@ -94,6 +95,25 @@ unregister_ui_elt(void *data) } } +void +ui_set_cur(unsigned int x, unsigned int y) +{ + cur_x = x; + cur_y = y; +} + +int +ui_get_curx(void) +{ + return (cur_x); +} + +int +ui_get_cury(void) +{ + return (cur_y); +} + int activate_ui_input(void *data) { @@ -146,8 +166,6 @@ static int do_ui_update(bool timed_out) { int retval = UICB_OK; - int curx = getcurx(wnd_main); - int cury = getcury(wnd_main); struct nask_ui *cur = nui; /* call all draw callback's */ @@ -166,7 +184,7 @@ do_ui_update(bool timed_out) mvprintw(0, max_x - STRLEN(APP_TIMEOUT_FMT), "[" APP_TIMEOUT_FMT "]", atmout); attroff(COLOR_PAIR(1)); /* EoT (End of Todo) */ - wmove(wnd_main, cury, curx); + wmove(wnd_main, cur_y, cur_x); wrefresh(wnd_main); return (retval); } @@ -212,6 +230,8 @@ init_ui(void) wnd_main = initscr(); max_x = getmaxx(wnd_main); max_y = getmaxy(wnd_main); + cur_x = getcurx(wnd_main); + cur_y = getcury(wnd_main); start_color(); init_pair(1, COLOR_RED, COLOR_WHITE); init_pair(2, COLOR_WHITE, COLOR_BLACK); diff --git a/src/ui.h b/src/ui.h index fa895ca..68f9fcd 100644 --- a/src/ui.h +++ b/src/ui.h @@ -48,6 +48,15 @@ register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd); void unregister_ui_elt(void *data); +void +ui_set_cur(unsigned int x, unsigned int y); + +int +ui_get_curx(void); + +int +ui_get_cury(void); + int activate_ui_input(void *data); diff --git a/src/ui_input.c b/src/ui_input.c index 3065f1d..b32570b 100644 --- a/src/ui_input.c +++ b/src/ui_input.c @@ -124,13 +124,8 @@ int activate_input(WINDOW *win, struct input *a) { if (a == NULL) return (UICB_ERR_UNDEF); - size_t p_len = strlen(a->prompt); curs_set(1); - if (win == NULL) { - move(a->y, a->x + p_len + a->cur_pos); - } else { - wmove(win, a->y, a->x + p_len + a->cur_pos); - } + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); return (activate_ui_input( (void *) a )); } @@ -150,7 +145,7 @@ add_input(WINDOW *win, struct input *a, int key) ++a->input_pos; ++a->input_len; a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); - //print_input(win, a); + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); ui_thrd_force_update(); return (UICB_OK); } @@ -172,7 +167,8 @@ del_input(WINDOW *win, struct input *a) --a->cur_pos; } mvwprintw(win, a->y, a->x + a->cur_pos + strlen(a->prompt), "_"); - print_input(win, a); + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); + ui_thrd_force_update(); return (UICB_OK); } @@ -184,7 +180,8 @@ clear_input(WINDOW *win, struct input *a) a->input_len = 0; a->input_pos = 0; a->cur_pos = 0; - print_input(win, a); + ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); + ui_thrd_force_update(); return (UICB_OK); } -- cgit v1.2.3 From 7c3c6b99bec920ea850739e62b9fec8a29dc0dc0 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 14 Nov 2015 22:53:40 +0100 Subject: ~auto(conf|make)~ --- Makefile | 53 ----------------------------------- Makefile.am | 11 ++++++++ Makefile.debug | 53 +++++++++++++++++++++++++++++++++++ autogen.sh | 15 ++++++++++ configure.ac | 68 ++++++++++++++++++++++++++++++++++++++++++++ m4/ax_check_typedef.m4 | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 3 ++ src/ui.c | 1 - 8 files changed, 226 insertions(+), 54 deletions(-) delete mode 100644 Makefile create mode 100644 Makefile.am create mode 100644 Makefile.debug create mode 100755 autogen.sh create mode 100644 configure.ac create mode 100644 m4/ax_check_typedef.m4 create mode 100644 src/Makefile.am diff --git a/Makefile b/Makefile deleted file mode 100644 index 9efaec9..0000000 --- a/Makefile +++ /dev/null @@ -1,53 +0,0 @@ -CFLAGS = $(shell ncurses5-config --cflags) -Wall -Wundef -Wshadow -D_GNU_SOURCE=1 -fPIC -fomit-frame-pointer -fno-inline -fstrength-reduce -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -fstrict-aliasing -Os -MD -MP -DBGFLAGS = -g -LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt -CC = gcc -INSTALL = install -STRIP = strip -VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) -BIN = naskpass -SOURCES = $(wildcard src/*.c) -OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) -DEPS = $(patsubst %.c,%.d,$(SOURCES)) - -all: $(OBJECTS) $(BIN) - -%.o: %.c - $(CC) $(CFLAGS) -D_VERSION=\"$(VERSION)\" -c $< -o $@ - -$(BIN): $(SOURCES) - $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) - $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all - -strip: $(OBJECTS) $(BIN) - $(STRIP) $(BIN) - -release: all strip - -debug: - @$(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' - @$(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' - -install: - $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass - $(INSTALL) -D -m 0755 scripts/naskpass.inithook $(DESTDIR)/usr/share/naskpass/naskpass.hook.initramfs - $(INSTALL) -D -m 0755 scripts/naskpass.initscript $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs - $(INSTALL) -D -m 0755 scripts/naskconf $(DESTDIR)/usr/share/naskpass/naskconf - -uninstall: - rm -f $(DESTDIR)/lib/cryptsetup/naskpass - rm -f $(DESTDIR)/usr/share/initramfs-tools/hooks/naskpass - rm -f $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs - rm -f $(DESTDIR)/usr/share/naskpass/naskconf - rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/naskpass - -clean: - rm -f $(DEPS) - rm -f $(OBJECTS) - rm -f $(BIN) - $(MAKE) -C tests clean - -source: - -dh_make --createorig -p naskpass_$(VERSION) -s -y - -.PHONY: all install clean diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..d682608 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,11 @@ +SUBDIRS = src + + +clean-local: + rm -f naskpass + +distclean-local: clean-local + rm -f aclocal.m4 config.log config.status configure + rm -f Makefile.in src/Makefile.in src/aconfig.h.in src/aconfig.h.in~ src/*.d + rm -rf build autom4te.cache + diff --git a/Makefile.debug b/Makefile.debug new file mode 100644 index 0000000..9efaec9 --- /dev/null +++ b/Makefile.debug @@ -0,0 +1,53 @@ +CFLAGS = $(shell ncurses5-config --cflags) -Wall -Wundef -Wshadow -D_GNU_SOURCE=1 -fPIC -fomit-frame-pointer -fno-inline -fstrength-reduce -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -fstrict-aliasing -Os -MD -MP +DBGFLAGS = -g +LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt +CC = gcc +INSTALL = install +STRIP = strip +VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) +BIN = naskpass +SOURCES = $(wildcard src/*.c) +OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) +DEPS = $(patsubst %.c,%.d,$(SOURCES)) + +all: $(OBJECTS) $(BIN) + +%.o: %.c + $(CC) $(CFLAGS) -D_VERSION=\"$(VERSION)\" -c $< -o $@ + +$(BIN): $(SOURCES) + $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) + $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all + +strip: $(OBJECTS) $(BIN) + $(STRIP) $(BIN) + +release: all strip + +debug: + @$(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' + @$(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' + +install: + $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass + $(INSTALL) -D -m 0755 scripts/naskpass.inithook $(DESTDIR)/usr/share/naskpass/naskpass.hook.initramfs + $(INSTALL) -D -m 0755 scripts/naskpass.initscript $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs + $(INSTALL) -D -m 0755 scripts/naskconf $(DESTDIR)/usr/share/naskpass/naskconf + +uninstall: + rm -f $(DESTDIR)/lib/cryptsetup/naskpass + rm -f $(DESTDIR)/usr/share/initramfs-tools/hooks/naskpass + rm -f $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs + rm -f $(DESTDIR)/usr/share/naskpass/naskconf + rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/naskpass + +clean: + rm -f $(DEPS) + rm -f $(OBJECTS) + rm -f $(BIN) + $(MAKE) -C tests clean + +source: + -dh_make --createorig -p naskpass_$(VERSION) -s -y + +.PHONY: all install clean diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..96fb4e9 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +rm -f config.cache +mkdir -p build + +echo "Looking in current directory for macros." +aclocal -I . +echo "Adding missing files." +automake --add-missing --force-missing +echo "Autoconf, Autoheader, Automake" +autoconf +autoheader +automake --foreign --add-missing --force-missing --copy +exit $? + diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..3786c0e --- /dev/null +++ b/configure.ac @@ -0,0 +1,68 @@ +# -*- Autoconf -*- +# configure script generation for ncblog +# + +AC_PREREQ([2.67]) +AC_INIT([naskpass], [0.01], [matzeton@googlemail.com]) +AC_CONFIG_AUX_DIR([build]) +AM_INIT_AUTOMAKE([1.11 foreign no-define -Wall -Werror]) +AM_WITH_DMALLOC +AC_CANONICAL_BUILD +AC_CANONICAL_HOST +AC_CONFIG_SRCDIR([src/aconfig.h.in]) +AC_CONFIG_HEADER([src/aconfig.h]) +AC_CONFIG_MACRO_DIR([m4]) +AC_USE_SYSTEM_EXTENSIONS +CFLAGS="-Os" +LDFLAGS="" + +# Checks for programs. +AM_PROG_AR +AM_PROG_INSTALL_STRIP +AC_PROG_CC +AC_PROG_INSTALL +AC_PROG_RANLIB +AC_C_INLINE +AC_PREFIX_DEFAULT([/usr]) +AC_CHECK_TOOL([STRIP],[strip]) + +# Checks for header files. +AC_HEADER_SYS_WAIT +AC_HEADER_TIME +AC_HEADER_STDBOOL +AC_HEADER_STDC +AC_HEADER_STAT +AC_HEADER_DIRENT +AC_HEADER_ASSERT +AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_COMPUTE_INT +AC_TYPE_UID_T +AC_TYPE_MODE_T +AC_TYPE_PID_T +AC_TYPE_SIZE_T +AC_TYPE_SSIZE_T +AC_TYPE_UINT16_T +AC_TYPE_UINT8_T +AX_CHECK_TYPEDEF([size_t], [stdio.h],,[AC_MSG_ERROR([*** Missing size_t typedef in stdio.h])]) +AX_CHECK_TYPEDEF([ssize_t], [stdio.h],,[AC_MSG_ERROR([*** Missing size_t typedef in stdio.h])]) + +# Checks for library functions. +AC_FUNC_FORK +AC_FUNC_MALLOC +AC_FUNC_MMAP +AC_FUNC_REALLOC +AC_FUNC_STRNLEN +AC_FUNC_STAT +AC_FUNC_MKTIME +AC_FUNC_VPRINTF +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork gettimeofday memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) + +AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) +LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" +AC_SUBST([AM_CFLAGS]) +AC_SUBST([AM_LDFLAGS]) +AC_CONFIG_FILES([Makefile src/Makefile]) +AC_OUTPUT + diff --git a/m4/ax_check_typedef.m4 b/m4/ax_check_typedef.m4 new file mode 100644 index 0000000..13b9d5e --- /dev/null +++ b/m4/ax_check_typedef.m4 @@ -0,0 +1,76 @@ +# =========================================================================== +# http://www.gnu.org/software/autoconf-archive/ax_check_typedef.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_CHECK_TYPEDEF(TYPEDEF, HEADER [, ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]) +# +# DESCRIPTION +# +# Check if the given typedef-name is recognized as a type. The trick is to +# use a sizeof(TYPEDEF) and see if the compiler is happy with that. +# +# This can be thought of as a mixture of AC_CHECK_TYPE(TYPEDEF,DEFAULT) +# and AC_CHECK_LIB(LIBRARY,FUNCTION,ACTION-IF-FOUND,ACTION-IF-NOT-FOUND). +# +# A convenience macro AX_CHECK_TYPEDEF_ is provided that will not emit any +# message to the user - it just executes one of the actions. +# +# LICENSE +# +# Copyright (c) 2008 Guido U. Draheim +# +# 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 . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 5 + +AU_ALIAS([AC_CHECK_TYPEDEF], [AX_CHECK_TYPEDEF]) +AC_DEFUN([AX_CHECK_TYPEDEF_], +[dnl +ac_lib_var=`echo $1['_']$2 | sed 'y%./+-%__p_%'` +AC_CACHE_VAL(ac_cv_lib_$ac_lib_var, +[ eval "ac_cv_type_$ac_lib_var='not-found'" + ac_cv_check_typedef_header=`echo ifelse([$2], , stddef.h, $2)` + AC_TRY_COMPILE( [#include <$ac_cv_check_typedef_header>], + [int x = sizeof($1); x = x;], + eval "ac_cv_type_$ac_lib_var=yes" , + eval "ac_cv_type_$ac_lib_var=no" ) + if test `eval echo '$ac_cv_type_'$ac_lib_var` = "no" ; then + ifelse([$4], , :, $4) + else + ifelse([$3], , :, $3) + fi +])]) + +dnl AX_CHECK_TYPEDEF(TYPEDEF, HEADER [, ACTION-IF-FOUND, +dnl [, ACTION-IF-NOT-FOUND ]]) +AC_DEFUN([AX_CHECK_TYPEDEF], +[dnl + AC_MSG_CHECKING([for $1 in $2]) + AX_CHECK_TYPEDEF_($1,$2,AC_MSG_RESULT(yes),AC_MSG_RESULT(no))dnl +]) diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..07b4cfa --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,3 @@ +bin_PROGRAMS=naskpass +naskpass_SOURCES=main.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_nwindow.c ui_statusbar.c + diff --git a/src/ui.c b/src/ui.c index bb12936..8b3d49f 100644 --- a/src/ui.c +++ b/src/ui.c @@ -12,7 +12,6 @@ #include #include #include -#include #include "ui.h" #include "ui_ipc.h" -- cgit v1.2.3 From dcf8b9b6981d0d21db6edb3265b56c55b39e4ff4 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 01:18:11 +0100 Subject: waitpid(...) checks now for WIFEXITED instead of WIFSIGNALED --- Makefile.am | 2 +- configure.ac | 2 +- src/aconfig.h.in | 299 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 2 - src/main.c | 13 +-- src/ui.c | 2 +- src/ui_elements.c | 11 +- src/ui_ipc.c | 30 ++++-- src/ui_ipc.h | 9 +- tests/mqtest.c | 23 ++++- 10 files changed, 363 insertions(+), 30 deletions(-) create mode 100644 src/aconfig.h.in diff --git a/Makefile.am b/Makefile.am index d682608..1d76541 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,6 +6,6 @@ clean-local: distclean-local: clean-local rm -f aclocal.m4 config.log config.status configure - rm -f Makefile.in src/Makefile.in src/aconfig.h.in src/aconfig.h.in~ src/*.d + rm -f Makefile.in src/Makefile.in src/aconfig.h.in~ src/*.d rm -rf build autom4te.cache diff --git a/configure.ac b/configure.ac index 3786c0e..943f5cf 100644 --- a/configure.ac +++ b/configure.ac @@ -34,7 +34,7 @@ AC_HEADER_STDC AC_HEADER_STAT AC_HEADER_DIRENT AC_HEADER_ASSERT -AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h]) +AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h alloca.h]) # Checks for typedefs, structures, and compiler characteristics. AC_COMPUTE_INT diff --git a/src/aconfig.h.in b/src/aconfig.h.in new file mode 100644 index 0000000..f690396 --- /dev/null +++ b/src/aconfig.h.in @@ -0,0 +1,299 @@ +/* src/aconfig.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the `alarm' function. */ +#undef HAVE_ALARM + +/* Define to 1 if you have the header file. */ +#undef HAVE_ALLOCA_H + +/* Define to 1 if you have the `asprintf' function. */ +#undef HAVE_ASPRINTF + +/* Define to 1 if you have the `clock_gettime' function. */ +#undef HAVE_CLOCK_GETTIME + +/* Define to 1 if you have the `close' function. */ +#undef HAVE_CLOSE + +/* Do NOT change THIS! */ +#undef HAVE_CONFIG + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_DIRENT_H + +/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */ +#undef HAVE_DOPRNT + +/* Define to 1 if you have the header file. */ +#undef HAVE_ERRNO_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_FCNTL_H + +/* Define to 1 if you have the `fork' function. */ +#undef HAVE_FORK + +/* Define to 1 if you have the `fprintf' function. */ +#undef HAVE_FPRINTF + +/* Define to 1 if you have the `getpagesize' function. */ +#undef HAVE_GETPAGESIZE + +/* Define to 1 if you have the `gettimeofday' function. */ +#undef HAVE_GETTIMEOFDAY + +/* Define to 1 if you have the header file. */ +#undef HAVE_INTTYPES_H + +/* Define to 1 if your system has a GNU libc compatible `malloc' function, and + to 0 otherwise. */ +#undef HAVE_MALLOC + +/* Define to 1 if you have the `memcpy' function. */ +#undef HAVE_MEMCPY + +/* Define to 1 if you have the `memmove' function. */ +#undef HAVE_MEMMOVE + +/* Define to 1 if you have the header file. */ +#undef HAVE_MEMORY_H + +/* Define to 1 if you have the `memset' function. */ +#undef HAVE_MEMSET + +/* Define to 1 if you have the `mkfifo' function. */ +#undef HAVE_MKFIFO + +/* Define to 1 if you have a working `mmap' system call. */ +#undef HAVE_MMAP + +/* Define to 1 if you have the header file. */ +#undef HAVE_MQUEUE_H + +/* Define to 1 if you have the header file, and it defines `DIR'. */ +#undef HAVE_NDIR_H + +/* Define to 1 if you have the `open' function. */ +#undef HAVE_OPEN + +/* Define to 1 if you have the `printf' function. */ +#undef HAVE_PRINTF + +/* Define to 1 if your system has a GNU libc compatible `realloc' function, + and to 0 otherwise. */ +#undef HAVE_REALLOC + +/* Define to 1 if you have the header file. */ +#undef HAVE_SEMAPHORE_H + +/* Define to 1 if you have the `stat' function. */ +#undef HAVE_STAT + +/* Define to 1 if `stat' has the bug that it succeeds when given the + zero-length file name argument. */ +#undef HAVE_STAT_EMPTY_STRING_BUG + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDBOOL_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDINT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDIO_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDLIB_H + +/* Define to 1 if you have the `strdup' function. */ +#undef HAVE_STRDUP + +/* Define to 1 if you have the `strerror' function. */ +#undef HAVE_STRERROR + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRINGS_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRING_H + +/* Define to 1 if you have the `strlen' function. */ +#undef HAVE_STRLEN + +/* Define to 1 if you have the `strndup' function. */ +#undef HAVE_STRNDUP + +/* Define to 1 if you have the `strnlen' function. */ +#undef HAVE_STRNLEN + +/* Define to 1 if you have the `strstr' function. */ +#undef HAVE_STRSTR + +/* Define to 1 if you have the `strtol' function. */ +#undef HAVE_STRTOL + +/* Define to 1 if you have the `system' function. */ +#undef HAVE_SYSTEM + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_SYS_DIR_H + +/* Define to 1 if you have the header file, and it defines `DIR'. + */ +#undef HAVE_SYS_NDIR_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_PARAM_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_STAT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TIME_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TYPES_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_WAIT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_TIME_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_UNISTD_H + +/* Define to 1 if you have the `vfork' function. */ +#undef HAVE_VFORK + +/* Define to 1 if you have the header file. */ +#undef HAVE_VFORK_H + +/* Define to 1 if you have the `vprintf' function. */ +#undef HAVE_VPRINTF + +/* Define to 1 if `fork' works. */ +#undef HAVE_WORKING_FORK + +/* Define to 1 if `vfork' works. */ +#undef HAVE_WORKING_VFORK + +/* Define to 1 if the system has the type `_Bool'. */ +#undef HAVE__BOOL + +/* Define to 1 if `lstat' dereferences a symlink specified with a trailing + slash. */ +#undef LSTAT_FOLLOWS_SLASHED_SYMLINK + +/* Define to 1 if assertions should be disabled. */ +#undef NDEBUG + +/* Define to the address where bug reports for this package should be sent. */ +#undef PACKAGE_BUGREPORT + +/* Define to the full name of this package. */ +#undef PACKAGE_NAME + +/* Define to the full name and version of this package. */ +#undef PACKAGE_STRING + +/* Define to the one symbol short name of this package. */ +#undef PACKAGE_TARNAME + +/* Define to the home page for this package. */ +#undef PACKAGE_URL + +/* Define to the version of this package. */ +#undef PACKAGE_VERSION + +/* Define to 1 if the `S_IS*' macros in do not work properly. */ +#undef STAT_MACROS_BROKEN + +/* Define to 1 if you have the ANSI C header files. */ +#undef STDC_HEADERS + +/* Define to 1 if you can safely include both and . */ +#undef TIME_WITH_SYS_TIME + +/* Enable extensions on AIX 3, Interix. */ +#ifndef _ALL_SOURCE +# undef _ALL_SOURCE +#endif +/* Enable GNU extensions on systems that have them. */ +#ifndef _GNU_SOURCE +# undef _GNU_SOURCE +#endif +/* Enable threading extensions on Solaris. */ +#ifndef _POSIX_PTHREAD_SEMANTICS +# undef _POSIX_PTHREAD_SEMANTICS +#endif +/* Enable extensions on HP NonStop. */ +#ifndef _TANDEM_SOURCE +# undef _TANDEM_SOURCE +#endif +/* Enable general extensions on Solaris. */ +#ifndef __EXTENSIONS__ +# undef __EXTENSIONS__ +#endif + + +/* Define if using the dmalloc debugging malloc package */ +#undef WITH_DMALLOC + +/* Define to 1 if on MINIX. */ +#undef _MINIX + +/* Define to 2 if the system does not provide POSIX.1 features except with + this defined. */ +#undef _POSIX_1_SOURCE + +/* Define to 1 if you need to in order for `stat' and other things to work. */ +#undef _POSIX_SOURCE + +/* Define for Solaris 2.5.1 so the uint8_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +#undef _UINT8_T + +/* Define to `int' if doesn't define. */ +#undef gid_t + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +#undef inline +#endif + +/* Define to rpl_malloc if the replacement function should be used. */ +#undef malloc + +/* Define to `int' if does not define. */ +#undef mode_t + +/* Define to `int' if does not define. */ +#undef pid_t + +/* Define to rpl_realloc if the replacement function should be used. */ +#undef realloc + +/* Define to `unsigned int' if does not define. */ +#undef size_t + +/* Define to `int' if does not define. */ +#undef ssize_t + +/* Define to `int' if doesn't define. */ +#undef uid_t + +/* Define to the type of an unsigned integer type of width exactly 16 bits if + such a type exists and the standard includes do not define it. */ +#undef uint16_t + +/* Define to the type of an unsigned integer type of width exactly 8 bits if + such a type exists and the standard includes do not define it. */ +#undef uint8_t + +/* Define as `fork' if `vfork' does not work. */ +#undef vfork diff --git a/src/config.h b/src/config.h index a7fddc0..9d72ef3 100644 --- a/src/config.h +++ b/src/config.h @@ -12,8 +12,6 @@ #define MSQ_PWD "/naskpass-passwd" #define MSQ_INF "/naskpass-info" -#define IPC_MQSIZ 128 - #ifdef _VERSION #define VERSION _VERSION #else diff --git a/src/main.c b/src/main.c index 5f0d6e9..8dbe953 100644 --- a/src/main.c +++ b/src/main.c @@ -142,21 +142,22 @@ main(int argc, char **argv) while ( ui_ipc_getvalue(SEM_UI) > 0 || ui_ipc_getvalue(SEM_IN) > 0 ) { ui_ipc_sempost(SEM_BS); if (read(ffd, pbuf, IPC_MQSIZ) >= 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD), strlen(MSG(MSG_BUSY_FD))); + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR), strlen(MSG(MSG_CRYPTCMD_ERR))); + ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); } - } else if ( ui_ipc_msgrecv(MQ_PW, pbuf, IPC_MQSIZ) != (int)-1 ) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY), strlen(MSG(MSG_BUSY))); + } else if ( ui_ipc_msgcount(MQ_PW) > 0 ) { + ui_ipc_msgrecv(MQ_PW, pbuf); + ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR), strlen(MSG(MSG_CRYPTCMD_ERR))); + ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); } ui_ipc_semwait(SEM_IN); } ui_ipc_semwait(SEM_BS); usleep(100000); waitpid(child, &c_status, WNOHANG); - if ( WIFSIGNALED(c_status) != 0 ) { + if ( WIFEXITED(c_status) != 0 ) { break; } } diff --git a/src/ui.c b/src/ui.c index 8b3d49f..5581bdc 100644 --- a/src/ui.c +++ b/src/ui.c @@ -203,7 +203,7 @@ ui_thrd(void *arg) pthread_mutex_lock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - if (--atmout == 0) ui_ipc_semtrywait(SEM_UI); + if (--atmout <= 0) ui_ipc_semtrywait(SEM_UI); pthread_mutex_lock(&mtx_busy); do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); pthread_mutex_unlock(&mtx_busy); diff --git a/src/ui_elements.c b/src/ui_elements.c index 27ccea0..b274529 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -58,12 +58,12 @@ infownd_update(WINDOW *win, struct txtwindow *tw) } static int -mq_passwd_send(char *passwd, size_t len) +mq_passwd_send(char *passwd) { int ret; ui_ipc_sempost(SEM_IN); - ret = ui_ipc_msgsend(MQ_PW, passwd, len); + ret = ui_ipc_msgsend(MQ_PW, passwd); return ret; } @@ -71,14 +71,17 @@ static int passwd_input_cb(WINDOW *wnd, void *data, int key) { struct input *a = (struct input *) data; + char ipc_buf[IPC_MQSIZ+1]; + memset(ipc_buf, '\0', IPC_MQSIZ+1); switch (key) { case UIKEY_ENTER: deactivate_input(pw_input); - mq_passwd_send(a->input, a->input_len); + mq_passwd_send(a->input); clear_input(wnd, a); + ui_ipc_msgrecv(MQ_IF, ipc_buf); set_txtwindow_title(infownd, "BUSY"); - set_txtwindow_text(infownd, "BLA"); + set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); break; case UIKEY_BACKSPACE: diff --git a/src/ui_ipc.c b/src/ui_ipc.c index 9eac79e..cf137e7 100644 --- a/src/ui_ipc.c +++ b/src/ui_ipc.c @@ -1,12 +1,13 @@ #include #include -#include +#include #ifdef SEM_TIMEDWAIT #include #endif #include #include #include +#include #include #include "ui_ipc.h" @@ -35,14 +36,14 @@ ui_ipc_init(int is_master) sp_oflags = O_CREAT | O_EXCL; mq_oflags = O_NONBLOCK | O_CREAT | O_EXCL; crt_flags = S_IRUSR | S_IWUSR; - JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); } else { sp_oflags = 0; mq_oflags = 0; crt_flags = 0; - JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); - JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_PW] = mq_open(MSQ_PWD, mq_oflags | O_WRONLY, crt_flags, &m_attr), (mqd_t)-1, error ); + JMP_IF( msqs[MQ_IF] = mq_open(MSQ_INF, mq_oflags | O_RDONLY, crt_flags, &m_attr), (mqd_t)-1, error ); } JMP_IF( sems[SEM_UI] = sem_open(SEM_GUI, sp_oflags, crt_flags, 0), SEM_FAILED, error ); JMP_IF( sems[SEM_IN] = sem_open(SEM_INP, sp_oflags, crt_flags, 0), SEM_FAILED, error ); @@ -117,14 +118,25 @@ ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, int timeout) #endif int -ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len) +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr) { - return ( mq_send(msqs[e_mq], msg_ptr, msg_len, 0) ); + char *tmp = alloca(IPC_MQSIZ); + memset(tmp, '\0', IPC_MQSIZ); + strncpy(tmp, msg_ptr, IPC_MQSIZ); + return ( mq_send(msqs[e_mq], tmp, IPC_MQSIZ, 0) ); } ssize_t -ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len) +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr) { - return ( mq_receive(msqs[e_mq], msg_ptr, msg_len, NULL) ); + return mq_receive(msqs[e_mq], msg_ptr, IPC_MQSIZ, NULL); } +long +ui_ipc_msgcount(enum UI_IPC_MSQ e_mq) +{ + struct mq_attr m_attr; + bzero(&m_attr, sizeof(struct mq_attr)); + if (mq_getattr(msqs[e_mq], &m_attr) != 0) return -1; + return m_attr.mq_curmsgs; +} diff --git a/src/ui_ipc.h b/src/ui_ipc.h index 2c5bcb5..ec36f35 100644 --- a/src/ui_ipc.h +++ b/src/ui_ipc.h @@ -4,6 +4,8 @@ #include "status.h" #include "config.h" +#define IPC_MQSIZ 128 + enum UI_IPC_SEM { SEM_RD = 0, /* UI Init done? */ @@ -44,9 +46,12 @@ ui_ipc_semtimedwait(enum UI_IPC_MSQ e_sp, int timeout); #endif int -ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr, size_t msg_len); +ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr); ssize_t -ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, size_t msg_len); +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr); + +long +ui_ipc_msgcount(enum UI_IPC_MSQ e_mq); #endif diff --git a/tests/mqtest.c b/tests/mqtest.c index e9bc8e7..f2808c8 100644 --- a/tests/mqtest.c +++ b/tests/mqtest.c @@ -1,18 +1,22 @@ #include #include +#include #include #include #include +#include #include #include static mqd_t mq_test; +static mqd_t mq_recv; static const size_t bufsiz = 256; int main(int argc, char **argv) { + int c_stat; struct mq_attr m_attr; char buf[bufsiz], recv[bufsiz]; unsigned int prio; @@ -31,10 +35,10 @@ int main(int argc, char **argv) mq_unlink("/testmq"); assert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); assert( mq_getattr(mq_test, &m_attr) == 0 ); - printf("flags.........: %ld\n" - "maxmsg........: %ld\n" - "msgsize.......: %ld\n" - "curmsg........: %ld\n", + printf("flags..........: %ld\n" + "maxmsg.........: %ld\n" + "msgsize........: %ld\n" + "curmsg.........: %ld\n", m_attr.mq_flags, m_attr.mq_maxmsg, m_attr.mq_msgsize, m_attr.mq_curmsgs); strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ"); @@ -44,6 +48,17 @@ int main(int argc, char **argv) printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf); printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv); + memset(recv, '\0', bufsiz); + if (fork() > 0) { + assert( (mq_recv = mq_open( "/testmq", O_RDONLY, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); + assert ( (sz_recv = mq_receive(mq_recv, recv, bufsiz, &prio)) > 0 ); + printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv); + return 0; + } + printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf); + assert ( mq_send(mq_test, buf, bufsiz, 0) == 0 ); + wait(&c_stat); + return 0; } -- cgit v1.2.3 From 7f30e0e93cf4ec24db8dd0e04adb8007bd7cffc3 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 02:06:18 +0100 Subject: fixed debian package build --- debian/rules | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/debian/rules b/debian/rules index 532ed81..048a2ae 100755 --- a/debian/rules +++ b/debian/rules @@ -4,11 +4,56 @@ #export DH_VERBOSE=1 DEBVERS := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p') -%: - dh $@ -override_dh_auto_build: - $(MAKE) VERSION="$(DEBVERS)" all +configure: configure-stamp +configure-stamp: + dh_testdir + ./autogen.sh + ./configure + touch configure-stamp + +build: configure-stamp build-stamp +build-stamp: + dh_testdir + $(MAKE) + touch build-stamp + +clean: + dh_testdir + dh_testroot + rm -f build-stamp configure-stamp + dh_clean + +distclean: build + $(MAKE) distclean + +install: build + dh_testdir + dh_testroot + dh_clean -k + dh_installdirs + $(MAKE) install prefix=$(CURDIR)/debian/naskpass + mkdir -p $(CURDIR)/debian/naskpass/usr/share/naskpass + install -D -m644 ./scripts/naskconf $(CURDIR)/debian/naskpass/usr/share/naskpass/ + install -D -m644 ./scripts/naskpass.inithook $(CURDIR)/debian/naskpass/usr/share/naskpass/ + install -D -m644 ./scripts/naskpass.initscript $(CURDIR)/debian/naskpass/usr/share/naskpass/ + +binary-indep: build install + +binary-arch: build install + dh_testdir + dh_testroot + dh_installdocs + dh_link + dh_strip + dh_compress + dh_fixperms + dh_installdeb + dh_shlibdeps + dh_gencontrol + dh_md5sums + dh_builddeb + +binary: binary-indep binary-arch +.PHONY: build clean distclean binary-indep binary-arch binary install configure -override_dh_auto_install: - $(MAKE) DESTDIR=$(CURDIR)/debian/naskpass install -- cgit v1.2.3 From 7bd71541d76cd5f23b91981f6d0a19d490926356 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 20:05:42 +0100 Subject: Makefile.debug is now DEBUG Makefile only! --- Makefile.debug | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Makefile.debug b/Makefile.debug index 9efaec9..66bf8e0 100644 --- a/Makefile.debug +++ b/Makefile.debug @@ -1,5 +1,4 @@ -CFLAGS = $(shell ncurses5-config --cflags) -Wall -Wundef -Wshadow -D_GNU_SOURCE=1 -fPIC -fomit-frame-pointer -fno-inline -fstrength-reduce -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -fstrict-aliasing -Os -MD -MP -DBGFLAGS = -g +CFLAGS = $(shell ncurses5-config --cflags) -Wall -Wundef -Wshadow -D_GNU_SOURCE=1 -fPIC -fomit-frame-pointer -fno-inline -fstrength-reduce -frerun-cse-after-loop -frerun-loop-opt -fexpensive-optimizations -fstrict-aliasing -Os -MD -MP -g LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt CC = gcc INSTALL = install @@ -25,8 +24,8 @@ strip: $(OBJECTS) $(BIN) release: all strip debug: - @$(MAKE) CFLAGS='$(CFLAGS) $(DBGFLAGS)' - @$(MAKE) -C tests CFLAGS='$(CFLAGS) $(DBGFLAGS)' + @$(MAKE) CFLAGS='$(CFLAGS) + @$(MAKE) -C tests CFLAGS='$(CFLAGS) install: $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass -- cgit v1.2.3 From 99dfc48c542ec59c9541faa33c8895bfa471089b Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 20:07:03 +0100 Subject: - ui_nwindow.c renamed to better ui_txtwindow.c - better ui_txtwindow.c --- src/main.c | 1 + src/ui.c | 21 ++++++- src/ui.h | 10 ++- src/ui_elements.c | 26 +++++--- src/ui_ipc.h | 2 +- src/ui_nwindow.c | 157 ----------------------------------------------- src/ui_nwindow.h | 44 -------------- src/ui_txtwindow.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui_txtwindow.h | 53 ++++++++++++++++ 9 files changed, 274 insertions(+), 215 deletions(-) delete mode 100644 src/ui_nwindow.c delete mode 100644 src/ui_nwindow.h create mode 100644 src/ui_txtwindow.c create mode 100644 src/ui_txtwindow.h diff --git a/src/main.c b/src/main.c index 8dbe953..0d0eae3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/src/ui.c b/src/ui.c index 5581bdc..fa65e29 100644 --- a/src/ui.c +++ b/src/ui.c @@ -19,7 +19,7 @@ #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" -#include "ui_nwindow.h" +#include "ui_txtwindow.h" #include "status.h" #include "config.h" @@ -94,6 +94,18 @@ unregister_ui_elt(void *data) } } +unsigned int +ui_get_maxx(void) +{ + return max_x; +} + +unsigned int +ui_get_maxy(void) +{ + return max_y; +} + void ui_set_cur(unsigned int x, unsigned int y) { @@ -101,13 +113,13 @@ ui_set_cur(unsigned int x, unsigned int y) cur_y = y; } -int +unsigned int ui_get_curx(void) { return (cur_x); } -int +unsigned int ui_get_cury(void) { return (cur_y); @@ -235,7 +247,10 @@ init_ui(void) init_pair(1, COLOR_RED, COLOR_WHITE); init_pair(2, COLOR_WHITE, COLOR_BLACK); init_pair(3, COLOR_BLACK, COLOR_WHITE); + /* TXTwindow */ init_pair(4, COLOR_YELLOW, COLOR_RED); + init_pair(5, COLOR_WHITE, COLOR_CYAN); + /* EoF TXTwindow */ raw(); keypad(stdscr, TRUE); noecho(); diff --git a/src/ui.h b/src/ui.h index 68f9fcd..6f83149 100644 --- a/src/ui.h +++ b/src/ui.h @@ -48,13 +48,19 @@ register_ui_elt(struct ui_callbacks *cbs, void *data, WINDOW *wnd); void unregister_ui_elt(void *data); +unsigned int +ui_get_maxx(void); + +unsigned int +ui_get_maxy(void); + void ui_set_cur(unsigned int x, unsigned int y); -int +unsigned int ui_get_curx(void); -int +unsigned int ui_get_cury(void); int diff --git a/src/ui_elements.c b/src/ui_elements.c index b274529..e57c0de 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "ui.h" @@ -7,7 +8,7 @@ #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" -#include "ui_nwindow.h" +#include "ui_txtwindow.h" #include "ui_elements.h" #include "status.h" @@ -17,9 +18,7 @@ #define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) #define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 #define INFOWND_WIDTH 25 -#define INFOWND_HEIGHT 3 -#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6) -#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1 +#define INFOWND_HEIGHT 1 static struct input *pw_input; static struct anic *heartbeat; @@ -80,9 +79,14 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) mq_passwd_send(a->input); clear_input(wnd, a); ui_ipc_msgrecv(MQ_IF, ipc_buf); + set_txtwindow_color(infownd, COLOR_PAIR(5), COLOR_PAIR(5)); set_txtwindow_title(infownd, "BUSY"); set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); + sleep(2); + ui_ipc_msgrecv(MQ_IF, ipc_buf); + set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); + set_txtwindow_text(infownd, ipc_buf); break; case UIKEY_BACKSPACE: del_input(wnd, a); @@ -106,11 +110,17 @@ void init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) { asprintf(&title, "/* %s-%s */", PKGNAME, VERSION); - pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); + pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, + (unsigned int)(max_y / 2)-PASSWD_YRELPOS, + PASSWD_WIDTH, "PASSWORD: ", + IPC_MQSIZ, COLOR_PAIR(3), COLOR_PAIR(2)); heartbeat = init_anic_default(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]"); - higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), higher_statusbar_update); - lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update); - infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update); + higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), + higher_statusbar_update); + lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), + lower_statusbar_update); + infownd = init_txtwindow_centered(INFOWND_WIDTH, INFOWND_HEIGHT, + infownd_update); infownd->userptr = calloc(4, sizeof(char)); (*(char*)(infownd->userptr)) = '.'; diff --git a/src/ui_ipc.h b/src/ui_ipc.h index ec36f35..5ecfaa4 100644 --- a/src/ui_ipc.h +++ b/src/ui_ipc.h @@ -8,7 +8,7 @@ enum UI_IPC_SEM { - SEM_RD = 0, /* UI Init done? */ + SEM_RD = 0, /* UI Init done? */ SEM_UI, /* TUI active? */ SEM_IN, /* Textfield has input avail */ SEM_BS, /* Master process busy */ diff --git a/src/ui_nwindow.c b/src/ui_nwindow.c deleted file mode 100644 index aa1812d..0000000 --- a/src/ui_nwindow.c +++ /dev/null @@ -1,157 +0,0 @@ -#include -#include - -#include "ui_nwindow.h" - - -struct txtwindow * -init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update) -{ - struct txtwindow *a = calloc(1, sizeof(struct txtwindow)); - - a->x = x; - a->y = y; - a->width = width; - a->height = height; - a->active = false; - a->title_len = INITIAL_TITLE_LEN; - a->title = calloc(a->title_len+1, sizeof(char)); - a->text = NULL; - a->attrs = attrs; - a->text_attrs = text_attrs; - a->window_func = cb_update; - return (a); -} - -static void -__free_text(struct txtwindow *a) -{ - if (a->text) { - if (a->text[0]) { - free(a->text[0]); - } - free(a->text); - a->text = NULL; - } -} - -void -free_txtwindow(struct txtwindow *a) -{ - __free_text(a); - if (a->title) { - free(a->title); - } - free(a); -} - -static void -print_wnd(struct txtwindow *a) -{ - int i, x = a->x, y = a->y, w = a->width, h = a->height; - char tmp[a->width+1]; - - attron(a->attrs); - /* print window surface */ - memset(tmp, ' ', a->width); - tmp[a->width] = '\0'; - for (i = y-1; i < y+h+2; i++) - mvprintw(i, x, tmp); - /* print window border */ - mvhline(y-2, x, 0, w); - mvhline(y+h+2, x, 0, w); - mvvline(y-1, x-1, 0, h+3); - mvvline(y-1, x+w, 0, h+3); - /* print window border edges */ - mvaddch(y-2, x-1, ACS_ULCORNER); - mvaddch(y+2+h, x-1, ACS_LLCORNER); - mvaddch(y-2, x+w, ACS_URCORNER); - mvaddch(y+2+h, x+w, ACS_LRCORNER); - /* print window title */ - attroff(a->attrs); - attron(a->text_attrs); - mvprintw(y-2, (float)x+(float)(w/2)-(float)(a->title_len*2/3), "[ %s ]", a->title); - /* print windows text */ - i = 0; - while ( a->text && a->text[i] ) { - mvprintw(y+i, x, a->text[i]); - i++; - } - attroff(a->text_attrs); -} - -static int -txtwindow_cb(WINDOW *win, void *data, bool timedout) -{ - struct txtwindow *a = (struct txtwindow *) data; - - if (a->active == true) { - print_wnd(a); - } - return (UICB_OK); -} - -void inline -register_txtwindow(struct txtwindow *a) -{ - struct ui_callbacks cbs; - cbs.ui_element = txtwindow_cb; - cbs.ui_input = NULL; - register_ui_elt(&cbs, (void *) a, NULL); -} - -static size_t -__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src) -{ - size_t cursiz = sz_dest; - - if (sz_src > sz_dest) { - *p_dest = (char *) realloc(*p_dest, (sz_src+1) * sizeof(char)); - cursiz = sz_src; - } - memset(*p_dest, '\0', (cursiz+1) * sizeof(char)); - memcpy(*p_dest, p_src, (cursiz+1) * sizeof(char)); - return sz_src; -} - -/* seperate a String with NEWLINES into an array */ -static char ** -__do_textadjust(struct txtwindow *a, char *text) -{ - int i = 0, rows = (int)(strlen(text) / a->width)+1; - char **adj_text = calloc(rows+1, sizeof(char *)); - char *p_strtok, *tok; - const char sep[] = "\n"; - - if (rows > a->height) goto error; - p_strtok = strdup(text); - while ( (tok = strsep(&p_strtok, sep)) && rows-- >= 0 ) { - if (strlen(tok) > a->width) { - strcpy(tok+a->width-3, "..."); - *(tok+a->width) = '\0'; - } - adj_text[i] = tok; - i++; - } - return adj_text; -error: - free(adj_text); - return NULL; -} - -void -set_txtwindow_text(struct txtwindow *a, char *text) -{ - char **fmt_text = __do_textadjust(a, text); - - if (fmt_text) { - __free_text(a); - a->text = fmt_text; - } -} - -void -set_txtwindow_title(struct txtwindow *a, const char *title) -{ - a->title_len = __do_textcpy(&a->title, a->title_len, title, strlen(title)); -} diff --git a/src/ui_nwindow.h b/src/ui_nwindow.h deleted file mode 100644 index 198481b..0000000 --- a/src/ui_nwindow.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef UI_TXTWINDOW_H -#define UI_TXTWINDOW_H 1 - -#include - -#include "ui.h" - -#define INITIAL_TITLE_LEN 32 - -#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() - -struct txtwindow { - unsigned int y; - unsigned int x; - unsigned int width; - unsigned int height; - bool active; - char *title; - size_t title_len; - char **text; - int (*window_func)(WINDOW *, struct txtwindow *); - chtype attrs; - chtype text_attrs; - void *userptr; -}; - -typedef int (*window_func)(WINDOW *, struct txtwindow *); - -struct txtwindow * -init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update); - -void -free_txtwindow(struct txtwindow *a); - -void -register_txtwindow(struct txtwindow *a); - -void -set_txtwindow_text(struct txtwindow *a, char *text); - -void -set_txtwindow_title(struct txtwindow *a, const char *title); - -#endif diff --git a/src/ui_txtwindow.c b/src/ui_txtwindow.c new file mode 100644 index 0000000..5852c32 --- /dev/null +++ b/src/ui_txtwindow.c @@ -0,0 +1,175 @@ +#include +#include + +#include "ui_txtwindow.h" + + +struct txtwindow * +init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, window_func cb_update) +{ + struct txtwindow *a = calloc(1, sizeof(struct txtwindow)); + + a->x = x; + a->y = y; + a->width = width; + a->height = height; + a->active = false; + a->title_len = INITIAL_TITLE_LEN; + a->title = calloc(a->title_len+1, sizeof(char)); + a->text = NULL; + a->attrs = 0; + a->text_attrs = 0; + a->window_func = cb_update; + return (a); +} + +struct txtwindow * +init_txtwindow_centered(unsigned int width, unsigned int height, window_func cb_update) +{ + unsigned int x = (ui_get_maxx()/2)-(width/2); + unsigned int y = (ui_get_maxy()/2)-(height/2); + return init_txtwindow(x, y, width, height, cb_update); +} + +static void +__free_text(struct txtwindow *a) +{ + if (a->text) { + if (a->text[0]) { + free(a->text[0]); + } + free(a->text); + a->text = NULL; + } +} + +void +free_txtwindow(struct txtwindow *a) +{ + __free_text(a); + if (a->title) { + free(a->title); + } + free(a); +} + +static void +print_wnd(struct txtwindow *a) +{ + int i, x = a->x, y = a->y, w = a->width, h = a->height; + char tmp[a->width+1]; + + attron(a->attrs); + /* print window surface */ + memset(tmp, ' ', a->width); + tmp[a->width] = '\0'; + for (i = y-1; i < y+h+1; i++) + mvprintw(i, x, tmp); + /* print window border */ + mvhline(y-2, x, 0, w); + mvhline(y+h+1, x, 0, w); + mvvline(y-1, x-1, 0, h+3); + mvvline(y-1, x+w, 0, h+3); + /* print window border edges */ + mvaddch(y-2, x-1, ACS_ULCORNER); + mvaddch(y+1+h, x-1, ACS_LLCORNER); + mvaddch(y-2, x+w, ACS_URCORNER); + mvaddch(y+1+h, x+w, ACS_LRCORNER); + /* print window title */ + attroff(a->attrs); + attron(a->text_attrs); + mvprintw(y-2, x+(w/2)-((a->title_len+4)/2), "[ %s ]", a->title); + /* print windows text */ + i = -1; + if (a->text) { + while ( a->text[++i] ) { + mvprintw(y+i, x+1, a->text[i]); + } + } + attroff(a->text_attrs); +} + +static int +txtwindow_cb(WINDOW *win, void *data, bool timedout) +{ + struct txtwindow *a = (struct txtwindow *) data; + + if (a->active == true) { + print_wnd(a); + } + return (UICB_OK); +} + +void inline +register_txtwindow(struct txtwindow *a) +{ + struct ui_callbacks cbs; + cbs.ui_element = txtwindow_cb; + cbs.ui_input = NULL; + register_ui_elt(&cbs, (void *) a, NULL); +} + +static size_t +__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src) +{ + size_t cursiz = sz_dest; + + if (sz_src > sz_dest) { + *p_dest = (char *) realloc(*p_dest, (sz_src+1) * sizeof(char)); + cursiz = sz_src; + } + memset(*p_dest, '\0', (cursiz+1) * sizeof(char)); + memcpy(*p_dest, p_src, (cursiz+1) * sizeof(char)); + return sz_src; +} + +/* seperate a String with NEWLINES into an array */ +static char ** +__do_textadjust(struct txtwindow *a, char *text) +{ + int i = 0, rows = (int)(strlen(text) / a->width); + char **adj_text = calloc(rows+2, sizeof(char *)); + char *p_strtok, *tok; + const char sep[] = "\n"; + + if (rows > a->height) goto error; + p_strtok = strdup(text); + do { + tok = strsep(&p_strtok, sep); + if (strlen(tok) > a->width) { + strcpy(tok+a->width-3, "..."); + *(tok+a->width) = '\0'; + } + adj_text[i] = tok; + i++; + } while (rows > 0); + return adj_text; +error: + free(adj_text); + return NULL; +} + +void +set_txtwindow_text(struct txtwindow *a, char *text) +{ + char **fmt_text = __do_textadjust(a, text); + + if (fmt_text) { + __free_text(a); + a->text = fmt_text; + } +} + +void +set_txtwindow_title(struct txtwindow *a, const char *title) +{ + a->title_len = __do_textcpy(&a->title, a->title_len, title, strlen(title)); +} + +void +set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt) +{ + a->attrs = wnd; + a->text_attrs = txt; +} + diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h new file mode 100644 index 0000000..913cd99 --- /dev/null +++ b/src/ui_txtwindow.h @@ -0,0 +1,53 @@ +#ifndef UI_TXTWINDOW_H +#define UI_TXTWINDOW_H 1 + +#include + +#include "ui.h" + +#define INITIAL_TITLE_LEN 32 + +#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() + +struct txtwindow { + unsigned int y; + unsigned int x; + unsigned int width; + unsigned int height; + bool active; + char *title; + size_t title_len; + char **text; + int (*window_func)(WINDOW *, struct txtwindow *); + chtype attrs; + chtype text_attrs; + void *userptr; +}; + +typedef int (*window_func)(WINDOW *, struct txtwindow *); + +struct txtwindow * +init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, window_func cb_update); + +struct txtwindow * +init_txtwindow_centered(unsigned int width, unsigned int height, window_func cb_update); + +void +free_txtwindow(struct txtwindow *a); + +void +register_txtwindow(struct txtwindow *a); + +void +set_txtwindow_text(struct txtwindow *a, char *text); + +void +set_txtwindow_title(struct txtwindow *a, const char *title); + +void +set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt); + +void +set_txtwindow_pos(struct txtwindow *a, unsigned int x, unsigned int y); + +#endif -- cgit v1.2.3 From 5dcfb21b85391ddaace3ec7e2161859661894597 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 22:30:32 +0100 Subject: fixed wgetch key loop and wtimeout bug --- src/ui.c | 30 ++++++++++++++++++------------ src/ui.h | 6 ++++++ src/ui_elements.c | 19 +++++++++++++++++-- src/ui_input.c | 5 +---- src/ui_input.h | 2 +- src/ui_txtwindow.h | 2 +- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/ui.c b/src/ui.c index fa65e29..f30d10b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -47,7 +47,6 @@ static pthread_t thrd; static unsigned int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t mtx_input = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mtx_busy = PTHREAD_MUTEX_INITIALIZER; @@ -132,7 +131,6 @@ activate_ui_input(void *data) struct nask_ui *cur = nui; if (cur == NULL || data == NULL) return DOUI_NINIT; - pthread_mutex_lock(&mtx_input); while ( cur->data != NULL ) { if ( cur->data == data ) { if ( cur->cbs.ui_input != NULL && cur->cbs.ui_input(cur->wnd, data, UIKEY_ACTIVATE) == DOUI_OK ) { @@ -143,7 +141,6 @@ activate_ui_input(void *data) } cur = cur->next; } - pthread_mutex_unlock(&mtx_input); return ret; } @@ -152,12 +149,10 @@ deactivate_ui_input(void *data) { int ret = DOUI_ERR; - pthread_mutex_lock(&mtx_input); if (active != NULL && data == active->data) { active = NULL; ret = DOUI_OK; } - pthread_mutex_unlock(&mtx_input); return ret; } @@ -235,6 +230,20 @@ ui_thrd_force_update(void) pthread_mutex_unlock(&mtx_busy); } +void +ui_thrd_suspend(void) +{ + ui_thrd_force_update(); + pthread_mutex_lock(&mtx_busy); +} + +void +ui_thrd_resume(void) +{ + pthread_mutex_unlock(&mtx_busy); + ui_thrd_force_update(); +} + WINDOW * init_ui(void) { @@ -293,18 +302,15 @@ do_ui(void) return ret; } ui_ipc_semwait(SEM_RD); - wtimeout(wnd_main, 1000); pthread_mutex_unlock(&mtx_update); + wtimeout(stdscr, 1000); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - if ((key = wgetch(wnd_main)) == '\0') { - break; - } - if (key == -1) { + if ((key = wgetch(wnd_main)) == ERR) { continue; } if ( process_key(key) != true ) { - ui_ipc_semtrywait(SEM_UI); - ui_thrd_force_update(); + //ui_ipc_semtrywait(SEM_UI); + //ui_thrd_force_update(); } } stop_ui_thrd(); diff --git a/src/ui.h b/src/ui.h index 6f83149..8aa1d55 100644 --- a/src/ui.h +++ b/src/ui.h @@ -72,6 +72,12 @@ deactivate_ui_input(void *data); void ui_thrd_force_update(void); +void +ui_thrd_suspend(void); + +void +ui_thrd_resume(void); + WINDOW * init_ui(void); diff --git a/src/ui_elements.c b/src/ui_elements.c index e57c0de..427415a 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -73,23 +73,36 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) char ipc_buf[IPC_MQSIZ+1]; memset(ipc_buf, '\0', IPC_MQSIZ+1); + wtimeout(stdscr, -1); switch (key) { case UIKEY_ENTER: - deactivate_input(pw_input); mq_passwd_send(a->input); + ui_thrd_suspend(); clear_input(wnd, a); + deactivate_input(pw_input); ui_ipc_msgrecv(MQ_IF, ipc_buf); set_txtwindow_color(infownd, COLOR_PAIR(5), COLOR_PAIR(5)); set_txtwindow_title(infownd, "BUSY"); set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); + ui_thrd_resume(); sleep(2); ui_ipc_msgrecv(MQ_IF, ipc_buf); + ui_thrd_suspend(); set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); + set_txtwindow_title(infownd, "ERROR"); set_txtwindow_text(infownd, ipc_buf); + ui_thrd_resume(); +// while (getch() != '\n') { } +wgetch(stdscr); + ui_thrd_suspend(); + set_txtwindow_active(infownd, false); + activate_input(pw_input); + ui_thrd_resume(); break; case UIKEY_BACKSPACE: del_input(wnd, a); + ui_thrd_force_update(); break; case UIKEY_ESC: return DOUI_ERR; @@ -102,7 +115,9 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) break; default: add_input(wnd, a, key); + ui_thrd_force_update(); } + wtimeout(stdscr, 1000); return DOUI_OK; } @@ -129,7 +144,7 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) register_statusbar(lower); register_anic_default(heartbeat); register_txtwindow(infownd); - activate_input(wnd_main, pw_input); + activate_input(pw_input); set_statusbar_text(higher, title); } diff --git a/src/ui_input.c b/src/ui_input.c index b32570b..9c53330 100644 --- a/src/ui_input.c +++ b/src/ui_input.c @@ -121,7 +121,7 @@ print_input(WINDOW *win, struct input *a) } int -activate_input(WINDOW *win, struct input *a) +activate_input(struct input *a) { if (a == NULL) return (UICB_ERR_UNDEF); curs_set(1); @@ -146,7 +146,6 @@ add_input(WINDOW *win, struct input *a, int key) ++a->input_len; a->cur_pos = (a->cur_pos+1 < a->width ? a->cur_pos+1 : a->cur_pos); ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); - ui_thrd_force_update(); return (UICB_OK); } @@ -168,7 +167,6 @@ del_input(WINDOW *win, struct input *a) } mvwprintw(win, a->y, a->x + a->cur_pos + strlen(a->prompt), "_"); ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); - ui_thrd_force_update(); return (UICB_OK); } @@ -181,7 +179,6 @@ clear_input(WINDOW *win, struct input *a) a->input_pos = 0; a->cur_pos = 0; ui_set_cur(a->x + strlen(a->prompt) + a->cur_pos, a->y); - ui_thrd_force_update(); return (UICB_OK); } diff --git a/src/ui_input.h b/src/ui_input.h index 8ed7fc3..df7088c 100644 --- a/src/ui_input.h +++ b/src/ui_input.h @@ -26,7 +26,7 @@ void free_input(struct input *a); int -activate_input(WINDOW *win, struct input *a); +activate_input(struct input *a); int deactivate_input(struct input *a); diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h index 913cd99..0ebbd9f 100644 --- a/src/ui_txtwindow.h +++ b/src/ui_txtwindow.h @@ -7,7 +7,7 @@ #define INITIAL_TITLE_LEN 32 -#define set_txtwindow_active(wnd, activate) wnd->active = activate; ui_thrd_force_update() +#define set_txtwindow_active(wnd, activate) wnd->active = activate; struct txtwindow { unsigned int y; -- cgit v1.2.3 From 089551924a7c6bae2b03bee9999e5563f9267571 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 23:18:45 +0100 Subject: (auto(conf|make)|dpkg) versioning fix (src/version.h shows the DEFINITIVE version of the pkg) --- .gitignore | 1 + Makefile.am | 5 +++++ Makefile.debug | 8 ++------ configure.ac | 6 +++++- debian/rules | 4 +++- src/Makefile.am | 2 +- src/config.h | 8 +++++--- 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index dc4c5e9..e233e82 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /naskpass +/src/version.h /tests/producer /tests/consumer /tests/semtest diff --git a/Makefile.am b/Makefile.am index 1d76541..5b9f08b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,10 @@ SUBDIRS = src +install-exec-local: + /bin/mkdir -p '$(prefix)/lib/cryptsetup' + /usr/bin/install -c src/naskpass '$(prefix)/lib/cryptsetup/naskpass' + rm '$(prefix)/bin/naskpass' + rmdir '$(prefix)/bin' clean-local: rm -f naskpass diff --git a/Makefile.debug b/Makefile.debug index 66bf8e0..2faf3b2 100644 --- a/Makefile.debug +++ b/Makefile.debug @@ -3,7 +3,6 @@ LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt CC = gcc INSTALL = install STRIP = strip -VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.2a"; fi) BIN = naskpass SOURCES = $(wildcard src/*.c) OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) @@ -12,7 +11,7 @@ DEPS = $(patsubst %.c,%.d,$(SOURCES)) all: $(OBJECTS) $(BIN) %.o: %.c - $(CC) $(CFLAGS) -D_VERSION=\"$(VERSION)\" -c $< -o $@ + $(CC) $(CFLAGS) -c $< -o $@ $(BIN): $(SOURCES) $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) @@ -46,7 +45,4 @@ clean: rm -f $(BIN) $(MAKE) -C tests clean -source: - -dh_make --createorig -p naskpass_$(VERSION) -s -y - -.PHONY: all install clean +.PHONY: all debug release strip install uninstall clean diff --git a/configure.ac b/configure.ac index 943f5cf..dc50072 100644 --- a/configure.ac +++ b/configure.ac @@ -3,7 +3,7 @@ # AC_PREREQ([2.67]) -AC_INIT([naskpass], [0.01], [matzeton@googlemail.com]) +AC_INIT([naskpass], [0], [matzeton@googlemail.com]) AC_CONFIG_AUX_DIR([build]) AM_INIT_AUTOMAKE([1.11 foreign no-define -Wall -Werror]) AM_WITH_DMALLOC @@ -66,3 +66,7 @@ AC_SUBST([AM_LDFLAGS]) AC_CONFIG_FILES([Makefile src/Makefile]) AC_OUTPUT +echo "Run 'make' to finish the process." +test -d .git && VERSION="git-$(git rev-parse --short HEAD)" +echo "#define VERSION \"${VERSION}\"" >src/version.h + diff --git a/debian/rules b/debian/rules index 048a2ae..5cdc77d 100755 --- a/debian/rules +++ b/debian/rules @@ -10,6 +10,7 @@ configure-stamp: dh_testdir ./autogen.sh ./configure + echo "#define VERSION \"$(DEBVERS)\"" >src/version.h touch configure-stamp build: configure-stamp build-stamp @@ -30,7 +31,8 @@ distclean: build install: build dh_testdir dh_testroot - dh_clean -k + dh_clean + dh_prep dh_installdirs $(MAKE) install prefix=$(CURDIR)/debian/naskpass mkdir -p $(CURDIR)/debian/naskpass/usr/share/naskpass diff --git a/src/Makefile.am b/src/Makefile.am index 07b4cfa..aab865c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,3 @@ bin_PROGRAMS=naskpass -naskpass_SOURCES=main.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_nwindow.c ui_statusbar.c +naskpass_SOURCES=main.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_txtwindow.c ui_statusbar.c diff --git a/src/config.h b/src/config.h index 9d72ef3..797376f 100644 --- a/src/config.h +++ b/src/config.h @@ -12,8 +12,10 @@ #define MSQ_PWD "/naskpass-passwd" #define MSQ_INF "/naskpass-info" -#ifdef _VERSION -#define VERSION _VERSION -#else +#ifdef HAVE_CONFIG_H +#include "version.h" +#endif + +#ifndef VERSION #define VERSION "unknown" #endif -- cgit v1.2.3 From cb8ac81c8c538f4a02edb5c2762cfbc5f5adef88 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 16 Nov 2015 23:56:07 +0100 Subject: better tests --- .gitignore | 2 +- debian/rules | 1 + tests/Makefile | 17 +++++++++++++---- tests/consumer.c | 38 -------------------------------------- tests/producer.c | 5 ++--- tests/producer_consumer.c | 39 +++++++++++++++++++++++++++++++++++++++ tests/semconfig.h | 2 +- tests/semtest.c | 10 ++++++++-- tests/semtest2.c | 43 ------------------------------------------- tests/strsep.c | 28 +++++++++++++++++++++++----- 10 files changed, 88 insertions(+), 97 deletions(-) delete mode 100644 tests/consumer.c create mode 100644 tests/producer_consumer.c delete mode 100644 tests/semtest2.c diff --git a/.gitignore b/.gitignore index e233e82..2aa84b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ /naskpass /src/version.h /tests/producer -/tests/consumer +/tests/producer_consumer /tests/semtest /tests/semtest2 /tests/mqtest diff --git a/debian/rules b/debian/rules index 5cdc77d..46395bb 100755 --- a/debian/rules +++ b/debian/rules @@ -17,6 +17,7 @@ build: configure-stamp build-stamp build-stamp: dh_testdir $(MAKE) + $(MAKE) -C tests -f Makefile run touch build-stamp clean: diff --git a/tests/Makefile b/tests/Makefile index 20b86ba..8afc274 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -Wall +CFLAGS = -Wall -g LDFLAGS = -lpthread -lrt CC = gcc SOURCES = $(wildcard *.c) @@ -7,9 +7,6 @@ DEPS = $(patsubst %.c,%.d,$(SOURCES)) all: $(BINARIES) -semtest2: - $(CC) $(CFLAGS) $(LDFLAGS) -I ../src ../src/ui_ipc.o semtest2.c -o semtest2 - %: %.c $(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<) @@ -17,4 +14,16 @@ clean: rm -f $(DEPS) rm -f $(BINARIES) +run: all + @echo "* running tests" + for test in $(patsubst %.c,%,$(SOURCES)); do \ + echo -n "* running $${test}"; \ + ./$${test} >/dev/null; \ + if [ $$? -ne 0 ]; then \ + echo " FAILED!"; \ + else \ + echo " OK!"; \ + fi; \ + done + .PHONY: all install clean diff --git a/tests/consumer.c b/tests/consumer.c deleted file mode 100644 index 3f93d1e..0000000 --- a/tests/consumer.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "semconfig.h" - -sem_t *mysem = NULL, *mycnt = NULL; -pid_t child; - - -int main(int argc, char **argv) { - int semval = 0; - - if ( (mysem = sem_open(TESTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL && - (mycnt = sem_open(CNTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL ) { - assert( sem_getvalue(mycnt, &semval) == 0 ); - printf("factory producing %d items\n", semval); - - while ( semval-- >= 0 ) { - usleep(250000); - LOG("consumer: -1"); - assert( sem_wait(mysem) == 0 ); - printf("remaining: %d\n", semval); - } - } else { - exit(1); - } - assert( sem_close(mysem) == 0 ); - - exit(0); -} - diff --git a/tests/producer.c b/tests/producer.c index 25af700..f1a568d 100644 --- a/tests/producer.c +++ b/tests/producer.c @@ -24,13 +24,12 @@ int main(int argc, char **argv) { if ( (mysem = sem_open(TESTSEM, O_CREAT, S_IRUSR | S_IWUSR, 0)) != NULL && (mycnt = sem_open(CNTSEM, O_CREAT, S_IRUSR | S_IWUSR, semval)) != NULL ) { while (semval-- >= 0) { - sleep(1); - LOG("producer: +1"); + usleep(250); + printf("producer: +1"); assert( sem_post(mysem) == 0 ); printf("remaining: %d\n", semval); } assert( sem_close(mysem) == 0 && sem_close(mycnt) == 0 ); - assert( sem_unlink(TESTSEM) == 0 && sem_unlink(CNTSEM) == 0 ); } else { exit(1); } diff --git a/tests/producer_consumer.c b/tests/producer_consumer.c new file mode 100644 index 0000000..104004a --- /dev/null +++ b/tests/producer_consumer.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 0; + + if ( (mysem = sem_open(TESTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL && + (mycnt = sem_open(CNTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL ) { + assert( sem_getvalue(mycnt, &semval) == 0 ); + printf("factory producing %d items\n", semval); + + while ( semval-- >= 0 ) { + usleep(250); + LOG("consumer: -1"); + assert( sem_wait(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + } else { + exit(1); + } + assert( sem_close(mysem) == 0 ); + assert( sem_unlink(TESTSEM) == 0 && sem_unlink(CNTSEM) == 0 ); + + exit(0); +} + diff --git a/tests/semconfig.h b/tests/semconfig.h index 0ad08b3..2878dbe 100644 --- a/tests/semconfig.h +++ b/tests/semconfig.h @@ -1,7 +1,7 @@ #ifndef CONFIG_H #define CONFIG_H 1 -#define LOG(text) fprintf(stderr, "%s\n", text); +#define LOG(text) printf("%s\n", text); #define CMD(cmd) LOG(cmd); cmd; #define TESTSEM "/testsem" #define CNTSEM "/testcnt" diff --git a/tests/semtest.c b/tests/semtest.c index f462a6e..fdecf2d 100644 --- a/tests/semtest.c +++ b/tests/semtest.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "semconfig.h" @@ -19,11 +20,11 @@ int main(int argc, char **argv) { if ( (mysem = sem_open(TESTSEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) != NULL ) { if ( (child = fork()) == 0 ) { /* child */ - sleep(1); + usleep(250); LOG("child: sempost"); sem_post(mysem); LOG("child: done"); - sleep(1); + usleep(250); exit(0); } else if (child > 0) { /* parent */ @@ -38,6 +39,11 @@ int main(int argc, char **argv) { sem_close(mysem); exit(1); } + + int sval; + assert ( sem_getvalue(mysem, &sval) == 0 ); + assert (sval == 0); + sem_unlink(TESTSEM); exit(0); } diff --git a/tests/semtest2.c b/tests/semtest2.c deleted file mode 100644 index 1724cf1..0000000 --- a/tests/semtest2.c +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "ui_ipc.h" - -pid_t child; - -#define LOG(cmd) fprintf(stderr, "%s\n", cmd); - -int main(int argc, char **argv) { - if (ui_ipc_init(1) != 0) { - perror("ui_ipc_init"); - exit(1); - } - if ( (child = fork()) == 0 ) { - /* child */ - sleep(1); - LOG("child: sempost"); - ui_ipc_sempost(SEM_RD); - LOG("child: done"); - sleep(1); - exit(0); - } else if (child > 0) { - /* parent */ - LOG("parent: semwait"); - ui_ipc_semwait(SEM_RD); - LOG("parent: waitpid"); - waitpid(child, NULL, 0); - } else if (child < 0) { - perror("fork"); - exit(1); - } - ui_ipc_free(1); - - exit(0); -} - diff --git a/tests/strsep.c b/tests/strsep.c index aa8e1ad..24564af 100644 --- a/tests/strsep.c +++ b/tests/strsep.c @@ -8,16 +8,28 @@ int main(int argc, char **argv) { int i = 0; char *tok, *p_tok; - char **arr; + static char **arr; - if (argc != 4) { + int arrsiz = 1000; + static char *delim; + static char *str; + + if (argc == 1) { + printf("automatic test ..\n"); + delim = strdup(" "); + str = strdup("this is a simple string, which should be extracted to 12 strings"); + } else if (argc != 4) { fprintf(stderr, "usage: %s [ARR_SIZ] [DELIM] [STRING]\n", argv[0]); exit(1); + } else { + arrsiz = atoi(argv[1]); + delim = strdup(argv[2]); + str = strdup(argv[3]); } - arr = calloc(atoi(argv[1]), sizeof(char *)); - p_tok = argv[3]; - while ( (tok = strsep(&p_tok, argv[2])) != NULL ) { + arr = calloc(arrsiz, sizeof(char *)); + p_tok = str; + while ( (tok = strsep(&p_tok, delim)) != NULL ) { arr[i] = tok; i++; } @@ -27,5 +39,11 @@ int main(int argc, char **argv) printf("ARRAY[%d]: %s\n", i, arr[i]); i++; } + + if (argc == 1) { + if (i == 12) { + return 0; + } else return -1; + } return 0; } -- cgit v1.2.3 From ecd71a9af7f74d1cd73ef015b767aa63dc0605d9 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 17 Nov 2015 09:53:47 +0100 Subject: - atmout/pthread_cond_timedwait bug (still not fixed) - txtwindow update callback added - better update funcs --- Makefile.debug | 5 ++++- src/main.c | 4 ++-- src/ui.c | 16 ++++++++------ src/ui_elements.c | 65 +++++++++++++++++++++++++++--------------------------- src/ui_statusbar.c | 8 +++---- src/ui_statusbar.h | 4 ++-- src/ui_txtwindow.c | 12 ++++++++++ src/ui_txtwindow.h | 8 ++++--- 8 files changed, 70 insertions(+), 52 deletions(-) diff --git a/Makefile.debug b/Makefile.debug index 2faf3b2..134457c 100644 --- a/Makefile.debug +++ b/Makefile.debug @@ -45,4 +45,7 @@ clean: rm -f $(BIN) $(MAKE) -C tests clean -.PHONY: all debug release strip install uninstall clean +test: + $(MAKE) -C tests run + +.PHONY: all debug release strip install uninstall clean test diff --git a/src/main.c b/src/main.c index 0d0eae3..34e27c9 100644 --- a/src/main.c +++ b/src/main.c @@ -34,8 +34,8 @@ enum msg_index { MSG_CRYPTCMD_ERR, MSG_NUM }; -static const char *msg_arr[] = { "Please wait, got a piped password ..", - "Please wait, busy ..", +static const char *msg_arr[] = { "Please wait, got a piped password", + "Please wait, busy", "check_fifo: %s is not a FIFO\n", "check_fifo: %s error(%d): %s\n", "fifo: cryptcreate busy", diff --git a/src/ui.c b/src/ui.c index f30d10b..acf04f5 100644 --- a/src/ui.c +++ b/src/ui.c @@ -161,7 +161,6 @@ process_key(char key) { bool ret = false; - atmout = APP_TIMEOUT; if ( active != NULL ) { ret = ( active->cbs.ui_input(active->wnd, active->data, key) == DOUI_OK ? true : false ); } @@ -176,6 +175,9 @@ do_ui_update(bool timed_out) /* call all draw callback's */ erase(); + if (timed_out == TRUE && atmout > 0) { + atmout--; + } else atmout = APP_TIMEOUT; while (cur != NULL) { if (cur->cbs.ui_element != NULL) { cur->cbs.ui_element(cur->wnd, cur->data, timed_out); @@ -199,18 +201,18 @@ static void * ui_thrd(void *arg) { int cnd_ret; - struct timeval now; + struct timespec now; struct timespec wait; - gettimeofday(&now, NULL); + //gettimeofday(&now, NULL); + clock_gettime(CLOCK_REALTIME, &now); wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; - wait.tv_nsec = now.tv_usec * 1000; + wait.tv_nsec = now.tv_nsec * 1000; do_ui_update(true); ui_ipc_sempost(SEM_RD); pthread_mutex_lock(&mtx_update); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - if (--atmout <= 0) ui_ipc_semtrywait(SEM_UI); pthread_mutex_lock(&mtx_busy); do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); pthread_mutex_unlock(&mtx_busy); @@ -309,9 +311,9 @@ do_ui(void) continue; } if ( process_key(key) != true ) { - //ui_ipc_semtrywait(SEM_UI); - //ui_thrd_force_update(); + ui_ipc_semtrywait(SEM_UI); } + ui_thrd_force_update(); } stop_ui_thrd(); free_ui_elements(); diff --git a/src/ui_elements.c b/src/ui_elements.c index 427415a..9f4994d 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -19,51 +19,44 @@ #define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1 #define INFOWND_WIDTH 25 #define INFOWND_HEIGHT 1 +#define BSTR_LEN 3 static struct input *pw_input; static struct anic *heartbeat; static struct statusbar *higher, *lower; static struct txtwindow *infownd; +static struct tctwindow *errwnd; static char *title = NULL; +static char busy_str[BSTR_LEN+1] = ".\0\0\0"; static int -lower_statusbar_update(WINDOW *win, struct statusbar *bar) +lower_statusbar_update(WINDOW *win, struct statusbar *bar, bool ui_timeout) { + if (ui_timeout == FALSE) return DOUI_OK; char *tmp = get_system_stat(); set_statusbar_text(bar, tmp); free(tmp); - return 0; -} - -static int -higher_statusbar_update(WINDOW *win, struct statusbar *bar) -{ - return 0; + return DOUI_OK; } static int -infownd_update(WINDOW *win, struct txtwindow *tw) +higher_statusbar_update(WINDOW *win, struct statusbar *bar, bool ui_timeout) { - char *tmp = (char*)(tw->userptr); - size_t len = strlen(tmp); - - if (tw->active) { - if ( len == 3 ) { - memset(tmp+1, '\0', 2); - } else strcat(tmp, "."); - } else (*tmp) = '.'; - return 0; + return DOUI_OK; } static int -mq_passwd_send(char *passwd) +infownd_update(WINDOW *win, struct txtwindow *tw, bool ui_timeout) { - int ret; - - ui_ipc_sempost(SEM_IN); - ret = ui_ipc_msgsend(MQ_PW, passwd); - return ret; + if (ui_timeout == TRUE && tw->active == TRUE) { + size_t len = strlen(busy_str); + if (len > BSTR_LEN) { + memset(busy_str, '\0', BSTR_LEN+1); + busy_str[0] = '.'; + } else strcat(busy_str, "."); + } + return DOUI_OK; } static int @@ -76,33 +69,44 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) wtimeout(stdscr, -1); switch (key) { case UIKEY_ENTER: - mq_passwd_send(a->input); + ui_ipc_msgsend(MQ_PW, a->input); + ui_thrd_suspend(); clear_input(wnd, a); deactivate_input(pw_input); + ui_thrd_resume(); + ui_ipc_msgrecv(MQ_IF, ipc_buf); + + ui_thrd_suspend(); set_txtwindow_color(infownd, COLOR_PAIR(5), COLOR_PAIR(5)); set_txtwindow_title(infownd, "BUSY"); set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); ui_thrd_resume(); + sleep(2); ui_ipc_msgrecv(MQ_IF, ipc_buf); + ui_thrd_suspend(); set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); set_txtwindow_title(infownd, "ERROR"); set_txtwindow_text(infownd, ipc_buf); ui_thrd_resume(); -// while (getch() != '\n') { } -wgetch(stdscr); + + while (wgetch(stdscr) != '\n') { }; + ui_thrd_suspend(); set_txtwindow_active(infownd, false); activate_input(pw_input); ui_thrd_resume(); + + //ui_thrd_force_update(); + ui_ipc_sempost(SEM_IN); break; case UIKEY_BACKSPACE: del_input(wnd, a); - ui_thrd_force_update(); + //ui_thrd_force_update(); break; case UIKEY_ESC: return DOUI_ERR; @@ -115,7 +119,7 @@ wgetch(stdscr); break; default: add_input(wnd, a, key); - ui_thrd_force_update(); + //ui_thrd_force_update(); } wtimeout(stdscr, 1000); return DOUI_OK; @@ -136,8 +140,6 @@ init_ui_elements(WINDOW *wnd_main, unsigned int max_x, unsigned int max_y) lower_statusbar_update); infownd = init_txtwindow_centered(INFOWND_WIDTH, INFOWND_HEIGHT, infownd_update); - infownd->userptr = calloc(4, sizeof(char)); - (*(char*)(infownd->userptr)) = '.'; register_input(NULL, pw_input, passwd_input_cb); register_statusbar(higher); @@ -159,7 +161,6 @@ free_ui_elements(void) free_anic_default(heartbeat); free_statusbar(higher); free_statusbar(lower); - free(infownd->userptr); free_txtwindow(infownd); free_ui(); if (title) { diff --git a/src/ui_statusbar.c b/src/ui_statusbar.c index 8fcfeb4..df88683 100644 --- a/src/ui_statusbar.c +++ b/src/ui_statusbar.c @@ -36,11 +36,6 @@ statusbar_cb(WINDOW *win, void *data, bool timed_out) size_t len; if (a == NULL) return (UICB_ERR_UNDEF); - if (timed_out == true) { - if (a->status_func != NULL) { - a->status_func(win, a); - } - } attron(a->attrs); len = strnlen(a->text, a->width); if (len < a->width) { @@ -50,6 +45,9 @@ statusbar_cb(WINDOW *win, void *data, bool timed_out) memset(tmp, ' ', a->width); tmp[a->width] = '\0'; strncpy((tmp + diff_pos), a->text, len); + if (a->status_func != NULL) { + a->status_func(win, a, timed_out); + } if (win != NULL) { mvwprintw(win, a->y, 0, tmp); } else { diff --git a/src/ui_statusbar.h b/src/ui_statusbar.h index 5139c14..65e9e12 100644 --- a/src/ui_statusbar.h +++ b/src/ui_statusbar.h @@ -8,11 +8,11 @@ struct statusbar { unsigned int y; unsigned int width; char *text; - int (*status_func)(WINDOW *, struct statusbar *); + int (*status_func)(WINDOW *, struct statusbar *, bool); chtype attrs; }; -typedef int (*status_func)(WINDOW *, struct statusbar *); +typedef int (*status_func)(WINDOW *, struct statusbar *, bool); struct statusbar * init_statusbar(unsigned int y, unsigned int width, chtype attrs, status_func cb_update); diff --git a/src/ui_txtwindow.c b/src/ui_txtwindow.c index 5852c32..90d152d 100644 --- a/src/ui_txtwindow.c +++ b/src/ui_txtwindow.c @@ -96,6 +96,11 @@ txtwindow_cb(WINDOW *win, void *data, bool timedout) if (a->active == true) { print_wnd(a); + if (a->window_func) { + attron(a->text_attrs); + a->window_func(win, a, timedout); + attroff(a->text_attrs); + } } return (UICB_OK); } @@ -173,3 +178,10 @@ set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt) a->text_attrs = txt; } +void +set_txtwindow_dim(struct txtwindow *a, unsigned int w, unsigned int h) +{ + a->width = w; + a->height = h; +} + diff --git a/src/ui_txtwindow.h b/src/ui_txtwindow.h index 0ebbd9f..1e1bc3f 100644 --- a/src/ui_txtwindow.h +++ b/src/ui_txtwindow.h @@ -18,13 +18,12 @@ struct txtwindow { char *title; size_t title_len; char **text; - int (*window_func)(WINDOW *, struct txtwindow *); + int (*window_func)(WINDOW *, struct txtwindow *, bool); chtype attrs; chtype text_attrs; - void *userptr; }; -typedef int (*window_func)(WINDOW *, struct txtwindow *); +typedef int (*window_func)(WINDOW *, struct txtwindow *, bool); struct txtwindow * init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, window_func cb_update); @@ -50,4 +49,7 @@ set_txtwindow_color(struct txtwindow *a, chtype wnd, chtype txt); void set_txtwindow_pos(struct txtwindow *a, unsigned int x, unsigned int y); +void +set_txtwindow_dim(struct txtwindow *a, unsigned int w, unsigned int h); + #endif -- cgit v1.2.3 From e674872a17825b6a9e48464ff80ced9b3e44ec46 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 17 Nov 2015 16:35:01 +0100 Subject: - pthread test --- .gitignore | 1 + tests/pthread.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/pthread.c diff --git a/.gitignore b/.gitignore index 2aa84b5..8c51c44 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ /tests/semtest2 /tests/mqtest /tests/strsep +/tests/pthread *.swp *.o *.d diff --git a/tests/pthread.c b/tests/pthread.c new file mode 100644 index 0000000..eed53a6 --- /dev/null +++ b/tests/pthread.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include + + +static pthread_mutex_t testMutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t testCond = PTHREAD_COND_INITIALIZER; + +static void +mywait(int time_secs) +{ + struct timespec now; + int rt; + + clock_gettime(CLOCK_REALTIME, &now); + now.tv_sec += time_secs; + + assert( pthread_mutex_lock(&testMutex) == 0 ); + rt = pthread_cond_timedwait(&testCond, &testMutex, &now); + assert( rt == ETIMEDOUT || rt == 0 ); + assert( pthread_mutex_unlock(&testMutex) == 0 ); + printf("Done.\n"); +} + +static void * +fun(void *arg) +{ + printf("Thread wait ..\n"); + mywait(1); + mywait(1); + return NULL; +} + +int +main(void) +{ + pthread_t thread; + void *ret; + + assert( pthread_create(&thread, NULL, fun, NULL) == 0 ); + usleep(50000); + assert( pthread_cond_signal(&testCond) == 0 ); + printf("Mainthread: join\n"); + assert( pthread_join(thread, &ret) == 0 ); + return 0; +} -- cgit v1.2.3 From 52a0dc4e34889317f4fec73a0a2f4069efa69ace Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 17 Nov 2015 17:12:55 +0100 Subject: ncurses test --- .gitignore | 1 + tests/Makefile | 2 +- tests/ncurses.c | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/ncurses.c diff --git a/.gitignore b/.gitignore index 8c51c44..61d8ae2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /tests/mqtest /tests/strsep /tests/pthread +/tests/ncurses *.swp *.o *.d diff --git a/tests/Makefile b/tests/Makefile index 8afc274..a8b0e29 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,5 @@ CFLAGS = -Wall -g -LDFLAGS = -lpthread -lrt +LDFLAGS = -lpthread -lrt -lncurses CC = gcc SOURCES = $(wildcard *.c) BINARIES = $(patsubst %.c,%,$(SOURCES)) diff --git a/tests/ncurses.c b/tests/ncurses.c new file mode 100644 index 0000000..23ef28d --- /dev/null +++ b/tests/ncurses.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + + +static WINDOW *wnd; + +int +main(void) +{ + assert( (wnd = initscr()) != NULL ); + assert( start_color() == 0 ); + assert( init_pair(1, COLOR_BLACK, COLOR_WHITE) == 0 ); + assert( raw() == 0 ); + assert( keypad(wnd, TRUE) == 0 ); + assert( noecho() == 0 ); + assert( cbreak() == 0 ); + assert( printw("TESTEST") == 0 ); + assert( refresh() == 0 ); + assert( clear() == 0 ); + assert( delwin(wnd) == 0 ); + assert( endwin() == 0 ); + return 0; +} -- cgit v1.2.3 From a3d1a3011a74cb9dca2a15edf47f766b69ff4331 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 17 Nov 2015 18:45:43 +0100 Subject: replaced obsolete gettimeofday with clock_gettime (pthread_conf_timedwait) --- configure.ac | 2 +- src/aconfig.h.in | 3 --- src/ui.c | 20 +++++++++----------- src/ui_elements.c | 19 ++++++++++++++----- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/configure.ac b/configure.ac index dc50072..bafaddc 100644 --- a/configure.ac +++ b/configure.ac @@ -57,7 +57,7 @@ AC_FUNC_STRNLEN AC_FUNC_STAT AC_FUNC_MKTIME AC_FUNC_VPRINTF -AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork gettimeofday memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" diff --git a/src/aconfig.h.in b/src/aconfig.h.in index f690396..a24ff8d 100644 --- a/src/aconfig.h.in +++ b/src/aconfig.h.in @@ -40,9 +40,6 @@ /* Define to 1 if you have the `getpagesize' function. */ #undef HAVE_GETPAGESIZE -/* Define to 1 if you have the `gettimeofday' function. */ -#undef HAVE_GETTIMEOFDAY - /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H diff --git a/src/ui.c b/src/ui.c index acf04f5..a3e43c2 100644 --- a/src/ui.c +++ b/src/ui.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -202,22 +203,20 @@ ui_thrd(void *arg) { int cnd_ret; struct timespec now; - struct timespec wait; - //gettimeofday(&now, NULL); - clock_gettime(CLOCK_REALTIME, &now); - wait.tv_sec = now.tv_sec + UILOOP_TIMEOUT; - wait.tv_nsec = now.tv_nsec * 1000; do_ui_update(true); ui_ipc_sempost(SEM_RD); pthread_mutex_lock(&mtx_update); + clock_gettime(CLOCK_REALTIME, &now); + now.tv_sec += UILOOP_TIMEOUT; while ( ui_ipc_getvalue(SEM_UI) > 0 ) { - cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); + cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &now); pthread_mutex_lock(&mtx_busy); do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); pthread_mutex_unlock(&mtx_busy); if (cnd_ret == ETIMEDOUT) { - wait.tv_sec += UILOOP_TIMEOUT; + clock_gettime(CLOCK_REALTIME, &now); + now.tv_sec += UILOOP_TIMEOUT; } } pthread_mutex_unlock(&mtx_update); @@ -235,7 +234,6 @@ ui_thrd_force_update(void) void ui_thrd_suspend(void) { - ui_thrd_force_update(); pthread_mutex_lock(&mtx_busy); } @@ -243,7 +241,6 @@ void ui_thrd_resume(void) { pthread_mutex_unlock(&mtx_busy); - ui_thrd_force_update(); } WINDOW * @@ -265,6 +262,7 @@ init_ui(void) raw(); keypad(stdscr, TRUE); noecho(); + nodelay(stdscr, TRUE); cbreak(); return (wnd_main); } @@ -305,13 +303,13 @@ do_ui(void) } ui_ipc_semwait(SEM_RD); pthread_mutex_unlock(&mtx_update); - wtimeout(stdscr, 1000); + wtimeout(stdscr, 500); while ( ui_ipc_getvalue(SEM_UI) > 0 ) { if ((key = wgetch(wnd_main)) == ERR) { continue; } if ( process_key(key) != true ) { - ui_ipc_semtrywait(SEM_UI); + raise(SIGTERM); } ui_thrd_force_update(); } diff --git a/src/ui_elements.c b/src/ui_elements.c index 9f4994d..535bc0c 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -66,7 +66,7 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) char ipc_buf[IPC_MQSIZ+1]; memset(ipc_buf, '\0', IPC_MQSIZ+1); - wtimeout(stdscr, -1); +// wtimeout(stdscr, -1); switch (key) { case UIKEY_ENTER: ui_ipc_msgsend(MQ_PW, a->input); @@ -84,6 +84,7 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) set_txtwindow_text(infownd, ipc_buf); set_txtwindow_active(infownd, true); ui_thrd_resume(); + ui_thrd_force_update(); sleep(2); ui_ipc_msgrecv(MQ_IF, ipc_buf); @@ -101,14 +102,22 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) activate_input(pw_input); ui_thrd_resume(); - //ui_thrd_force_update(); ui_ipc_sempost(SEM_IN); break; case UIKEY_BACKSPACE: del_input(wnd, a); - //ui_thrd_force_update(); break; case UIKEY_ESC: + wtimeout(stdscr, 0); + ui_thrd_suspend(); + deactivate_input(pw_input); + set_txtwindow_active(infownd, true); + set_txtwindow_color(infownd, COLOR_PAIR(5), COLOR_PAIR(5)); + set_txtwindow_title(infownd, "BUSY"); + set_txtwindow_text(infownd, "bye bye"); + ui_thrd_resume(); + ui_thrd_force_update(); + sleep(2); return DOUI_ERR; case UIKEY_DOWN: case UIKEY_UP: @@ -119,9 +128,9 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) break; default: add_input(wnd, a, key); - //ui_thrd_force_update(); } - wtimeout(stdscr, 1000); +// wtimeout(stdscr, 1000); + refresh(); return DOUI_OK; } -- cgit v1.2.3 From 2e8eddb34c371ec2b7c1d8f337fff35dd5205866 Mon Sep 17 00:00:00 2001 From: toni Date: Tue, 17 Nov 2015 22:47:28 +0100 Subject: fixed permissions for scripts --- debian/rules | 6 +++--- scripts/naskconf | 0 scripts/naskpass.inithook | 0 scripts/naskpass.initscript | 0 4 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 scripts/naskconf mode change 100644 => 100755 scripts/naskpass.inithook mode change 100644 => 100755 scripts/naskpass.initscript diff --git a/debian/rules b/debian/rules index 46395bb..90c262e 100755 --- a/debian/rules +++ b/debian/rules @@ -37,9 +37,9 @@ install: build dh_installdirs $(MAKE) install prefix=$(CURDIR)/debian/naskpass mkdir -p $(CURDIR)/debian/naskpass/usr/share/naskpass - install -D -m644 ./scripts/naskconf $(CURDIR)/debian/naskpass/usr/share/naskpass/ - install -D -m644 ./scripts/naskpass.inithook $(CURDIR)/debian/naskpass/usr/share/naskpass/ - install -D -m644 ./scripts/naskpass.initscript $(CURDIR)/debian/naskpass/usr/share/naskpass/ + install -D -m755 ./scripts/naskconf $(CURDIR)/debian/naskpass/usr/share/naskpass/ + install -D -m755 ./scripts/naskpass.inithook $(CURDIR)/debian/naskpass/usr/share/naskpass/ + install -D -m755 ./scripts/naskpass.initscript $(CURDIR)/debian/naskpass/usr/share/naskpass/ binary-indep: build install diff --git a/scripts/naskconf b/scripts/naskconf old mode 100644 new mode 100755 diff --git a/scripts/naskpass.inithook b/scripts/naskpass.inithook old mode 100644 new mode 100755 diff --git a/scripts/naskpass.initscript b/scripts/naskpass.initscript old mode 100644 new mode 100755 -- cgit v1.2.3 From 138eae869c25339fdadf3e27fd7300a9ab2ce9bf Mon Sep 17 00:00:00 2001 From: root Date: Wed, 18 Nov 2015 23:40:12 +0100 Subject: - debian scripts: chmod +x - naskconf: file path fixed --- debian/postinst | 0 debian/prerm | 0 scripts/naskconf | 6 +++--- 3 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 debian/postinst mode change 100644 => 100755 debian/prerm diff --git a/debian/postinst b/debian/postinst old mode 100644 new mode 100755 diff --git a/debian/prerm b/debian/prerm old mode 100644 new mode 100755 diff --git a/scripts/naskconf b/scripts/naskconf index f7c9f5d..5f82f22 100755 --- a/scripts/naskconf +++ b/scripts/naskconf @@ -14,8 +14,8 @@ _nask_cmd () { return 1 fi dpkg-divert --package naskpass --add --rename --divert /var/backups/cryptroot.initramfs.bak ${ORGFILE} - cp /usr/share/naskpass/naskpass.script.initramfs ${ORGFILE} - ln -s /usr/share/naskpass/naskpass.hook.initramfs \ + cp /usr/share/naskpass/naskpass.initscript ${ORGFILE} + ln -s /usr/share/naskpass/naskpass.inithook \ /usr/share/initramfs-tools/hooks/naskpass db_set naskpass/active true elif [ "x$1" = "xDCTV" ] && [ "$RET" = "true" ]; then @@ -24,7 +24,7 @@ _nask_cmd () { dpkg-divert --package naskpass --rename --remove ${ORGFILE} db_set naskpass/active false elif [ "x$1" = "xUPDT" ] && [ "$RET" = "true" ]; then - cp /usr/share/naskpass/naskpass.script.initramfs ${ORGFILE} + cp /usr/share/naskpass/naskpass.initscript ${ORGFILE} fi return 0 } -- cgit v1.2.3 From 598ac27635ab47d9b07f61700dfa0c098e84eadc Mon Sep 17 00:00:00 2001 From: root Date: Wed, 18 Nov 2015 23:58:01 +0100 Subject: added auto(conf|make) as build depend --- debian/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/control b/debian/control index ee161d9..dc3c228 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: naskpass Section: admin Priority: extra Maintainer: Toni Uhlig -Build-Depends: debhelper (>= 8.0.0), libncurses5-dev, libtinfo-dev +Build-Depends: debhelper (>= 8.0.0), libncurses5-dev, libtinfo-dev, autoconf, automake Standards-Version: 3.9.3 Homepage: https://github.com/freecoding/naskpass.git #Vcs-Git: git://git.debian.org/collab-maint/naskpass.git -- cgit v1.2.3 From c67ee5ec195a04e9b9e0821d4c45d3a526af1d09 Mon Sep 17 00:00:00 2001 From: toni Date: Thu, 19 Nov 2015 09:35:45 +0100 Subject: nask_update returns not 0 --- debian/postinst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/postinst b/debian/postinst index 4e658c3..085db60 100755 --- a/debian/postinst +++ b/debian/postinst @@ -11,7 +11,7 @@ set -e case "$1" in configure) - nask_update + nask_update || true db_input high naskpass/activate || true db_go db_get naskpass/activate -- cgit v1.2.3 From 1d96bc97559d4db973da8deb0154553d0077b9a4 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 8 Dec 2015 23:42:40 +0100 Subject: fixed debconf error (missing template - dh_installdebconf) --- debian/naskpass.postinst | 44 ++++++++++++++++++++++++++++++++++++++++++++ debian/naskpass.prerm | 31 +++++++++++++++++++++++++++++++ debian/postinst | 45 --------------------------------------------- debian/prerm | 31 ------------------------------- debian/rules | 2 ++ 5 files changed, 77 insertions(+), 76 deletions(-) create mode 100755 debian/naskpass.postinst create mode 100755 debian/naskpass.prerm delete mode 100755 debian/postinst delete mode 100755 debian/prerm diff --git a/debian/naskpass.postinst b/debian/naskpass.postinst new file mode 100755 index 0000000..590a2ab --- /dev/null +++ b/debian/naskpass.postinst @@ -0,0 +1,44 @@ +#!/bin/sh +# postinst script for naskpass +# +# see: dh_installdeb(1) + +set -e + +. /usr/share/debconf/confmodule +. /usr/share/naskpass/naskconf + +case "$1" in + + configure) + nask_update + db_input high naskpass/activate + db_go + db_get naskpass/activate + if [ "x$RET" = "xtrue" ]; then + nask_activate + if [ "x${ERRMSG}" != "x" ]; then + echo "* ${ERRMSG}" >&2 + nask_deactivate + fi + else + nask_deactivate + fi + update-initramfs -u + ;; + + abort-upgrade) + ;; + + *) + echo "postinst called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 diff --git a/debian/naskpass.prerm b/debian/naskpass.prerm new file mode 100755 index 0000000..5b1c46d --- /dev/null +++ b/debian/naskpass.prerm @@ -0,0 +1,31 @@ +#!/bin/sh +# postrm script for naskpass +# +# see: dh_installdeb(1) + +set -e + + +. /usr/share/debconf/confmodule +. /usr/share/naskpass/naskconf + +case "$1" in + remove|purge) + nask_deactivate + db_purge + ;; + upgrade|failed-upgrade|abort-install|abort-upgrade|disappear) + ;; + + *) + echo "postrm called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 diff --git a/debian/postinst b/debian/postinst deleted file mode 100755 index 085db60..0000000 --- a/debian/postinst +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/sh -# postinst script for naskpass -# -# see: dh_installdeb(1) - -set -e - -. /usr/share/debconf/confmodule -. /usr/share/naskpass/naskconf - -case "$1" in - - configure) - nask_update || true - db_input high naskpass/activate || true - db_go - db_get naskpass/activate - if [ "$RET" = "true" ]; then - nask_activate || true - if [ "x${ERRMSG}" != "x" ]; then - echo "* ${ERRMSG}" >&2 - nask_deactivate - fi - else - nask_deactivate - fi - update-initramfs -u - ;; - - abort-upgrade) - db_purge - ;; - - *) - echo "postinst called with unknown argument \`$1'" >&2 - exit 1 - ;; -esac - -# dh_installdeb will replace this with shell code automatically -# generated by other debhelper scripts. - -#DEBHELPER# - -exit 0 diff --git a/debian/prerm b/debian/prerm deleted file mode 100755 index 5b1c46d..0000000 --- a/debian/prerm +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh -# postrm script for naskpass -# -# see: dh_installdeb(1) - -set -e - - -. /usr/share/debconf/confmodule -. /usr/share/naskpass/naskconf - -case "$1" in - remove|purge) - nask_deactivate - db_purge - ;; - upgrade|failed-upgrade|abort-install|abort-upgrade|disappear) - ;; - - *) - echo "postrm called with unknown argument \`$1'" >&2 - exit 1 - ;; -esac - -# dh_installdeb will replace this with shell code automatically -# generated by other debhelper scripts. - -#DEBHELPER# - -exit 0 diff --git a/debian/rules b/debian/rules index 90c262e..1c41e7c 100755 --- a/debian/rules +++ b/debian/rules @@ -35,6 +35,8 @@ install: build dh_clean dh_prep dh_installdirs + dh_installchangelogs + dh_installdebconf $(MAKE) install prefix=$(CURDIR)/debian/naskpass mkdir -p $(CURDIR)/debian/naskpass/usr/share/naskpass install -D -m755 ./scripts/naskconf $(CURDIR)/debian/naskpass/usr/share/naskpass/ -- cgit v1.2.3 From 70268fa85ea048fdd26aff38b83466b73048d46a Mon Sep 17 00:00:00 2001 From: toni Date: Wed, 9 Dec 2015 00:05:56 +0100 Subject: default option --- src/opt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opt.h b/src/opt.h index 6d66101..a5fd37f 100644 --- a/src/opt.h +++ b/src/opt.h @@ -2,7 +2,7 @@ #define OPT_H 1 #define OPT(opt_index) config_opts[opt_index] -#define GETOPT(opt_index) OPT(opt_index).opt +#define GETOPT(opt_index) (OPT(opt_index).found != 0 ? OPT(opt_index).opt : OPT(opt_index).def) #define OPT_USED(opt_index, uvalue) OPT(opt_index).found = uvalue; #define d_OPT(opt_index, rvalue) OPT(opt_index).opt.dec = rvalue; OPT_USED(opt_index, 1); #define s_OPT(opt_index, rvalue) OPT(opt_index).opt.str = rvalue; OPT_USED(opt_index, 1); -- cgit v1.2.3 From ac09bf8f4d1b4e71828199772a76538931a6c72d Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jan 2016 11:23:09 +0100 Subject: added ipc check binary --- Makefile.am | 4 +++- Makefile.debug | 23 +++++++++++++++-------- configure.ac | 2 +- src/Makefile.am | 4 ++-- src/check/check.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/check/check.c diff --git a/Makefile.am b/Makefile.am index 5b9f08b..26ab017 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,11 +3,13 @@ SUBDIRS = src install-exec-local: /bin/mkdir -p '$(prefix)/lib/cryptsetup' /usr/bin/install -c src/naskpass '$(prefix)/lib/cryptsetup/naskpass' + /usr/bin/install -c src/naskpass_check '$(prefix)/lib/cryptsetup/naskpass_check' rm '$(prefix)/bin/naskpass' + rm '$(prefix)/bin/naskpass_check' rmdir '$(prefix)/bin' clean-local: - rm -f naskpass + rm -f naskpass naskpass_check distclean-local: clean-local rm -f aclocal.m4 config.log config.status configure diff --git a/Makefile.debug b/Makefile.debug index 134457c..e727221 100644 --- a/Makefile.debug +++ b/Makefile.debug @@ -3,22 +3,28 @@ LDFLAGS = $(shell ncurses5-config --libs) -pthread -lrt CC = gcc INSTALL = install STRIP = strip -BIN = naskpass +BIN_NASKP = naskpass +BIN_CHECK = $(BIN_NASKP)_check +BINS = $(BIN_NASKP) $(BIN_CHECK) SOURCES = $(wildcard src/*.c) OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) DEPS = $(patsubst %.c,%.d,$(SOURCES)) -all: $(OBJECTS) $(BIN) +all: $(OBJECTS) $(BINS) %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ -$(BIN): $(SOURCES) - $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN) +$(BIN_NASKP): $(SOURCES) + $(CC) $(LDFLAGS) $(OBJECTS) -o $(BIN_NASKP) $(MAKE) -C tests CC='$(CC)' CFLAGS='$(CFLAGS)' all -strip: $(OBJECTS) $(BIN) - $(STRIP) $(BIN) +$(BIN_CHECK): src/check/check.c + $(CC) $(CFLAGS) $(LDFLAGS) src/check/check.c -o $(BIN_CHECK) + +strip: $(OBJECTS) $(BINS) + $(STRIP) $(BIN_NASKP) + $(STRIP) $(BIN_CHECK) release: all strip @@ -27,7 +33,8 @@ debug: @$(MAKE) -C tests CFLAGS='$(CFLAGS) install: - $(INSTALL) -D -m 0755 $(BIN) $(DESTDIR)/lib/cryptsetup/naskpass + $(INSTALL) -D -m 0755 $(BIN_NASKP) $(DESTDIR)/lib/cryptsetup/naskpass + $(INSTALL) -D -m 0755 $(BIN_CHECK) $(DESTDIR)/lib/cryptsetup/naskpass_check $(INSTALL) -D -m 0755 scripts/naskpass.inithook $(DESTDIR)/usr/share/naskpass/naskpass.hook.initramfs $(INSTALL) -D -m 0755 scripts/naskpass.initscript $(DESTDIR)/usr/share/naskpass/naskpass.script.initramfs $(INSTALL) -D -m 0755 scripts/naskconf $(DESTDIR)/usr/share/naskpass/naskconf @@ -42,7 +49,7 @@ uninstall: clean: rm -f $(DEPS) rm -f $(OBJECTS) - rm -f $(BIN) + rm -f $(BINS) $(MAKE) -C tests clean test: diff --git a/configure.ac b/configure.ac index bafaddc..5ea94ee 100644 --- a/configure.ac +++ b/configure.ac @@ -5,7 +5,7 @@ AC_PREREQ([2.67]) AC_INIT([naskpass], [0], [matzeton@googlemail.com]) AC_CONFIG_AUX_DIR([build]) -AM_INIT_AUTOMAKE([1.11 foreign no-define -Wall -Werror]) +AM_INIT_AUTOMAKE([1.11 subdir-objects foreign no-define -Wall -Werror]) AM_WITH_DMALLOC AC_CANONICAL_BUILD AC_CANONICAL_HOST diff --git a/src/Makefile.am b/src/Makefile.am index aab865c..305406e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,3 @@ -bin_PROGRAMS=naskpass +bin_PROGRAMS=naskpass naskpass_check naskpass_SOURCES=main.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_txtwindow.c ui_statusbar.c - +naskpass_check_SOURCES=check/check.c diff --git a/src/check/check.c b/src/check/check.c new file mode 100644 index 0000000..bbc22eb --- /dev/null +++ b/src/check/check.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include + +#define myassert(x) if ((x)) { ret &= 0xFF; } else { ret &= 0x00; } + +static volatile unsigned char ret = 0xFF; +static mqd_t mq_test; +static mqd_t mq_recv; +static const size_t bufsiz = 256; + +int main(int argc, char **argv) +{ + int c_stat; + struct mq_attr m_attr; + char buf[bufsiz], recv[bufsiz]; + unsigned int prio; + ssize_t sz_recv; + + memset(buf, '\0', bufsiz); + memset(recv, '\0', bufsiz); + if (argc > 1) + strncpy(buf, argv[1], bufsiz-1); + + m_attr.mq_flags = 0; + m_attr.mq_msgsize = bufsiz; + m_attr.mq_maxmsg = 10; + m_attr.mq_curmsgs = 0; + + mq_unlink("/testmq"); + myassert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); + myassert( mq_getattr(mq_test, &m_attr) == 0 ); + + strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ"); + myassert( mq_send(mq_test, buf, bufsiz, 0) == 0 ); + myassert( (sz_recv = mq_receive(mq_test, recv, bufsiz, &prio)) > 0 ); + + memset(recv, '\0', bufsiz); + if (fork() > 0) { + myassert( (mq_recv = mq_open( "/testmq", O_RDONLY, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); + myassert( (sz_recv = mq_receive(mq_recv, recv, bufsiz, &prio)) > 0 ); + return ret; + } + myassert( mq_send(mq_test, buf, bufsiz, 0) == 0 ); + wait(&c_stat); + myassert( c_stat == 0xFF ); + myassert( mq_close(mq_test) == 0 ); + myassert( mq_unlink("/testmq") == 0 ); + + return ~ret; +} -- cgit v1.2.3 From 718387818bced424d74040e4330cd1a63df00c4e Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jan 2016 11:39:05 +0100 Subject: naskpass_check --- configure.ac | 2 +- scripts/naskpass.inithook | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 5ea94ee..bba191d 100644 --- a/configure.ac +++ b/configure.ac @@ -57,7 +57,7 @@ AC_FUNC_STRNLEN AC_FUNC_STAT AC_FUNC_MKTIME AC_FUNC_VPRINTF -AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol mq_open mq_getattr mq_send mq_receive mq_close mq_unlink],,[AC_MSG_ERROR([*** Missing essential functions.])]) AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" diff --git a/scripts/naskpass.inithook b/scripts/naskpass.inithook index 083c477..074dff3 100755 --- a/scripts/naskpass.inithook +++ b/scripts/naskpass.inithook @@ -19,6 +19,7 @@ esac . /usr/share/initramfs-tools/hook-functions copy_exec /lib/cryptsetup/naskpass /lib/cryptsetup +copy_exec /lib/cryptsetup/naskpass_check /lib/cryptsetup mkdir -p ${DESTDIR}/lib/terminfo/l cp /lib/terminfo/l/linux ${DESTDIR}/lib/terminfo/l/ -- cgit v1.2.3 From b4274c3fe239761c88915237da670fd061083576 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jan 2016 12:26:56 +0100 Subject: removed mq_ checks --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index bba191d..5ea94ee 100644 --- a/configure.ac +++ b/configure.ac @@ -57,7 +57,7 @@ AC_FUNC_STRNLEN AC_FUNC_STAT AC_FUNC_MKTIME AC_FUNC_VPRINTF -AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol mq_open mq_getattr mq_send mq_receive mq_close mq_unlink],,[AC_MSG_ERROR([*** Missing essential functions.])]) +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" -- cgit v1.2.3 From 1ed2e4dd46d9306014e3d75b9dd746cf9622546e Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jan 2016 12:59:00 +0100 Subject: password check fixed, run_cryptcreate silenced --- src/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 34e27c9..31afd29 100644 --- a/src/main.c +++ b/src/main.c @@ -73,7 +73,7 @@ run_cryptcreate(char *pass, char *crypt_cmd) char *cmd; if (crypt_cmd == NULL || pass == NULL) return (-1); - asprintf(&cmd, "echo '%s' | %s", pass, crypt_cmd); + asprintf(&cmd, "echo '%s' | %s >/dev/null 2>/dev/null", pass, crypt_cmd); retval = system(cmd); free(cmd); return (retval); @@ -152,6 +152,8 @@ main(int argc, char **argv) ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); + } else { + ui_ipc_semtrywait(SEM_UI); } ui_ipc_semwait(SEM_IN); } -- cgit v1.2.3 From ad31e73105e58079c2f4fe85b70b80cd7ae1c76a Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jan 2016 13:29:11 +0100 Subject: blah --- .gitignore | 1 + scripts/naskpass.initscript | 2 +- src/check/check.c | 36 +++++++++++++++++++++++------------- src/log.c | 19 +++++++++++++++++++ src/log.h | 8 ++++++++ src/main.c | 14 +++++++++++--- src/opt.c | 3 +++ 7 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/.gitignore b/.gitignore index 61d8ae2..de07cbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /naskpass +/naskpass_check /src/version.h /tests/producer /tests/producer_consumer diff --git a/scripts/naskpass.initscript b/scripts/naskpass.initscript index 21ced37..fa22318 100755 --- a/scripts/naskpass.initscript +++ b/scripts/naskpass.initscript @@ -311,7 +311,7 @@ setup_mapping() fi else dmesg -n 1 - if ! $cryptkeyscript -c "cryptsetup -T 1 open $cryptsource $crypttarget"; then + if ! $cryptkeyscript -c "/sbin/cryptsetup -T 1 open $cryptsource $crypttarget"; then message "naskpass: failed" continue else diff --git a/src/check/check.c b/src/check/check.c index bbc22eb..9d28129 100644 --- a/src/check/check.c +++ b/src/check/check.c @@ -4,13 +4,16 @@ #include #include #include +#include #include -#define myassert(x) if ((x)) { ret &= 0xFF; } else { ret &= 0x00; } -static volatile unsigned char ret = 0xFF; +#define myassert(x, emask) if ((x)) { ret &= ret; } else { ret |= emask; } + +static volatile unsigned long long int ret = 0x0; static mqd_t mq_test; static mqd_t mq_recv; +static sem_t *sp_test; static const size_t bufsiz = 256; int main(int argc, char **argv) @@ -32,24 +35,31 @@ int main(int argc, char **argv) m_attr.mq_curmsgs = 0; mq_unlink("/testmq"); - myassert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); - myassert( mq_getattr(mq_test, &m_attr) == 0 ); + myassert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1, 0x1 ); + myassert( mq_getattr(mq_test, &m_attr) == 0, 0x2 ); strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ"); - myassert( mq_send(mq_test, buf, bufsiz, 0) == 0 ); - myassert( (sz_recv = mq_receive(mq_test, recv, bufsiz, &prio)) > 0 ); + myassert( mq_send(mq_test, buf, bufsiz, 0) == 0, 0x4 ); + myassert( (sz_recv = mq_receive(mq_test, recv, bufsiz, &prio)) > 0, 0x8 ); memset(recv, '\0', bufsiz); if (fork() > 0) { - myassert( (mq_recv = mq_open( "/testmq", O_RDONLY, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); - myassert( (sz_recv = mq_receive(mq_recv, recv, bufsiz, &prio)) > 0 ); + myassert( (mq_recv = mq_open( "/testmq", O_RDONLY, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1, 0x10 ); + myassert( (sz_recv = mq_receive(mq_recv, recv, bufsiz, &prio)) > 0, 0x20 ); return ret; } - myassert( mq_send(mq_test, buf, bufsiz, 0) == 0 ); + myassert( mq_send(mq_test, buf, bufsiz, 0) == 0, 0x40 ); wait(&c_stat); - myassert( c_stat == 0xFF ); - myassert( mq_close(mq_test) == 0 ); - myassert( mq_unlink("/testmq") == 0 ); + myassert( c_stat == 0xFF, 0x80 ); + myassert( mq_close(mq_test) == 0, 0x100 ); + myassert( mq_unlink("/testmq") == 0, 0x200 ); + + myassert( sem_unlink("/testsem") == 0, 0x400 ); + myassert( (sp_test = sem_open("/testsem", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)), 0x800 ); + myassert( sem_post(sp_test) == 0, 0x1000 ); + myassert( sem_wait(sp_test) == 0, 0x1200 ); + myassert( sem_close(sp_test) == 0, 0x1400 ); + myassert( sem_unlink("/testsem") == 0, 0x1800 ); - return ~ret; + return ret; } diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..93d6532 --- /dev/null +++ b/src/log.c @@ -0,0 +1,19 @@ +#include +#include + +#include "log.h" + + +static int logfd; + +int log_init(char *file) +{ + logfd = fopen(file, "a+"); + return logfd; +} + +int logs(char *format, ...) +{ + +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..7b4affc --- /dev/null +++ b/src/log.h @@ -0,0 +1,8 @@ +#ifndef LOG_H +#define LOG_H 1 + +int log_init(char *file); + +int logs(char *format, ...); + +#endif diff --git a/src/main.c b/src/main.c index 31afd29..1065371 100644 --- a/src/main.c +++ b/src/main.c @@ -73,7 +73,7 @@ run_cryptcreate(char *pass, char *crypt_cmd) char *cmd; if (crypt_cmd == NULL || pass == NULL) return (-1); - asprintf(&cmd, "echo '%s' | %s >/dev/null 2>/dev/null", pass, crypt_cmd); + asprintf(&cmd, "echo '%s' | %s", pass, crypt_cmd); retval = system(cmd); free(cmd); return (retval); @@ -112,7 +112,12 @@ main(int argc, char **argv) } memset(pbuf, '\0', IPC_MQSIZ+1); - parse_cmd(argc, argv); + if ( parse_cmd(argc, argv) != 0 ) + goto error; + if (OPT(CRYPT_CMD).found == 0) { + usage(argv[0]); + goto error; + } if (check_fifo(GETOPT(FIFO_PATH).str) == false) { usage(argv[0]); goto error; @@ -164,7 +169,10 @@ main(int argc, char **argv) break; } } - wait(&c_status); + waitpid(child, &c_status, WNOHANG); + if ( WIFEXITED(c_status) == 0 ) { + wait(&c_status); + } memset(pbuf, '\0', IPC_MQSIZ+1); } else { /* fork error */ diff --git a/src/opt.c b/src/opt.c index 7b6d083..19d8879 100644 --- a/src/opt.c +++ b/src/opt.c @@ -39,6 +39,9 @@ parse_cmd(int argc, char **argv) case 'c': s_OPT(CRYPT_CMD, strdup(optarg)); break; + default: + usage(argv[0]); + return 1; } } return 0; -- cgit v1.2.3 From 2decdd502f1c9b33800cc41daff25536066f7560 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 25 Jan 2016 04:32:53 +0100 Subject: early logging --- src/log.c | 24 ++++++++++++++++++------ src/log.h | 4 ++-- src/opt.c | 4 ++-- src/opt.h | 3 ++- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/log.c b/src/log.c index 93d6532..06513cf 100644 --- a/src/log.c +++ b/src/log.c @@ -1,19 +1,31 @@ #include #include +#include +#include #include "log.h" +#define LOG_BUFSIZ 128 -static int logfd; +static FILE* logfile; -int log_init(char *file) + +int log_init(char* file) { - logfd = fopen(file, "a+"); - return logfd; + logfile = fopen(file, "a+"); + return (logfile ? 0 : errno); } -int logs(char *format, ...) +int logs(char* format, ...) { - + int ret; + char* buf; + va_list vargs; + + buf = (char*) calloc(LOG_BUFSIZ, sizeof(char)); + va_start(vargs, format); + ret = vsnprintf(buf, LOG_BUFSIZ, format, vargs); + va_end(vargs); + return ret; } diff --git a/src/log.h b/src/log.h index 7b4affc..aad0bfb 100644 --- a/src/log.h +++ b/src/log.h @@ -1,8 +1,8 @@ #ifndef LOG_H #define LOG_H 1 -int log_init(char *file); +int log_init(char* file); -int logs(char *format, ...); +int logs(char* format, ...); #endif diff --git a/src/opt.c b/src/opt.c index 19d8879..76b17e7 100644 --- a/src/opt.c +++ b/src/opt.c @@ -9,7 +9,7 @@ #define CONFIG_OPT(default_val) { {0},0,{default_val} } -struct opt config_opts[] = { CONFIG_OPT(DEFAULT_FIFO), CONFIG_OPT(NULL) }; +struct opt config_opts[] = { CONFIG_OPT(DEFAULT_FIFO), CONFIG_OPT(NULL), CONFIG_OPT("/tmp/naskpass.log") }; const int opt_siz = ( sizeof(config_opts)/sizeof(config_opts[0]) ); @@ -20,7 +20,7 @@ usage(char *arg0) fprintf(stderr, " Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL); fprintf(stderr, " License GPLv3+: GNU GPL version 3 or later .\n\n"); fprintf(stderr, " Command:\n\t%s [args]\n", arg0); - fprintf(stderr, " Arguments:\n\t-h this\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", DEFAULT_FIFO); + fprintf(stderr, " Arguments:\n\t-h this\n\t-l [logfile] default: %s\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", GETOPT(LOG_FILE), GETOPT(FIFO_PATH)); } int diff --git a/src/opt.h b/src/opt.h index a5fd37f..102dc68 100644 --- a/src/opt.h +++ b/src/opt.h @@ -20,7 +20,8 @@ struct opt { enum opt_index { FIFO_PATH = 0, - CRYPT_CMD + CRYPT_CMD, + LOG_FILE }; -- cgit v1.2.3 From fbd15154bea20f5b29e0d193b60f648891684f8a Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 30 Jan 2016 00:17:25 +0100 Subject: early logging 2 --- src/log.c | 16 ++++++++++++---- src/log.h | 2 ++ src/main.c | 12 ++++++++++-- src/opt.c | 13 ++++++++----- src/opt.h | 7 +++---- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/log.c b/src/log.c index 06513cf..ea52fda 100644 --- a/src/log.c +++ b/src/log.c @@ -7,24 +7,32 @@ #define LOG_BUFSIZ 128 -static FILE* logfile; +static FILE* logfile = NULL; int log_init(char* file) { + if (!file) return -1; logfile = fopen(file, "a+"); return (logfile ? 0 : errno); } +void log_free(void) +{ + if (logfile) + fclose(logfile); + logfile = NULL; +} + int logs(char* format, ...) { int ret; - char* buf; va_list vargs; - buf = (char*) calloc(LOG_BUFSIZ, sizeof(char)); + if (!logfile) return -1; va_start(vargs, format); - ret = vsnprintf(buf, LOG_BUFSIZ, format, vargs); + ret = vfprintf(logfile, format, vargs); + fflush(logfile); va_end(vargs); return ret; } diff --git a/src/log.h b/src/log.h index aad0bfb..96cb82d 100644 --- a/src/log.h +++ b/src/log.h @@ -3,6 +3,8 @@ int log_init(char* file); +void log_free(void); + int logs(char* format, ...); #endif diff --git a/src/main.c b/src/main.c index 1065371..e36982f 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,7 @@ #include "config.h" #include "opt.h" +#include "log.h" #include "ui.h" #include "ui_ipc.h" @@ -114,12 +115,13 @@ main(int argc, char **argv) memset(pbuf, '\0', IPC_MQSIZ+1); if ( parse_cmd(argc, argv) != 0 ) goto error; + log_init( GETOPT(LOG_FILE).str ); + logs("%s\n", "log init"); if (OPT(CRYPT_CMD).found == 0) { - usage(argv[0]); + fprintf(stderr, "%s: crypt cmd is mandatory\n", argv[0]); goto error; } if (check_fifo(GETOPT(FIFO_PATH).str) == false) { - usage(argv[0]); goto error; } if ((ffd = open(GETOPT(FIFO_PATH).str, O_NONBLOCK | O_RDWR)) < 0) { @@ -130,6 +132,7 @@ main(int argc, char **argv) ui_ipc_sempost(SEM_UI); if ((child = fork()) == 0) { /* child */ + logs("%s\n", "child"); if (ffd >= 0) close(ffd); fclose(stderr); /* Slave process: TUI */ @@ -140,6 +143,7 @@ main(int argc, char **argv) exit(0); } else if (child > 0) { /* parent */ + logs("%s\n", "parent"); fclose(stdin); fclose(stdout); ui_ipc_sempost(SEM_BS); @@ -180,9 +184,13 @@ main(int argc, char **argv) goto error; } + logs("%s\n", "finished"); + log_free(); ret = EXIT_SUCCESS; error: if (ffd >= 0) close(ffd); ui_ipc_free(1); + logs("%s\n", "init error"); + log_free(); exit(ret); } diff --git a/src/opt.c b/src/opt.c index 76b17e7..e1ea835 100644 --- a/src/opt.c +++ b/src/opt.c @@ -9,7 +9,7 @@ #define CONFIG_OPT(default_val) { {0},0,{default_val} } -struct opt config_opts[] = { CONFIG_OPT(DEFAULT_FIFO), CONFIG_OPT(NULL), CONFIG_OPT("/tmp/naskpass.log") }; +struct opt config_opts[] = { CONFIG_OPT(DEFAULT_FIFO), CONFIG_OPT(NULL), CONFIG_OPT(NULL) }; const int opt_siz = ( sizeof(config_opts)/sizeof(config_opts[0]) ); @@ -20,7 +20,7 @@ usage(char *arg0) fprintf(stderr, " Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL); fprintf(stderr, " License GPLv3+: GNU GPL version 3 or later .\n\n"); fprintf(stderr, " Command:\n\t%s [args]\n", arg0); - fprintf(stderr, " Arguments:\n\t-h this\n\t-l [logfile] default: %s\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", GETOPT(LOG_FILE), GETOPT(FIFO_PATH)); + fprintf(stderr, " Arguments:\n\t-h this\n\t-l [logfile]\n\t-f [passfifo] default: %s\n\t-c [cryptcreate]\n", GETOPT(FIFO_PATH).str); } int @@ -28,16 +28,19 @@ parse_cmd(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "hf:c:")) != -1) { + while ((opt = getopt(argc, argv, "hf:c:l:")) != -1) { switch (opt) { case 'h': usage(argv[0]); return 1; case 'f': - s_OPT(FIFO_PATH, strdup(optarg)); + SETOPT_str(FIFO_PATH, strdup(optarg)); break; case 'c': - s_OPT(CRYPT_CMD, strdup(optarg)); + SETOPT_str(CRYPT_CMD, strdup(optarg)); + break; + case 'l': + SETOPT_str(LOG_FILE, strdup(optarg)); break; default: usage(argv[0]); diff --git a/src/opt.h b/src/opt.h index 102dc68..57777de 100644 --- a/src/opt.h +++ b/src/opt.h @@ -2,14 +2,13 @@ #define OPT_H 1 #define OPT(opt_index) config_opts[opt_index] +#define SETOPT_str(opt_index, value) { OPT(opt_index).found = 1; OPT(opt_index).opt.str = value; } #define GETOPT(opt_index) (OPT(opt_index).found != 0 ? OPT(opt_index).opt : OPT(opt_index).def) -#define OPT_USED(opt_index, uvalue) OPT(opt_index).found = uvalue; -#define d_OPT(opt_index, rvalue) OPT(opt_index).opt.dec = rvalue; OPT_USED(opt_index, 1); -#define s_OPT(opt_index, rvalue) OPT(opt_index).opt.str = rvalue; OPT_USED(opt_index, 1); + union opt_entry { char *str; - int *dec; + int dec; }; struct opt { -- cgit v1.2.3 From 0c79886240b68ae9ed144cce5bd2167996859239 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 19 Mar 2016 16:16:16 +0100 Subject: initscript old askpass fallback --- scripts/naskpass.initscript | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/naskpass.initscript b/scripts/naskpass.initscript index fa22318..5540667 100755 --- a/scripts/naskpass.initscript +++ b/scripts/naskpass.initscript @@ -290,7 +290,7 @@ setup_mapping() # Plymouth will add a : if it is a non-graphical prompt cryptkey="Please unlock disk $diskname" else - if [ -x /lib/cryptsetup/naskpass ]; then + if [ -x /lib/cryptsetup/naskpass ] && [ $askpass_fallback -eq 0 ]; then cryptkeyscript="/lib/cryptsetup/naskpass" cryptkey="" else @@ -302,7 +302,7 @@ setup_mapping() if [ ! -e "$NEWROOT" ]; then - if [ -x /bin/plymouth ] && plymouth --ping; then + if [ -x /bin/plymouth ] && plymouth --ping || [ $askpass_fallback -ne 0 ]; then message "naskpass does not work with playmouth, falling back to default askpass .." if ! crypttarget="$crypttarget" cryptsource="$cryptsource" \ $cryptkeyscript "$cryptkey" | $cryptopen; then @@ -383,6 +383,7 @@ setup_mapping() # # Do we have any kernel boot arguments? +askpass_fallback=0 cmdline_cryptopts='' unset cmdline_root for opt in $(cat /proc/cmdline); do @@ -406,6 +407,9 @@ for opt in $(cat /proc/cmdline); do *) # lilo major/minor number (See #398957). Ignore esac ;; + cryptfallback) + askpass_fallback=1 + ;; esac done -- cgit v1.2.3 From 6e5817332883a2bf5555a7a63b33fd7221482e12 Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 19 Mar 2016 17:14:39 +0100 Subject: fixed missing makefile.am object and initscript conditional plymouth msg --- scripts/naskpass.initscript | 4 +++- src/Makefile.am | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/naskpass.initscript b/scripts/naskpass.initscript index 5540667..98fd1dc 100755 --- a/scripts/naskpass.initscript +++ b/scripts/naskpass.initscript @@ -303,7 +303,9 @@ setup_mapping() if [ ! -e "$NEWROOT" ]; then if [ -x /bin/plymouth ] && plymouth --ping || [ $askpass_fallback -ne 0 ]; then - message "naskpass does not work with playmouth, falling back to default askpass .." + if [ $askpass_fallback -eq 0 ]; then + message "naskpass does not work with plymouth, falling back to default askpass .." + fi if ! crypttarget="$crypttarget" cryptsource="$cryptsource" \ $cryptkeyscript "$cryptkey" | $cryptopen; then message "cryptsetup: cryptsetup failed, bad password or options?" diff --git a/src/Makefile.am b/src/Makefile.am index 305406e..e6f5c91 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,3 @@ bin_PROGRAMS=naskpass naskpass_check -naskpass_SOURCES=main.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_txtwindow.c ui_statusbar.c +naskpass_SOURCES=main.c log.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_txtwindow.c ui_statusbar.c naskpass_check_SOURCES=check/check.c -- cgit v1.2.3 From 0b20f315b07be0ae178d859c98576ae34330ff54 Mon Sep 17 00:00:00 2001 From: toni Date: Fri, 25 Mar 2016 17:19:30 +0100 Subject: fixed re-install debconf bug; logging stuff --- debian/naskpass.postinst | 4 ++-- src/main.c | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/debian/naskpass.postinst b/debian/naskpass.postinst index 590a2ab..4d9cc42 100755 --- a/debian/naskpass.postinst +++ b/debian/naskpass.postinst @@ -12,7 +12,7 @@ case "$1" in configure) nask_update - db_input high naskpass/activate + db_input high naskpass/activate || true db_go db_get naskpass/activate if [ "x$RET" = "xtrue" ]; then @@ -27,7 +27,7 @@ case "$1" in update-initramfs -u ;; - abort-upgrade) + install|upgrade|abort-upgrade) ;; *) diff --git a/src/main.c b/src/main.c index e36982f..ce34a41 100644 --- a/src/main.c +++ b/src/main.c @@ -170,11 +170,13 @@ main(int argc, char **argv) usleep(100000); waitpid(child, &c_status, WNOHANG); if ( WIFEXITED(c_status) != 0 ) { + logs("%s\n", "child exited"); break; } } waitpid(child, &c_status, WNOHANG); if ( WIFEXITED(c_status) == 0 ) { + logs("%s\n", "waiting for child"); wait(&c_status); } memset(pbuf, '\0', IPC_MQSIZ+1); -- cgit v1.2.3 From 3ddc8f8a7531208ffd867489c34f4c74e1acd667 Mon Sep 17 00:00:00 2001 From: toni Date: Sun, 3 Jul 2016 17:41:09 +0200 Subject: wait(...) bug fixed --- src/main.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index ce34a41..6cd2eea 100644 --- a/src/main.c +++ b/src/main.c @@ -174,11 +174,8 @@ main(int argc, char **argv) break; } } - waitpid(child, &c_status, WNOHANG); - if ( WIFEXITED(c_status) == 0 ) { - logs("%s\n", "waiting for child"); - wait(&c_status); - } + logs("%s\n", "waiting for child"); + wait(&child); memset(pbuf, '\0', IPC_MQSIZ+1); } else { /* fork error */ -- cgit v1.2.3 From a5026891c37cc19f27cc311252d7d5e8056bc337 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jul 2016 00:14:25 +0200 Subject: wait(...) fail fixed --- .gitignore | 12 ------------ src/main.c | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index de07cbd..ab9c734 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,3 @@ -/naskpass -/naskpass_check -/src/version.h -/tests/producer -/tests/producer_consumer -/tests/semtest -/tests/semtest2 -/tests/mqtest -/tests/strsep -/tests/pthread -/tests/ncurses *.swp *.o *.d - diff --git a/src/main.c b/src/main.c index 6cd2eea..f2a435d 100644 --- a/src/main.c +++ b/src/main.c @@ -175,7 +175,7 @@ main(int argc, char **argv) } } logs("%s\n", "waiting for child"); - wait(&child); + wait(&c_status); memset(pbuf, '\0', IPC_MQSIZ+1); } else { /* fork error */ -- cgit v1.2.3 From 3a8e6b979f6a741342546622477d8bd4e8483cce Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jul 2016 01:35:41 +0200 Subject: fixed SEM_UI post bug --- src/main.c | 7 ++++++- src/ui_elements.c | 17 +++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index f2a435d..916bf4d 100644 --- a/src/main.c +++ b/src/main.c @@ -129,7 +129,6 @@ main(int argc, char **argv) goto error; } - ui_ipc_sempost(SEM_UI); if ((child = fork()) == 0) { /* child */ logs("%s\n", "child"); @@ -158,14 +157,19 @@ main(int argc, char **argv) } } else if ( ui_ipc_msgcount(MQ_PW) > 0 ) { ui_ipc_msgrecv(MQ_PW, pbuf); + logs("%s", "password, "); ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { + logs("%s", "cryptcreate error, "); ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); } else { + logs("%s", "cryptcreate success, trywait SEM_UI, "); ui_ipc_semtrywait(SEM_UI); } + logs("%s", "wait SEM_IN, "); ui_ipc_semwait(SEM_IN); } + logs("%s", "wait SEM_BS, "); ui_ipc_semwait(SEM_BS); usleep(100000); waitpid(child, &c_status, WNOHANG); @@ -173,6 +177,7 @@ main(int argc, char **argv) logs("%s\n", "child exited"); break; } + logs("loop end (SEM_BS=%d, SEM_IN=%d, SEM_UI=%d).\n", ui_ipc_getvalue(SEM_BS), ui_ipc_getvalue(SEM_IN), ui_ipc_getvalue(SEM_UI)); } logs("%s\n", "waiting for child"); wait(&c_status); diff --git a/src/ui_elements.c b/src/ui_elements.c index 535bc0c..f072d45 100644 --- a/src/ui_elements.c +++ b/src/ui_elements.c @@ -87,15 +87,16 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) ui_thrd_force_update(); sleep(2); - ui_ipc_msgrecv(MQ_IF, ipc_buf); - - ui_thrd_suspend(); - set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); - set_txtwindow_title(infownd, "ERROR"); - set_txtwindow_text(infownd, ipc_buf); - ui_thrd_resume(); - while (wgetch(stdscr) != '\n') { }; + if (ui_ipc_msgcount(MQ_IF) > 0) { + ui_ipc_msgrecv(MQ_IF, ipc_buf); + ui_thrd_suspend(); + set_txtwindow_color(infownd, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD); + set_txtwindow_title(infownd, "ERROR"); + set_txtwindow_text(infownd, ipc_buf); + ui_thrd_resume(); + while (wgetch(stdscr) != '\n') { }; + } ui_thrd_suspend(); set_txtwindow_active(infownd, false); -- cgit v1.2.3 From ac97850fb2b8c1e4efbaf782bf8afbc4b3d6a058 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jul 2016 10:01:34 +0200 Subject: syslog && compile script --- compile.sh | 11 +++++++++++ configure.ac | 4 ++-- src/aconfig.h.in | 15 ++++++++++++--- src/log.c | 28 ++++++++++++++++++++-------- src/opt.c | 6 ++++-- 5 files changed, 49 insertions(+), 15 deletions(-) create mode 100755 compile.sh diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..3db651f --- /dev/null +++ b/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -e + +DIR=$(dirname $0) +PWD=$(pwd) + +cd ${DIR} +./autogen.sh +./configure +make +cd ${PWD} diff --git a/configure.ac b/configure.ac index 5ea94ee..740f02a 100644 --- a/configure.ac +++ b/configure.ac @@ -34,7 +34,7 @@ AC_HEADER_STDC AC_HEADER_STAT AC_HEADER_DIRENT AC_HEADER_ASSERT -AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h alloca.h]) +AC_CHECK_HEADERS([stdio.h stdlib.h stdbool.h string.h unistd.h errno.h sys/stat.h sys/types.h sys/wait.h fcntl.h semaphore.h time.h mqueue.h syslog.h],[],[AC_MSG_ERROR([*** missing essential header files])]) # Checks for typedefs, structures, and compiler characteristics. AC_COMPUTE_INT @@ -57,7 +57,7 @@ AC_FUNC_STRNLEN AC_FUNC_STAT AC_FUNC_MKTIME AC_FUNC_VPRINTF -AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol],,[AC_MSG_ERROR([*** Missing essential functions.])]) +AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol openlog vsyslog closelog],,[AC_MSG_ERROR([*** Missing essential functions.])]) AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" diff --git a/src/aconfig.h.in b/src/aconfig.h.in index a24ff8d..8dcbf12 100644 --- a/src/aconfig.h.in +++ b/src/aconfig.h.in @@ -3,9 +3,6 @@ /* Define to 1 if you have the `alarm' function. */ #undef HAVE_ALARM -/* Define to 1 if you have the header file. */ -#undef HAVE_ALLOCA_H - /* Define to 1 if you have the `asprintf' function. */ #undef HAVE_ASPRINTF @@ -15,6 +12,9 @@ /* Define to 1 if you have the `close' function. */ #undef HAVE_CLOSE +/* Define to 1 if you have the `closelog' function. */ +#undef HAVE_CLOSELOG + /* Do NOT change THIS! */ #undef HAVE_CONFIG @@ -74,6 +74,9 @@ /* Define to 1 if you have the `open' function. */ #undef HAVE_OPEN +/* Define to 1 if you have the `openlog' function. */ +#undef HAVE_OPENLOG + /* Define to 1 if you have the `printf' function. */ #undef HAVE_PRINTF @@ -130,6 +133,9 @@ /* Define to 1 if you have the `strtol' function. */ #undef HAVE_STRTOL +/* Define to 1 if you have the header file. */ +#undef HAVE_SYSLOG_H + /* Define to 1 if you have the `system' function. */ #undef HAVE_SYSTEM @@ -171,6 +177,9 @@ /* Define to 1 if you have the `vprintf' function. */ #undef HAVE_VPRINTF +/* Define to 1 if you have the `vsyslog' function. */ +#undef HAVE_VSYSLOG + /* Define to 1 if `fork' works. */ #undef HAVE_WORKING_FORK diff --git a/src/log.c b/src/log.c index ea52fda..cd8fc7c 100644 --- a/src/log.c +++ b/src/log.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "log.h" @@ -12,16 +13,23 @@ static FILE* logfile = NULL; int log_init(char* file) { - if (!file) return -1; - logfile = fopen(file, "a+"); - return (logfile ? 0 : errno); + if (!file) { + openlog("naskpass", LOG_NDELAY | LOG_PID, LOG_DAEMON); + return 0; + } else { + logfile = fopen(file, "a+"); + return (logfile ? 0 : errno); + } } void log_free(void) { - if (logfile) + if (!logfile) { + closelog(); + } else { fclose(logfile); - logfile = NULL; + logfile = NULL; + } } int logs(char* format, ...) @@ -29,10 +37,14 @@ int logs(char* format, ...) int ret; va_list vargs; - if (!logfile) return -1; va_start(vargs, format); - ret = vfprintf(logfile, format, vargs); - fflush(logfile); + if (!logfile) { + vsyslog(LOG_DEBUG, format, vargs); + ret = 0; + } else { + ret = vfprintf(logfile, format, vargs); + fflush(logfile); + } va_end(vargs); return ret; } diff --git a/src/opt.c b/src/opt.c index e1ea835..0c2071a 100644 --- a/src/opt.c +++ b/src/opt.c @@ -28,7 +28,7 @@ parse_cmd(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "hf:c:l:")) != -1) { + while ((opt = getopt(argc, argv, "hf:c:l::")) != -1) { switch (opt) { case 'h': usage(argv[0]); @@ -40,7 +40,9 @@ parse_cmd(int argc, char **argv) SETOPT_str(CRYPT_CMD, strdup(optarg)); break; case 'l': - SETOPT_str(LOG_FILE, strdup(optarg)); + if (optarg) { + SETOPT_str(LOG_FILE, strdup(optarg)); + } else SETOPT_str(LOG_FILE, NULL); break; default: usage(argv[0]); -- cgit v1.2.3 From 3b40a0f20ce33de44f47fd56c818323185ad091d Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jul 2016 13:29:45 +0200 Subject: autoconf: debug mode --- compile.sh | 2 +- configure.ac | 13 ++++++++++++- src/Makefile.am | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compile.sh b/compile.sh index 3db651f..5c7b1d6 100755 --- a/compile.sh +++ b/compile.sh @@ -6,6 +6,6 @@ PWD=$(pwd) cd ${DIR} ./autogen.sh -./configure +./configure $* make cd ${PWD} diff --git a/configure.ac b/configure.ac index 740f02a..cef76e0 100644 --- a/configure.ac +++ b/configure.ac @@ -13,7 +13,7 @@ AC_CONFIG_SRCDIR([src/aconfig.h.in]) AC_CONFIG_HEADER([src/aconfig.h]) AC_CONFIG_MACRO_DIR([m4]) AC_USE_SYSTEM_EXTENSIONS -CFLAGS="-Os" +CFLAGS="" LDFLAGS="" # Checks for programs. @@ -59,6 +59,17 @@ AC_FUNC_MKTIME AC_FUNC_VPRINTF AC_CHECK_FUNCS([clock_gettime asprintf system printf fprintf mkfifo stat open close fork memmove memcpy memset strdup strndup strerror strstr strlen strnlen strtol openlog vsyslog closelog],,[AC_MSG_ERROR([*** Missing essential functions.])]) +AC_ARG_ENABLE(debug, +AS_HELP_STRING([--enable-debug], + [enable debugging, default: no]), +[case "${enableval}" in + yes) debug=true ;; + no) debug=false ;; + *) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;; +esac], +[debug=false]) +AM_CONDITIONAL(DEBUG, test x"$debug" = x"true") + AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!]) LDFLAGS="${LDFLAGS} -pthread -lrt -lncurses" AC_SUBST([AM_CFLAGS]) diff --git a/src/Makefile.am b/src/Makefile.am index e6f5c91..44d2220 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,8 @@ bin_PROGRAMS=naskpass naskpass_check naskpass_SOURCES=main.c log.c opt.c status.c ui_ani.c ui.c ui_elements.c ui_input.c ui_ipc.c ui_txtwindow.c ui_statusbar.c naskpass_check_SOURCES=check/check.c +if DEBUG +naskpass_CFLAGS=-O0 -g3 -DDEBUG +else +naskpass_CFLAGS=-Os +endif -- cgit v1.2.3 From 7824c509b894319d8e92336621656c0d0696f276 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jul 2016 13:30:19 +0200 Subject: ui_ipc test unit --- tests/Makefile | 3 +++ tests/ipctest.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/ipctest.c diff --git a/tests/Makefile b/tests/Makefile index a8b0e29..9350e8e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -10,6 +10,9 @@ all: $(BINARIES) %: %.c $(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<) +ipctest: ipctest.c ../src/ui_ipc.c + $(CC) $(CFLAGS) $(LDFLAGS) ipctest.c ../src/ui_ipc.c -o ipctest + clean: rm -f $(DEPS) rm -f $(BINARIES) diff --git a/tests/ipctest.c b/tests/ipctest.c new file mode 100644 index 0000000..bd7c620 --- /dev/null +++ b/tests/ipctest.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "semconfig.h" +#include "../src/ui_ipc.h" + + + +int main(int argc, char **argv) { + int ret, c_status; + pid_t child; + + ret = ui_ipc_init(1); + + ret |= ui_ipc_sempost(SEM_BS); + ret |= ui_ipc_sempost(SEM_BS); + if ( (child = fork()) == 0 ) { + printf("child: wait (%d)\n", ui_ipc_getvalue(SEM_BS)); + ret |= ui_ipc_semtrywait(SEM_BS); + ret |= ui_ipc_semtrywait(SEM_BS); + ret |= ui_ipc_semwait(SEM_BS); + printf("child: done (%d)\n", ui_ipc_getvalue(SEM_BS)); + ret |= ui_ipc_sempost(SEM_BS); + exit( (ret == 0 ? 0 : ret) ); + } else if (child > 0) { + usleep(100000); + printf("parent: post (%d)\n", ui_ipc_getvalue(SEM_BS)); + ui_ipc_sempost(SEM_BS); + } else { + ret |= 1; + } + + wait(&c_status); + ret |= ui_ipc_semtrywait(SEM_BS); + ui_ipc_free(1); + exit( c_status | ret ); +} + -- cgit v1.2.3 From bc30ed7f5624f7d5ccc1e9937ed7bcb7faae9892 Mon Sep 17 00:00:00 2001 From: toni Date: Mon, 4 Jul 2016 15:26:25 +0200 Subject: luks test script, fixed process/thread sync bug --- luks_test.sh | 27 +++++++++++++++++++++++++++ src/log.h | 6 ++++++ src/main.c | 28 ++++++++++++++-------------- 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100755 luks_test.sh diff --git a/luks_test.sh b/luks_test.sh new file mode 100755 index 0000000..ebe622c --- /dev/null +++ b/luks_test.sh @@ -0,0 +1,27 @@ +#!/bin/sh +set -e + + +if [ "x$1" = "x" ] || [ "x$2" = "x" ]; then + echo "$0: [FILE] [NAME]" + exit 1 +fi +FILE=$1 +NAME=$2 + +if [ ! -w ${FILE} ]; then + dd if=/dev/zero of=${FILE} bs=1M count=10 + /sbin/cryptsetup luksFormat ${FILE} +fi + +sudo src/naskpass -l -f ./testfifo -c "/sbin/cryptsetup open ${FILE} ${NAME}" + +set +e +sudo /sbin/cryptsetup status ${NAME} +ret=$? +set -e + +if [ $ret -eq 0 ]; then + sudo /sbin/cryptsetup close ${NAME} + /bin/echo -e "\n$0: close'd" +fi diff --git a/src/log.h b/src/log.h index 96cb82d..113141b 100644 --- a/src/log.h +++ b/src/log.h @@ -1,6 +1,12 @@ #ifndef LOG_H #define LOG_H 1 +#ifdef DEBUG +#define logs_dbg(fmt, ...) logs(fmt, __VA_ARGS__) +#else +#define logs_dbg(fmt, ...) +#endif + int log_init(char* file); void log_free(void); diff --git a/src/main.c b/src/main.c index 916bf4d..523422f 100644 --- a/src/main.c +++ b/src/main.c @@ -74,7 +74,7 @@ run_cryptcreate(char *pass, char *crypt_cmd) char *cmd; if (crypt_cmd == NULL || pass == NULL) return (-1); - asprintf(&cmd, "echo '%s' | %s", pass, crypt_cmd); + asprintf(&cmd, "echo '%s' | %s >/devnull 2>/dev/null", pass, crypt_cmd); retval = system(cmd); free(cmd); return (retval); @@ -117,6 +117,7 @@ main(int argc, char **argv) goto error; log_init( GETOPT(LOG_FILE).str ); logs("%s\n", "log init"); + logs_dbg("%s\n", "debug mode active"); if (OPT(CRYPT_CMD).found == 0) { fprintf(stderr, "%s: crypt cmd is mandatory\n", argv[0]); goto error; @@ -129,6 +130,7 @@ main(int argc, char **argv) goto error; } + ui_ipc_sempost(SEM_UI); if ((child = fork()) == 0) { /* child */ logs("%s\n", "child"); @@ -145,10 +147,10 @@ main(int argc, char **argv) logs("%s\n", "parent"); fclose(stdin); fclose(stdout); - ui_ipc_sempost(SEM_BS); - ui_ipc_sempost(SEM_UI); /* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */ - while ( ui_ipc_getvalue(SEM_UI) > 0 || ui_ipc_getvalue(SEM_IN) > 0 ) { + ui_ipc_sempost(SEM_BS); + while ( ui_ipc_getvalue(SEM_UI) > 0 ) { + logs_dbg("loop start (SEM_BS=%d, SEM_IN=%d, SEM_UI=%d).\n", ui_ipc_getvalue(SEM_BS), ui_ipc_getvalue(SEM_IN), ui_ipc_getvalue(SEM_UI)); ui_ipc_sempost(SEM_BS); if (read(ffd, pbuf, IPC_MQSIZ) >= 0) { ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD)); @@ -157,27 +159,27 @@ main(int argc, char **argv) } } else if ( ui_ipc_msgcount(MQ_PW) > 0 ) { ui_ipc_msgrecv(MQ_PW, pbuf); - logs("%s", "password, "); + logs_dbg("%s\n", "password"); ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { - logs("%s", "cryptcreate error, "); + logs_dbg("%s\n", "cryptcreate error"); ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); } else { - logs("%s", "cryptcreate success, trywait SEM_UI, "); + logs_dbg("%s\n", "cryptcreate success, trywait SEM_UI"); ui_ipc_semtrywait(SEM_UI); } - logs("%s", "wait SEM_IN, "); + logs_dbg("%s\n", "wait SEM_IN"); ui_ipc_semwait(SEM_IN); } - logs("%s", "wait SEM_BS, "); + logs_dbg("%s (SEM_BS=%d)\n", "wait SEM_BS", ui_ipc_getvalue(SEM_BS)); ui_ipc_semwait(SEM_BS); usleep(100000); waitpid(child, &c_status, WNOHANG); - if ( WIFEXITED(c_status) != 0 ) { + if ( WIFEXITED(&c_status) != 0 ) { logs("%s\n", "child exited"); break; } - logs("loop end (SEM_BS=%d, SEM_IN=%d, SEM_UI=%d).\n", ui_ipc_getvalue(SEM_BS), ui_ipc_getvalue(SEM_IN), ui_ipc_getvalue(SEM_UI)); + logs_dbg("loop end (SEM_BS=%d, SEM_IN=%d, SEM_UI=%d).\n", ui_ipc_getvalue(SEM_BS), ui_ipc_getvalue(SEM_IN), ui_ipc_getvalue(SEM_UI)); } logs("%s\n", "waiting for child"); wait(&c_status); @@ -188,13 +190,11 @@ main(int argc, char **argv) goto error; } - logs("%s\n", "finished"); - log_free(); ret = EXIT_SUCCESS; error: + logs("%s\n", "exiting .."); if (ffd >= 0) close(ffd); ui_ipc_free(1); - logs("%s\n", "init error"); log_free(); exit(ret); } -- cgit v1.2.3