aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 7d3995cae3458939e3c00eb8c5fec02254b36bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <semaphore.h>
#include <fcntl.h>

#include "ui_ani.h"
#include "ui_input.h"
#include "ui_statusbar.h"
#include "ui.h"

#define AUTHOR "Toni Uhlig"
#define AUTHOR_EMAIL "matzeton@googlemail.com"
#define PKGNAME "naskpass"
#define PKGDESC "A NCurses replacement for cryptsetup's askpass."
#ifdef _VERSION
#define VERSION _VERSION
#else
#define VERSION "unknown"
#endif

#define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger"



static void
usage(char *arg0)
{
  fprintf(stderr, "%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 <http://gnu.org/licenses/gpl.html>.\n\n");
  fprintf(stderr, "  Command: %s: [passfifo]\n", arg0);
}

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, "stat: %s is not a FIFO\n", fifo_path);
          return (false);
        }
      }
    }
  }
  perror("check_fifo");
  return (false);
}

int main(int argc, char **argv)
{
  pid_t child;
  int status, ffd;

  if (argc != 2) {
    usage(argv[0]);
    exit(EXIT_FAILURE);
  }

  if (check_fifo(argv[1]) == false) {
    exit(EXIT_FAILURE);
  }
  if ((ffd = open(argv[1], O_NONBLOCK)) < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }

  if ((child = vfork()) == 0) {
    if (setsid() == (pid_t)-1) {
      perror("setsid");
      exit (EXIT_FAILURE);
    }
    do_ui();
  } if (child > 0) {
    wait(&status);
  }else {
    return (EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}