summaryrefslogtreecommitdiff
path: root/pkg/launcher/bitmask-launcher.c
blob: b9bda1c2ad07308e14b1c4df0258dc50ba740ef5 (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
/*
 * bitmask-launcher.c
 *
 * part of the bitmask bundle.
 * execute main entrypoint in a child folder inside the bundle.
 *
 * (c) LEAP Encryption Access Project, 2016-2018.
 * License: GPL.
 *
*/

#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#define MAXBUFFSIZE 1024

char* const lib = "/lib";
char* const entrypoint = "app";
char* const linkname = "/proc/self/exe";

int main(int argc, char *argv[])
{
    char buf[MAXBUFFSIZE];
    char pth[MAXBUFFSIZE];
    char *dirc, *dname;
	const size_t bufsize = MAXBUFFSIZE + 1;

    argv[0] = entrypoint;
    buf[0] = 0;
    pth[0] = 0;

    readlink(linkname, buf, bufsize - 1);

    dirc = strdup(buf);
    dname = dirname(dirc);
    strncat(pth, dname, strlen(dname));
    strncat(pth, lib, strlen(lib));

    if (chdir(pth) < 0)
    {
        fprintf(stderr, "error: %s\n", strerror(errno));
    }
    execv(entrypoint, argv);
}