comparison libcommon/backend.c @ 9:c9ef9e91dd8e

new libcommon, initial version
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 06:55:38 +0000
parents
children 9eb5460f51a6
comparison
equal deleted inserted replaced
8:34bbb0585cab 9:c9ef9e91dd8e
1 /*
2 * This module is responsible for launching and connecting
3 * our SIM card communication back end.
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 extern unsigned calypso_fd, pcsc_reader_num;
11
12 static char calypso_be_pathname[] = "/opt/freecalypso/bin/fcsim-calypso-be";
13 static char pcsc_be_pathname[] = "/opt/freecalypso/bin/fc-pcsc-backend";
14
15 static char *backend_prog, *backend_argv[3], backend_optarg[16];
16
17 FILE *cpipeF, *rpipeF;
18
19 static void
20 setup_be_calypso()
21 {
22 backend_prog = calypso_be_pathname;
23 backend_argv[0] = "fcsim-calypso-be";
24 sprintf(backend_optarg, "-C%u", calypso_fd);
25 backend_argv[1] = backend_optarg;
26 backend_argv[2] = 0;
27 }
28
29 static void
30 setup_be_pcsc()
31 {
32 backend_prog = pcsc_be_pathname;
33 backend_argv[0] = "fc-pcsc-backend";
34 sprintf(backend_optarg, "-p%u", pcsc_reader_num);
35 backend_argv[1] = backend_optarg;
36 backend_argv[2] = 0;
37 }
38
39 launch_backend()
40 {
41 int cpipe[2], rpipe[2], rc;
42
43 if (calypso_fd)
44 setup_be_calypso();
45 else
46 setup_be_pcsc();
47 if (pipe(cpipe) < 0 || pipe(rpipe) < 0) {
48 perror("pipe");
49 exit(1);
50 }
51 rc = vfork();
52 if (rc < 0) {
53 perror("vfork for launching back end");
54 exit(1);
55 }
56 if (!rc) {
57 /* we are in the child - prepare to exec the BE */
58 dup2(cpipe[0], 0);
59 dup2(rpipe[1], 1);
60 close(cpipe[0]);
61 close(cpipe[1]);
62 close(rpipe[0]);
63 close(rpipe[1]);
64 /* do the exec */
65 execv(backend_prog, backend_argv);
66 perror(backend_prog);
67 _exit(1);
68 }
69 close(cpipe[0]);
70 close(rpipe[1]);
71 cpipeF = fdopen(cpipe[1], "w");
72 if (!cpipeF) {
73 perror("fdopen");
74 exit(1);
75 }
76 rpipeF = fdopen(rpipe[0], "r");
77 if (!rpipeF) {
78 perror("fdopen");
79 exit(1);
80 }
81 return(0);
82 }