diff options
author | toni <matzeton@googlemail.com> | 2016-07-29 15:30:00 +0200 |
---|---|---|
committer | toni <matzeton@googlemail.com> | 2016-07-29 15:30:00 +0200 |
commit | 78e05eba11d08be64460a829e452d99da91800d7 (patch) | |
tree | 7b831495b613f1630039ef6da68095d1f0ac59e1 | |
parent | ca97cbf8cd9daecc8bd80e84e761e539029bac57 (diff) |
foo
-rwxr-xr-x | configure | 96 | ||||
-rw-r--r-- | configure.ac | 30 | ||||
-rw-r--r-- | src/aconfig.h.in | 6 | ||||
-rw-r--r-- | src/config.h | 1 | ||||
-rw-r--r-- | src/main.c | 19 | ||||
-rw-r--r-- | src/ui.c | 11 | ||||
-rw-r--r-- | src/ui.h | 6 | ||||
-rw-r--r-- | src/ui_ipc.c | 22 | ||||
-rw-r--r-- | src/ui_ipc.h | 7 | ||||
-rw-r--r-- | src/ui_nask.c | 44 | ||||
-rw-r--r-- | tests/ipctest.c | 3 |
11 files changed, 206 insertions, 39 deletions
@@ -6568,6 +6568,28 @@ _ACEOF fi +ac_fn_c_check_type "$LINENO" "time_t" "ac_cv_type_time_t" "$ac_includes_default" +if test "x$ac_cv_type_time_t" = xyes; then : + +else + +cat >>confdefs.h <<_ACEOF +#define time_t unsigned int +_ACEOF + +fi + +ac_fn_c_check_type "$LINENO" "pid_t" "ac_cv_type_pid_t" "$ac_includes_default" +if test "x$ac_cv_type_pid_t" = xyes; then : + +else + +cat >>confdefs.h <<_ACEOF +#define pid_t int +_ACEOF + +fi + # Checks for library functions. for ac_header in vfork.h @@ -7561,6 +7583,80 @@ fi done +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for struct timespec" >&5 +$as_echo_n "checking for struct timespec... " >&6; } +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include <time.h> +int +main () +{ +struct timespec tm; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + +cat >>confdefs.h <<_ACEOF +#define HAVE_TIMESPEC 1 +_ACEOF + + +else + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +cat >>confdefs.h <<_ACEOF +#define HAVE_TIMESPEC 0 +_ACEOF + + + +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for struct stat" >&5 +$as_echo_n "checking for struct stat... " >&6; } +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include <sys/stat.h> +int +main () +{ +struct stat st; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } + +cat >>confdefs.h <<_ACEOF +#define HAVE_TIMESPEC 1 +_ACEOF + + +else + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +cat >>confdefs.h <<_ACEOF +#define HAVE_TIMESPEC 0 +_ACEOF + + + +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing initscr" >&5 $as_echo_n "checking for library containing initscr... " >&6; } if ${ac_cv_search_initscr+:} false; then : diff --git a/configure.ac b/configure.ac index 48baed0..f612c1f 100644 --- a/configure.ac +++ b/configure.ac @@ -46,6 +46,8 @@ AC_TYPE_UINT16_T AC_TYPE_UINT8_T AC_CHECK_TYPE(size_t, unsigned int) AC_CHECK_TYPE(ssize_t, unsigned int) +AC_CHECK_TYPE(time_t, unsigned int) +AC_CHECK_TYPE(pid_t, int) # Checks for library functions. AC_FUNC_FORK @@ -58,6 +60,34 @@ AC_FUNC_MKTIME AC_FUNC_VPRINTF AC_CHECK_FUNCS([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_MSG_CHECKING([for struct timespec]) +AC_COMPILE_IFELSE( [AC_LANG_PROGRAM([[#include <time.h>]], + [[struct timespec tm;]], + [[clock_gettime(CLOCK_REALTIME, &tm);]], + [[tm.tv_sec += 10;]])], + [ + AC_MSG_RESULT([yes]) + AC_DEFINE_UNQUOTED([HAVE_TIMESPEC], 1, [Define to 1 if you have struct timespec.]) + ], + [ + AC_MSG_RESULT([no]) + AC_DEFINE_UNQUOTED([HAVE_TIMESPEC], 0, [Define to 1 if you have struct timespec.]) + ] + ) + +AC_MSG_CHECKING([for struct stat]) +AC_COMPILE_IFELSE( [AC_LANG_PROGRAM([[#include <sys/stat.h>]], + [[struct stat st;]])], + [ + AC_MSG_RESULT([yes]) + AC_DEFINE_UNQUOTED([HAVE_TIMESPEC], 1, [Define to 1 if you have struct stat.]) + ], + [ + AC_MSG_RESULT([no]) + AC_DEFINE_UNQUOTED([HAVE_TIMESPEC], 0, [Define to 1 if you have struct stat.]) + ] + ) + AC_SEARCH_LIBS([initscr], [ncurses],,[AC_MSG_ERROR([*** Missing ncurses library. -lncurses])]) AC_SEARCH_LIBS([wattr_on], [ncurses],,[AC_MSG_ERROR([*** Missing ncurses library. -lncurses])]) AC_SEARCH_LIBS([wattr_off], [ncurses],,[AC_MSG_ERROR([*** Missing ncurses library. -lncurses])]) diff --git a/src/aconfig.h.in b/src/aconfig.h.in index 4f80ab6..7cb63b7 100644 --- a/src/aconfig.h.in +++ b/src/aconfig.h.in @@ -162,6 +162,9 @@ /* Define to 1 if you have the <sys/wait.h> header file. */ #undef HAVE_SYS_WAIT_H +/* Define to 1 if you have struct stat. */ +#undef HAVE_TIMESPEC + /* Define to 1 if you have the <time.h> header file. */ #undef HAVE_TIME_H @@ -290,6 +293,9 @@ /* Define to `unsigned int' if <sys/types.h> does not define. */ #undef ssize_t +/* Define to `unsigned int' if <sys/types.h> does not define. */ +#undef time_t + /* Define to `int' if <sys/types.h> doesn't define. */ #undef uid_t diff --git a/src/config.h b/src/config.h index 7da69ee..d20991f 100644 --- a/src/config.h +++ b/src/config.h @@ -6,6 +6,7 @@ #define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger" #define SEM_GUI "/naskpass-gui" +#define SEM_INP "/naskpass-input" #define MSQ_PWD "/naskpass-passwd" #define MSQ_INF "/naskpass-info" @@ -1,5 +1,6 @@ #include <stdio.h> #include <stdlib.h> +#include <stdbool.h> #include <string.h> #include <errno.h> #include <sys/stat.h> @@ -117,6 +118,7 @@ main(int argc, char **argv) logs_dbg("%s\n", "debug mode active"); ui_ipc_sempost(SEM_UI); + ui_ipc_sempost(SEM_IN); if ((child = fork()) == 0) { /* child */ logs("%s\n", "child"); @@ -136,12 +138,20 @@ main(int argc, char **argv) /* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */ while ( ui_ipc_getvalue(SEM_UI) > 0 ) { if (read(ffd, pbuf, IPC_MQSIZ) >= 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY_FD)); + ui_ipc_semwait(SEM_IN); + logs_dbg("%s\n", "fifo password"); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { - ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); + logs_dbg("%s\n", "cryptcreate error"); + sleep(3); + } else { + csetup_ok = true; + logs_dbg("%s\n", "cryptcreate success"); + ui_ipc_semtrywait(SEM_UI); } + ui_ipc_sempost(SEM_IN); } else if ( ui_ipc_msgcount(MQ_PW) > 0 ) { - ui_ipc_msgrecv(MQ_PW, pbuf); + //ui_ipc_semwait(SEM_IN); + ui_ipc_msgrecv(MQ_PW, pbuf, 0); logs_dbg("%s\n", "password"); ui_ipc_msgsend(MQ_IF, MSG(MSG_BUSY)); if (run_cryptcreate(pbuf, GETOPT(CRYPT_CMD).str) != 0) { @@ -149,9 +159,10 @@ main(int argc, char **argv) ui_ipc_msgsend(MQ_IF, MSG(MSG_CRYPTCMD_ERR)); } else { csetup_ok = true; - logs_dbg("%s\n", "cryptcreate success, trywait SEM_UI"); + logs_dbg("%s\n", "cryptcreate success"); ui_ipc_semtrywait(SEM_UI); } + //ui_ipc_sempost(SEM_IN); } usleep(100000); waitpid(child, &c_status, WNOHANG); @@ -36,6 +36,7 @@ static unsigned int cur_x, cur_y; static WINDOW *wnd_main = NULL; static struct nask_ui /* simple linked list to all UI objects */ *nui = NULL, /* current active input */ *active = NULL; +static uicb_update update_callback = NULL; static pthread_t thrd; static int atmout = APP_TIMEOUT; static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER; @@ -161,7 +162,7 @@ process_key(char key) return ret; } -int +static int do_ui_update(bool timed_out) { int retval = UICB_OK; @@ -176,6 +177,10 @@ do_ui_update(bool timed_out) } else if (atmout == 0) { ui_ipc_semtrywait(SEM_UI); } + + if (update_callback) + update_callback(timed_out); + while (cur != NULL) { if (cur->cbs.ui_element != NULL) { if ( cur->cbs.ui_element(cur->wnd, cur->data, timed_out) != UICB_OK) @@ -299,8 +304,10 @@ char ui_wgetch(int timeout) } WINDOW * -init_ui(void) +init_ui(uicb_update on_update_callback) { + if (on_update_callback) + update_callback = on_update_callback; wnd_main = initscr(); max_x = getmaxx(wnd_main); max_y = getmaxy(wnd_main); @@ -29,6 +29,7 @@ typedef int (*uicb_base)(WINDOW *, void *, bool); typedef int (*uicb_input)(WINDOW *, void *, int); +typedef int (*uicb_update)(bool); struct ui_callbacks { @@ -73,9 +74,6 @@ deactivate_ui_input(void *data); bool process_key(char key); -int -do_ui_update(bool timed_out); - void ui_thrd_force_update(bool force_all, bool timedout); @@ -96,7 +94,7 @@ char ui_wgetch(int timeout); int ui_wgetchtest(int timeout, char testchar); WINDOW * -init_ui(void); +init_ui(uicb_update on_update_callback); void free_ui(void); diff --git a/src/ui_ipc.c b/src/ui_ipc.c index c190a11..fa73f28 100644 --- a/src/ui_ipc.c +++ b/src/ui_ipc.c @@ -1,9 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#ifdef SEM_TIMEDWAIT #include <time.h> -#endif #include <semaphore.h> #include <mqueue.h> #include <sys/stat.h> @@ -46,6 +44,7 @@ ui_ipc_init(int is_master) 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 ); return 0; error: return errno; @@ -64,6 +63,7 @@ ui_ipc_free(int is_master) } if (is_master > 0) { sem_unlink(SEM_GUI); + sem_unlink(SEM_INP); mq_unlink(MSQ_PWD); mq_unlink(MSQ_INF); } @@ -98,18 +98,16 @@ ui_ipc_getvalue(enum UI_IPC_SEM e_sp) return sp_val; } -#ifdef SEM_TIMEDWAIT int -ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, int timeout) +ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, time_t tmout) { struct timespec ts; if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { return -1; } - ts.tc_sec += timeout; - return ( sem_timedwait(sems[q_mq], &ts) ); + ts.tv_sec += tmout; + return ( sem_timedwait(sems[e_sp], &ts) ); } -#endif int ui_ipc_msgsend(enum UI_IPC_MSQ e_mq, const char *msg_ptr) @@ -121,9 +119,15 @@ 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) +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, time_t tmout) { - return mq_receive(msqs[e_mq], msg_ptr, IPC_MQSIZ, NULL); + if (tmout > 0) { + struct timespec tm; + clock_gettime(CLOCK_REALTIME, &tm); + tm.tv_sec += tmout; + return mq_timedreceive(msqs[e_mq], msg_ptr, IPC_MQSIZ, NULL, &tm); + } else + return mq_receive(msqs[e_mq], msg_ptr, IPC_MQSIZ, NULL); } long diff --git a/src/ui_ipc.h b/src/ui_ipc.h index 79bdf8d..d366141 100644 --- a/src/ui_ipc.h +++ b/src/ui_ipc.h @@ -9,6 +9,7 @@ enum UI_IPC_SEM { SEM_UI = 0, /* TUI active? */ + SEM_IN, /* FD input available? */ SEM_NUM }; @@ -37,16 +38,14 @@ 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 +ui_ipc_semtimedwait(enum UI_IPC_SEM e_sp, time_t tmout); int 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); +ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, time_t tmout); long ui_ipc_msgcount(enum UI_IPC_MSQ e_mq); diff --git a/src/ui_nask.c b/src/ui_nask.c index 896c834..f1a571d 100644 --- a/src/ui_nask.c +++ b/src/ui_nask.c @@ -87,14 +87,14 @@ passwd_input_cb(WINDOW *wnd, void *data, int key) deactivate_input(pw_input); ui_thrd_resume(); - ui_ipc_msgrecv(MQ_IF, ipc_buf); + ui_ipc_msgrecv(MQ_IF, ipc_buf, 3); show_info_wnd(infownd, "BUSY", ipc_buf, COLOR_PAIR(5), COLOR_PAIR(5), true, false); sleep(2); if (ui_ipc_msgcount(MQ_IF) > 0) { - ui_ipc_msgrecv(MQ_IF, ipc_buf); - show_info_wnd(infownd, "ERROR", ipc_buf, COLOR_PAIR(4), COLOR_PAIR(4), true, true); - while (ui_wgetchtest(1500, '\n') != DOUI_KEY) { }; + ui_ipc_msgrecv(MQ_IF, ipc_buf, 3); + show_info_wnd(infownd, "ERROR", ipc_buf, COLOR_PAIR(4), COLOR_PAIR(4), true, true); + while (ui_wgetchtest(1500, '\n') != DOUI_KEY) { }; } set_txtwindow_active(infownd, false); @@ -130,15 +130,15 @@ static void init_ui_elements(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); + 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); @@ -170,6 +170,20 @@ free_ui_elements(void) } } +static int +on_update_cb(bool timeout) +{ + if ( ui_ipc_getvalue(SEM_IN) <= 0 ) { + attron(COLOR_PAIR(4)); + const char msg[] = "Got a piped password .."; + mvprintw((unsigned int)(ui_get_maxy() / 2)-PASSWD_YRELPOS-4, + (unsigned int)(ui_get_maxx() / 2)-PASSWD_XRELPOS+(strlen(msg)/2), + msg); + attroff(COLOR_PAIR(4)); + } + return UICB_OK; +} + int do_ui(void) { @@ -177,7 +191,7 @@ do_ui(void) int ret = DOUI_ERR; /* init TUI and UI Elements (input field, status bar, etc) */ - if (init_ui()) + if (init_ui(on_update_cb)) init_ui_elements(ui_get_maxx(), ui_get_maxy()); else return DOUI_ERR; @@ -206,9 +220,7 @@ do_ui(void) ui_ipc_semtrywait(SEM_UI); } - ui_thrd_suspend(); - do_ui_update(false); - ui_thrd_resume(); + ui_thrd_force_update(true,false); } stop_ui_thrd(); free_ui_elements(); diff --git a/tests/ipctest.c b/tests/ipctest.c index f84f390..b4e118a 100644 --- a/tests/ipctest.c +++ b/tests/ipctest.c @@ -21,10 +21,13 @@ int main(int argc, char **argv) { ret |= ui_ipc_sempost(SEM_UI); ret |= ui_ipc_sempost(SEM_UI); + assert(ui_ipc_getvalue(SEM_UI) == 2); if ( (child = fork()) == 0 ) { printf("child: wait (%d)\n", ui_ipc_getvalue(SEM_UI)); ret |= ui_ipc_semtrywait(SEM_UI); + assert(ui_ipc_getvalue(SEM_UI) == 1); ret |= ui_ipc_semtrywait(SEM_UI); + assert(ui_ipc_getvalue(SEM_UI) == 0); ret |= ui_ipc_semwait(SEM_UI); printf("child: done (%d)\n", ui_ipc_getvalue(SEM_UI)); ret |= ui_ipc_sempost(SEM_UI); |