1.1 --- a/test_files/programs/dstest_exec_payload.c Fri Mar 10 18:54:41 2023 +0100
1.2 +++ b/test_files/programs/dstest_exec_payload.c Mon Mar 13 00:40:16 2023 +0100
1.3 @@ -1,7 +1,7 @@
1.4 /*
1.5 * A test of executing code in a new task.
1.6 *
1.7 - * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -19,19 +19,152 @@
1.13 * Boston, MA 02110-1301, USA
1.14 */
1.15
1.16 +#include <l4/re/env.h>
1.17 +#include <l4/sys/err.h>
1.18 +
1.19 #include <stdio.h>
1.20 +#include <stdlib.h>
1.21 +#include <string.h>
1.22 +
1.23 +#include <fsclient/client.h>
1.24 +#include <systypes/env.h>
1.25 +#include <systypes/fcntl.h>
1.26 +
1.27 +
1.28 +
1.29 +/* Read a line from the file. */
1.30 +
1.31 +static int readline(file_t *file, int *lineno, char **start, char **end)
1.32 +{
1.33 + static char buf[256];
1.34 + static offset_t current = 0, limit = 0;
1.35 + char *newline = NULL;
1.36 +
1.37 + do
1.38 + {
1.39 + /* Obtain file content. */
1.40 +
1.41 + if (!limit)
1.42 + {
1.43 + limit = client_read(file, buf, 256);
1.44 +
1.45 + if (!limit)
1.46 + {
1.47 + *start = NULL;
1.48 + *end = NULL;
1.49 + return 0;
1.50 + }
1.51 +
1.52 + current = 0;
1.53 +
1.54 + /* Start of line at start of buffer. */
1.55 +
1.56 + if (newline != NULL)
1.57 + {
1.58 + current += 1;
1.59 + *start = buf;
1.60 + *end = (char *) memchr(buf + current, (int) '\n', limit - current);
1.61 +
1.62 + if (*end == NULL)
1.63 + *end = buf + limit;
1.64 +
1.65 + return 1;
1.66 + }
1.67 + }
1.68 +
1.69 + /* Find newline. */
1.70 +
1.71 + newline = (char *) memchr(buf + current, (int) '\n', limit);
1.72 +
1.73 + if (newline != NULL)
1.74 + {
1.75 + (*lineno)++;
1.76 + current = newline - (char *) buf + 1;
1.77 +
1.78 + /* Start of line before end of buffer. */
1.79 +
1.80 + if (current < limit)
1.81 + {
1.82 + *start = newline + 1;
1.83 + *end = (char *) memchr(buf + current, (int) '\n', limit - current);
1.84 +
1.85 + if (*end == NULL)
1.86 + *end = buf + limit;
1.87 +
1.88 + return 1;
1.89 + }
1.90 +
1.91 + /* Otherwise, reset the buffer to read the start of line. */
1.92 +
1.93 + else
1.94 + limit = 0;
1.95 + }
1.96 +
1.97 + /* No newline: read more data. */
1.98 +
1.99 + else
1.100 + limit = 0;
1.101 + }
1.102 + while (1);
1.103 +}
1.104
1.105
1.106
1.107 int main(int argc, char *argv[])
1.108 {
1.109 - int i;
1.110 + file_t *file;
1.111 + int i, lineno;
1.112 + char *start, *end;
1.113
1.114 printf("Hello from %s (%d)!\n", argc > 0 ? argv[0] : "exec_payload.c", argc);
1.115
1.116 for (i = 0; i < argc; i++)
1.117 printf("Arg #%d: %s\n", i, argv[i]);
1.118
1.119 + if (argc < 2)
1.120 + return 1;
1.121 +
1.122 + printf("Filesystem is %lx\n", l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME));
1.123 + printf("Opening file %s...\n", argv[1]);
1.124 +
1.125 + file = client_open(argv[1], O_RDONLY);
1.126 +
1.127 + if (!client_opened(file))
1.128 + {
1.129 + if (file != NULL)
1.130 + printf("Error: %s\n", l4sys_errtostr(file->error));
1.131 + else
1.132 + printf("Could not open file.\n");
1.133 +
1.134 + while (1);
1.135 + }
1.136 +
1.137 + lineno = atoi(argv[2]);
1.138 +
1.139 + printf("Finding line %d...\n", lineno);
1.140 +
1.141 + i = 1;
1.142 + while (i < lineno)
1.143 + if (!readline(file, &i, &start, &end))
1.144 + break;
1.145 +
1.146 + printf("Showing line %d...\n", lineno);
1.147 +
1.148 + while (i == lineno)
1.149 + {
1.150 + fwrite(start, sizeof(char), end - start, stdout);
1.151 + if (!readline(file, &i, &start, &end))
1.152 + break;
1.153 + }
1.154 +
1.155 + fputs("\n\n", stdout);
1.156 +
1.157 + printf("Closing file...\n");
1.158 +
1.159 + client_close(file);
1.160 + client_notifier_close(client_notifier_task());
1.161 +
1.162 + printf("Terminating.\n");
1.163 return 0;
1.164 }
1.165