1/**
2 * \file
3 * \brief angler - terminal and session initialization manager
4 *
5 * Each instance of angler starts one new session. The first argument to
6 * angler determines the terminal to associate with the session. The second,
7 * the type of the terminal.
8 *
9 * Did you know that the angler angles for other types of fish?
10 * The angler is sometimes also called sea-devil.
11 */
12
13/*
14 * Copyright (c) 2012, ETH Zurich.
15 * All rights reserved.
16 *
17 * This file is distributed under the terms in the attached LICENSE file.
18 * If you do not find this file, copies can be found by writing to:
19 * ETH Zurich D-INFK, CAB F.78, Universitaetstrasse 6, CH-8092 Zurich,
20 * Attn: Systems Group.
21 */
22
23#include <stdlib.h>
24#include <stdio.h>
25
26#include <barrelfish/barrelfish.h>
27#include <barrelfish/spawn_client.h>
28#include <angler/angler.h>
29
30/**
31 * The shell to start.
32 */
33#define SHELL "fish"
34
35static void start_shell(struct capref session_id, char *terminal_type)
36{
37    errval_t err;
38    coreid_t my_core_id = disp_get_core_id();
39
40    char *shell = SHELL;
41
42    /* setup argv of the shell */
43    char *shell_argv[2];
44    shell_argv[0] = SHELL;
45    shell_argv[1] = NULL;
46
47    /* setup environment of the shell */
48    setenv("TERM", terminal_type, 1);
49
50    /* inherit the session capability */
51    struct capref inheritcn_cap;
52    err = alloc_inheritcn_with_caps(&inheritcn_cap, NULL_CAP, session_id, NULL_CAP);
53    if (err_is_fail(err)) {
54        USER_PANIC_ERR(err, "Error allocating inherit CNode with session cap.");
55    }
56
57    /* spawn shell on the same core */
58    extern char **environ;
59    err = spawn_program_with_caps(my_core_id, shell, shell_argv, environ,
60                                  inheritcn_cap, NULL_CAP, SPAWN_FLAGS_NEW_DOMAIN,
61                                  NULL);
62    if (err_is_fail(err)) {
63        USER_PANIC_ERR(err, "Error spawning shell.");
64    }
65}
66
67static void print_usage(char *prog_name)
68{
69    fprintf(stderr, "%s: Wrong number of arguments. Expecting two arguments.\n",
70            prog_name);
71    fprintf(stderr, "Usage: %s <terminal> <terminal type>\n", prog_name);
72    fprintf(stderr, "\n");
73    fprintf(stderr, "\tterminal -  serial0.terminal|console0.terminal|...\n");
74    fprintf(stderr, "\tterminal type - xterm|...\n");
75}
76
77int main(int argc, char **argv)
78{
79    errval_t err;
80    struct capref session_id;
81
82    /* argument processing */
83    if (argc < 3) {
84        print_usage(argv[0]);
85        return EXIT_FAILURE;
86    }
87    char *terminal = argv[1];
88    char *terminal_type = argv[2];
89
90    /* start new session */
91    err = angler_new_session(terminal, &session_id);
92    if (err_is_fail(err)) {
93        USER_PANIC_ERR(err, "Error starting session.");
94    }
95
96    /* spawn shell */
97    start_shell(session_id, terminal_type);
98
99    return EXIT_SUCCESS;
100}
101