summaryrefslogtreecommitdiff
path: root/Userland/sh.cpp
blob: 19870439644f10925b27aeeaf8b883e1a178ab28 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <LibC/stdio.h>
#include <LibC/unistd.h>
#include <LibC/process.h>
#include <LibC/errno.h>
#include <LibC/string.h>
#include <LibC/stdlib.h>
#include <LibC/utsname.h>
#include <LibC/pwd.h>
#include <signal.h>
#include <AK/FileSystemPath.h>

struct GlobalState {
    String cwd;
    String username;
    const char* ttyname_short { nullptr };
    char ttyname[32];
    char hostname[32];
    pid_t sid;
};
static GlobalState* g;

static void prompt()
{
    if (getuid() == 0)
        printf("# ");
    else
        printf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g->username.characters(), g->hostname, g->cwd.characters());
}

static int sh_pwd(int, const char**)
{
    printf("%s\n", g->cwd.characters());
    return 0;
}

static volatile bool g_got_signal = false;

void did_receive_signal(int signum)
{
    printf("\nMy word, I've received a signal with number %d\n", signum);
    g_got_signal = true;
}

void handle_sigint(int signum)
{
    printf("Interrupt received by sh\n");
}

static int sh_busy(int, const char**)
{
    struct sigaction sa;
    sa.sa_handler = did_receive_signal;
    sa.sa_flags = 0;
    sa.sa_mask = 0;
    sa.sa_restorer = nullptr;
    int rc = sigaction(SIGUSR1, &sa, nullptr);
    assert(rc == 0);
    printf("listening for signal SIGUSR1 while looping in userspace...\n");
    for (;;) {
        for (volatile int i = 0; i < 100000; ++i)
            ;
        if (g_got_signal)
            break;
    }
    g_got_signal = false;
    return 0;
}

static int sh_fork(int, const char**)
{
    pid_t pid = fork();
    printf("getpid()=%d, fork()=%d\n", getpid(), pid);
    return 0;
}

static int sh_fe(int, const char**)
{
    pid_t pid = fork();
    if (!pid) {
        int rc = execve("/bin/ps", nullptr, nullptr);
        if (rc < 0) {
            perror("execve");
            exit(1);
        }
    }
    return 0;
}

static int sh_fef(int, const char**)
{
    pid_t pid = fork();
    if (!pid) {
        int rc = execve("/bin/psx", nullptr, nullptr);
        if (rc < 0) {
            perror("execve");
            exit(1);
        }
    }
    return 0;
}

static int sh_wt(int, const char**)
{
    const char* rodata_ptr = "foo";
    printf("Writing to rodata=%p...\n", rodata_ptr);
    *(char*)rodata_ptr = 0;

    char* text_ptr = (char*)sh_fef;
    printf("Writing to text=%p...\n", text_ptr);
    *text_ptr = 0;
    return 0;
}

static int sh_exit(int, const char**)
{
    printf("Good-bye!\n");
    exit(0);
    return 0;
}

static int sh_cd(int argc, const char** argv)
{
    if (argc == 1) {
        printf("usage: cd <path>\n");
        return 0;
    }

    char pathbuf[128];
    if (argv[1][0] == '/')
        memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
    else
        sprintf(pathbuf, "%s/%s", g->cwd.characters(), argv[1]);

    FileSystemPath canonicalPath(pathbuf);
    if (!canonicalPath.isValid()) {
        printf("FileSystemPath failed to canonicalize '%s'\n", pathbuf);
        return 1;
    }
    const char* path = canonicalPath.string().characters();

    struct stat st;
    int rc = lstat(path, &st);
    if (rc < 0) {
        printf("lstat(%s) failed: %s\n", path, strerror(errno));
        return 1;
    }
    if (!S_ISDIR(st.st_mode)) {
        printf("Not a directory: %s\n", path);
        return 1;
    }
    rc = chdir(path);
    if (rc < 0) {
        printf("chdir(%s) failed: %s\n", path, strerror(errno));
        return 1;
    }
    g->cwd = canonicalPath.string();
    return 0;
}

static bool handle_builtin(int argc, const char** argv, int& retval)
{
    if (argc == 0)
        return false;
    if (!strcmp(argv[0], "cd")) {
        retval = sh_cd(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "pwd")) {
        retval = sh_pwd(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "exit")) {
        retval = sh_exit(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "fe")) {
        retval = sh_fe(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "fef")) {
        retval = sh_fef(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "busy")) {
        retval = sh_busy(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "wt")) {
        retval = sh_wt(argc, argv);
        return true;
    }
    if (!strcmp(argv[0], "fork")) {
        retval = sh_fork(argc, argv);
        return true;
    }
    return false;
}

static int try_exec(const char* path, const char** argv)
{
    int ret = execve(path, argv, nullptr);
    assert(ret < 0);

    const char* search_path = "/bin";
    char pathbuf[128];
    sprintf(pathbuf, "%s/%s", search_path, argv[0]);
    ret = execve(pathbuf, argv, nullptr);
    if (ret == -1)
        return -1;
    return ret;
}

static int runcmd(char* cmd)
{
    if (cmd[0] == 0)
        return 0;
    char buf[128];
    memcpy(buf, cmd, 128);

    const char* argv[32];
    size_t argc = 1;
    argv[0] = &buf[0];
    size_t buflen = strlen(buf);
    for (size_t i = 0; i < buflen; ++i) {
        if (buf[i] == ' ') {
            buf[i] = '\0';
            argv[argc++] = &buf[i + 1];
        }
    }
    argv[argc] = nullptr;

    int retval = 0;
    if (handle_builtin(argc, argv, retval)) {
        return 0;
    }

    pid_t child = fork();
    if (!child) {
        setpgid(0, 0);
        tcsetpgrp(0, getpid());
        int ret = try_exec(argv[0], argv);
        if (ret < 0) {
            printf("exec failed: %s (%s)\n", cmd, strerror(errno));
            exit(1);
        }
        // We should never get here!
        assert(false);
    }

    int wstatus = 0;
    waitpid(child, &wstatus, 0);

    // FIXME: Should I really have to tcsetpgrp() after my child has exited?
    //        Is the terminal controlling pgrp really still the PGID of the dead process?
    tcsetpgrp(0, getpid());

    if (WIFEXITED(wstatus)) {
        //printf("Exited normally with status %d\n", WEXITSTATUS(wstatus));
    } else {
        if (WIFSIGNALED(wstatus)) {
            switch (WTERMSIG(wstatus)) {
            case SIGINT:
                printf("Interrupted\n");
                break;
            default:
                printf("Terminated by signal %d\n", WTERMSIG(wstatus));
                break;
            }
        } else {
            printf("Exited abnormally\n");
        }
    }
    return retval;
}

static void greeting()
{
    utsname uts;
    int rc = uname(&uts);
    if (rc < 0) {
        perror("uname");
        return;
    }
    printf("\n%s/%s on %s\n\n", uts.sysname, uts.machine, g->ttyname_short);
}

int main(int, char**)
{
    g = new GlobalState;
    g->sid = setsid();
    tcsetpgrp(0, getpgrp());

    {
        struct sigaction sa;
        sa.sa_handler = handle_sigint;
        sa.sa_flags = 0;
        sa.sa_mask = 0;
        sa.sa_restorer = nullptr;
        int rc = sigaction(SIGINT, &sa, nullptr);
        assert(rc == 0);
    }

    int rc = gethostname(g->hostname, sizeof(g->hostname));
    if (rc < 0)
        perror("gethostname");
    rc = ttyname_r(0, g->ttyname, sizeof(g->ttyname));
    if (rc < 0)
        perror("ttyname_r");
    else
        g->ttyname_short = strrchr(g->ttyname, '/') + 1;
    {
        auto* pw = getpwuid(getuid());
        if (pw)
            g->username = pw->pw_name;
        endpwent();
    }

    greeting();

    char linebuf[128];
    int linedx = 0;
    linebuf[0] = '\0';

    {
        char cwdbuf[1024];
        getcwd(cwdbuf, sizeof(cwdbuf));
        g->cwd = cwdbuf;
    }
    prompt();
    for (;;) {
        char keybuf[16];
        ssize_t nread = read(0, keybuf, sizeof(keybuf));
        if (nread < 0) {
            printf("failed to read :(\n");
            return 2;
        }
        for (ssize_t i = 0; i < nread; ++i) {
            putchar(keybuf[i]);
            if (keybuf[i] != '\n') {
                linebuf[linedx++] = keybuf[i];
                linebuf[linedx] = '\0';
            } else {
                runcmd(linebuf);
                linebuf[0] = '\0';
                linedx = 0;
                prompt();
            }
        }
    }
    return 0;
}