~mir-team/mir/chain-simplification

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
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * 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 <http://www.gnu.org/licenses/>.
 *
 * Author: Daniel van Vugt <daniel.van.vugt@canonical.com>
 */

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

static void appendenv(const char* varname, const char* append)
{
    char buf[1024] = "";
    const char* value = append;
    const char* old = getenv(varname);
    if (old != NULL)
    {
        snprintf(buf, sizeof(buf)-1, "%s:%s", old, append);
        buf[sizeof(buf)-1] = '\0';
        value = buf;
    }
    setenv(varname, value, 1);
}

int main(int argc, char** argv)
{
    char path[1024], *dest = path, *dest_max = path+sizeof(path)-1;
    char *pivot = path;
    size_t pivot_max = 0;
    const char *src = argv[0], *name = argv[0];

    (void)argc;
    while (*src && dest < dest_max)
    {
        *dest = *src;
        if (*dest == '/')
        {
            pivot = dest + 1;
            name = src + 1;
        }
        ++src;
        ++dest;
    }
    pivot_max = dest_max - pivot;

    strncpy(pivot, "../lib/client-modules/", pivot_max);
    *dest_max = '\0';
    setenv("MIR_CLIENT_PLATFORM_PATH", path, 1);
    printf("MIR_CLIENT_PLATFORM_PATH=%s\n", path);
    strncpy(pivot+7, "server-modules/", pivot_max-7);
    setenv("MIR_SERVER_PLATFORM_PATH", path, 1);
    printf("MIR_SERVER_PLATFORM_PATH=%s\n", path);

    pivot[6] = '\0';  /* truncate lib/client-modules to just lib */
    appendenv("LD_LIBRARY_PATH", path);
    printf("LD_LIBRARY_PATH=%s\n", getenv("LD_LIBRARY_PATH"));

    snprintf(pivot, pivot_max, EXECUTABLE_FORMAT, name);
    *dest_max = '\0';
    printf("exec=%s\n", path);

    argv[0] = path;
    execv(argv[0], argv);

    fprintf(stderr, "Failed to execute: %s\n", path);
    return 1;
}