[469] | 1 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 2 | // File : ksh.c |
---|
| 3 | // Date : October 2017 |
---|
| 4 | // Author : Alain Greiner |
---|
[469] | 5 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[457] | 6 | // This application implements a minimal shell for ALMOS-MKH. |
---|
| 7 | // |
---|
| 8 | // This user KSH process contains two POSIX threads: |
---|
[446] | 9 | // - the "main" thread contains the infinite loop implementing |
---|
[574] | 10 | // the children processes termination monitoring, using the wait() syscall. |
---|
[469] | 11 | // - the "interactive" thread contains the infinite loop implementing the command |
---|
| 12 | // interpreter attached to the TXT terminal, and handling one KSH command |
---|
| 13 | // per iteration. |
---|
[457] | 14 | // |
---|
[469] | 15 | // The children processes are created by the <load> command, and are |
---|
[457] | 16 | // attached to the same TXT terminal as the KSH process itself. |
---|
[469] | 17 | // A child process can be lauched in foreground or in background: |
---|
| 18 | // . when the child process is running in foreground, the KSH process loses |
---|
| 19 | // the TXT terminal ownership, that is transfered to the child process. |
---|
| 20 | // . when the child process is running in background: the KSH process keeps |
---|
| 21 | // the TXT terminal ownership. |
---|
| 22 | // |
---|
| 23 | // A semaphore is used to synchronize the two KSH threads. At each iteration, |
---|
| 24 | // the interactive thread check the semaphore (with a sem_wait). It blocks |
---|
| 25 | // and deschedules, if the KSH process loosed the TXT ownership (after a load, |
---|
| 26 | // or for any other cause. It unblocks with the following policy: |
---|
| 27 | // . if the command is "not a load", the semaphore is incremented by the |
---|
| 28 | // cmd_***() function when the command is completed, to allow the KSH interactive() |
---|
| 29 | // function to get the next command in the while loop. |
---|
| 30 | // . if the command is a "load without &", the TXT is given to the NEW process by the |
---|
| 31 | // execve() syscall, and is released to the KSH process when NEW process terminates. |
---|
| 32 | // The KSH process is notified and the KSH main() function increments the semahore |
---|
| 33 | // to allow the KSH interactive() function to handle commands. |
---|
| 34 | // . if the command is a "load with &", the cmd_load() function returns the TXT |
---|
| 35 | // to the KSH process and increment the semaphore, when the parent KSH process |
---|
| 36 | // returns from the fork() syscall. |
---|
| 37 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 38 | |
---|
| 39 | #include <stdio.h> |
---|
| 40 | #include <stdlib.h> |
---|
| 41 | #include <string.h> |
---|
[444] | 42 | #include <sys/wait.h> |
---|
| 43 | #include <signal.h> |
---|
| 44 | #include <unistd.h> |
---|
[445] | 45 | #include <almosmkh.h> |
---|
[457] | 46 | #include <semaphore.h> |
---|
[588] | 47 | #include <hal_macros.h> |
---|
[230] | 48 | |
---|
[407] | 49 | #define CMD_MAX_SIZE (256) // max number of characters in one command |
---|
[436] | 50 | #define LOG_DEPTH (32) // max number of registered commands |
---|
[407] | 51 | #define MAX_ARGS (32) // max number of arguments in a command |
---|
[230] | 52 | |
---|
[574] | 53 | #define MAIN_DEBUG 0 |
---|
[469] | 54 | #define CMD_LOAD_DEBUG 0 |
---|
[457] | 55 | |
---|
[469] | 56 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 57 | // Structures |
---|
[469] | 58 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 59 | |
---|
[407] | 60 | // one entry in the registered commands array |
---|
| 61 | typedef struct log_entry_s |
---|
| 62 | { |
---|
| 63 | char buf[CMD_MAX_SIZE]; |
---|
| 64 | unsigned int count; |
---|
| 65 | } |
---|
| 66 | log_entry_t; |
---|
[230] | 67 | |
---|
[407] | 68 | // one entry in the supported command types array |
---|
| 69 | typedef struct ksh_cmd_s |
---|
| 70 | { |
---|
| 71 | char * name; |
---|
| 72 | char * desc; |
---|
| 73 | void (*fn)( int , char ** ); |
---|
| 74 | } |
---|
| 75 | ksh_cmd_t; |
---|
| 76 | |
---|
| 77 | |
---|
[469] | 78 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 79 | // Global Variables |
---|
[469] | 80 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 81 | |
---|
[407] | 82 | ksh_cmd_t cmd[]; // array of supported commands |
---|
[230] | 83 | |
---|
[407] | 84 | log_entry_t log_entries[LOG_DEPTH]; // array of registered commands |
---|
[230] | 85 | |
---|
[407] | 86 | unsigned int ptw; // write pointer in log_entries[] |
---|
| 87 | unsigned int ptr; // read pointer in log_entries[] |
---|
[230] | 88 | |
---|
[457] | 89 | pthread_attr_t attr; // interactive thread attributes |
---|
[446] | 90 | |
---|
[457] | 91 | sem_t semaphore; // block interactive thread when zero |
---|
| 92 | |
---|
[469] | 93 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 94 | // Shell Commands |
---|
[469] | 95 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 96 | |
---|
[407] | 97 | ///////////////////////////////////////////// |
---|
| 98 | static void cmd_cat( int argc , char **argv ) |
---|
[230] | 99 | { |
---|
[407] | 100 | char * path; |
---|
[230] | 101 | |
---|
[407] | 102 | if (argc != 2) |
---|
| 103 | { |
---|
[409] | 104 | printf(" usage: cat pathname\n"); |
---|
[230] | 105 | return; |
---|
| 106 | } |
---|
[407] | 107 | |
---|
[230] | 108 | path = argv[1]; |
---|
| 109 | |
---|
[473] | 110 | printf(" error: not implemented yet\n", argc, argv ); |
---|
[407] | 111 | |
---|
| 112 | /* |
---|
| 113 | // open the file |
---|
| 114 | fd = open( path , O_RDONLY , 0 ); |
---|
| 115 | if (fd < 0) |
---|
| 116 | { |
---|
[409] | 117 | printf(" error: cannot open %s\n", path); |
---|
[230] | 118 | goto exit; |
---|
| 119 | } |
---|
| 120 | |
---|
[407] | 121 | // get file size |
---|
| 122 | if (stat(path, &st) == -1) |
---|
| 123 | { |
---|
[409] | 124 | printf(" error: cannot stat %s\n", path); |
---|
[230] | 125 | goto exit; |
---|
| 126 | } |
---|
| 127 | if (S_ISDIR(st.st_mode)) { |
---|
[409] | 128 | printf(" error: %s is a directory\n", path); |
---|
[230] | 129 | goto exit; |
---|
| 130 | } |
---|
| 131 | size = st.st_size; |
---|
| 132 | |
---|
[407] | 133 | // mmap the file |
---|
[230] | 134 | buf = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
---|
| 135 | if (buf == NULL || buf == (char *)-1) { |
---|
[409] | 136 | printf(" error: cannot map %s\n", path); |
---|
[230] | 137 | goto exit; |
---|
| 138 | } |
---|
| 139 | |
---|
[446] | 140 | // set terminating '0' |
---|
[230] | 141 | buf[size-1] = 0; |
---|
| 142 | |
---|
[407] | 143 | // display the file content |
---|
[230] | 144 | printf("%s", buf); |
---|
| 145 | |
---|
| 146 | exit: |
---|
[407] | 147 | if (buf != NULL) munmap(buf, size); |
---|
| 148 | if (fd >= 0) close(fd); |
---|
| 149 | */ |
---|
[230] | 150 | |
---|
[457] | 151 | // release semaphore to get next command |
---|
| 152 | sem_post( &semaphore ); |
---|
| 153 | |
---|
[407] | 154 | } // end cmd_cat() |
---|
| 155 | |
---|
| 156 | //////////////////////////////////////////// |
---|
| 157 | static void cmd_cd( int argc , char **argv ) |
---|
| 158 | { |
---|
| 159 | char * path; |
---|
| 160 | |
---|
| 161 | if (argc != 2) |
---|
| 162 | { |
---|
[409] | 163 | printf(" usage: cd pathname\n"); |
---|
[407] | 164 | return; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | path = argv[1]; |
---|
| 168 | |
---|
[473] | 169 | printf(" error: not implemented yet\n", argc, argv ); |
---|
[407] | 170 | |
---|
[457] | 171 | // release semaphore to get next command |
---|
| 172 | sem_post( &semaphore ); |
---|
[407] | 173 | |
---|
| 174 | } // end cmd_cd() |
---|
| 175 | |
---|
| 176 | ///////////////////////////////////////// |
---|
[230] | 177 | static void cmd_cp(int argc, char **argv) |
---|
| 178 | { |
---|
[407] | 179 | // int src_fd = -1, dst_fd = -1; |
---|
| 180 | // char *srcpath, *dstpath; |
---|
| 181 | // struct stat st; |
---|
| 182 | // size_t size, i; |
---|
| 183 | // char buf[1024]; |
---|
[230] | 184 | |
---|
[407] | 185 | if (argc != 3) |
---|
| 186 | { |
---|
[409] | 187 | printf(" usage: cp src_pathname dst_pathname\n"); |
---|
[230] | 188 | return; |
---|
| 189 | } |
---|
| 190 | |
---|
[473] | 191 | printf(" error: not implemented yet\n", argc, argv ); |
---|
[407] | 192 | |
---|
| 193 | /* |
---|
[230] | 194 | srcpath = argv[1]; |
---|
| 195 | dstpath = argv[2]; |
---|
| 196 | |
---|
[407] | 197 | // open the src file |
---|
[230] | 198 | src_fd = open(srcpath, O_RDONLY, 0); |
---|
| 199 | if (src_fd < 0) { |
---|
[409] | 200 | printf(" error: cannot open %s / err = %d\n", srcpath, errno); |
---|
[230] | 201 | goto exit; |
---|
| 202 | } |
---|
| 203 | |
---|
[407] | 204 | // get file size |
---|
[230] | 205 | if (stat(srcpath, &st) == -1) { |
---|
[409] | 206 | printf(" error: cannot stat %s\n", srcpath); |
---|
[230] | 207 | goto exit; |
---|
| 208 | } |
---|
| 209 | if (S_ISDIR(st.st_mode)) { |
---|
[409] | 210 | printf(" error: %s is a directory\n", srcpath); |
---|
[230] | 211 | goto exit; |
---|
| 212 | } |
---|
| 213 | size = st.st_size; |
---|
| 214 | |
---|
[407] | 215 | // open the dst file |
---|
[230] | 216 | dst_fd = open(dstpath, O_CREAT|O_TRUNC|O_RDWR, 0); |
---|
| 217 | if (dst_fd < 0) { |
---|
[409] | 218 | printf(" error: cannot open %s / err = %d\n", dstpath, errno); |
---|
[230] | 219 | goto exit; |
---|
| 220 | } |
---|
| 221 | if (stat(dstpath, &st) == -1) { |
---|
[409] | 222 | printf(" error: cannot stat %s\n", dstpath); |
---|
[230] | 223 | goto exit; |
---|
| 224 | } |
---|
| 225 | if (S_ISDIR(st.st_mode)) { |
---|
[409] | 226 | printf(" error: %s is a directory\n", dstpath); |
---|
[230] | 227 | goto exit; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | i = 0; |
---|
| 231 | while (i < size) |
---|
| 232 | { |
---|
| 233 | size_t rlen = (size - i < 1024 ? size - i : 1024); |
---|
| 234 | size_t wlen; |
---|
| 235 | ssize_t ret; |
---|
| 236 | |
---|
[407] | 237 | // read the source |
---|
[230] | 238 | ret = read(src_fd, buf, rlen); |
---|
| 239 | if (ret == -1) { |
---|
[409] | 240 | printf(" error: cannot read from file %s\n", srcpath); |
---|
[230] | 241 | goto exit; |
---|
| 242 | } |
---|
| 243 | rlen = (size_t)ret; |
---|
| 244 | |
---|
[407] | 245 | // write to the destination |
---|
[230] | 246 | ret = write(dst_fd, buf, rlen); |
---|
| 247 | if (ret == -1) { |
---|
[409] | 248 | printf(" error: cannot write to file %s\n", dstpath); |
---|
[230] | 249 | goto exit; |
---|
| 250 | } |
---|
| 251 | wlen = (size_t)ret; |
---|
| 252 | |
---|
[407] | 253 | // check |
---|
[230] | 254 | if (wlen != rlen) { |
---|
[409] | 255 | printf(" error: cannot write on device\n"); |
---|
[230] | 256 | goto exit; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | i += rlen; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | exit: |
---|
[407] | 263 | if (src_fd >= 0) close(src_fd); |
---|
| 264 | if (dst_fd >= 0) close(dst_fd); |
---|
| 265 | */ |
---|
[230] | 266 | |
---|
[457] | 267 | // release semaphore to get next command |
---|
| 268 | sem_post( &semaphore ); |
---|
| 269 | |
---|
[407] | 270 | } // end cmd_cp() |
---|
| 271 | |
---|
[436] | 272 | ///////////////////////////////////////////////// |
---|
| 273 | static void cmd_display( int argc , char **argv ) |
---|
| 274 | { |
---|
| 275 | unsigned int cxy; |
---|
| 276 | unsigned int lid; |
---|
| 277 | unsigned int pid; |
---|
| 278 | unsigned int txt_id; |
---|
| 279 | |
---|
| 280 | if( strcmp( argv[1] , "vmm" ) == 0 ) |
---|
| 281 | { |
---|
[442] | 282 | if( argc != 4 ) |
---|
[436] | 283 | { |
---|
[442] | 284 | printf(" usage: display vmm cxy pid\n"); |
---|
[436] | 285 | return; |
---|
| 286 | } |
---|
| 287 | |
---|
[442] | 288 | cxy = atoi(argv[2]); |
---|
| 289 | pid = atoi(argv[3]); |
---|
[436] | 290 | |
---|
[442] | 291 | if( display_vmm( cxy , pid ) ) |
---|
[436] | 292 | { |
---|
[442] | 293 | printf(" error: no process %x in cluster %x\n", pid , cxy ); |
---|
[436] | 294 | } |
---|
| 295 | } |
---|
| 296 | else if( strcmp( argv[1] , "sched" ) == 0 ) |
---|
| 297 | { |
---|
| 298 | if( argc != 4 ) |
---|
| 299 | { |
---|
| 300 | printf(" usage: display sched cxy lid\n"); |
---|
| 301 | return; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | cxy = atoi(argv[2]); |
---|
| 305 | lid = atoi(argv[3]); |
---|
| 306 | |
---|
| 307 | if( display_sched( cxy , lid ) ) |
---|
| 308 | { |
---|
| 309 | printf(" error: illegal arguments cxy = %x / lid = %d\n", cxy, lid ); |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | else if( strcmp( argv[1] , "process" ) == 0 ) |
---|
| 313 | { |
---|
| 314 | if( argc != 3 ) |
---|
| 315 | { |
---|
| 316 | printf(" usage: display process cxy\n"); |
---|
| 317 | return; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | cxy = atoi(argv[2]); |
---|
| 321 | |
---|
[588] | 322 | if( display_cluster_processes( cxy , 0 ) ) |
---|
[436] | 323 | { |
---|
| 324 | printf(" error: illegal argument cxy = %x\n", cxy ); |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | else if( strcmp( argv[1] , "txt" ) == 0 ) |
---|
| 328 | { |
---|
| 329 | if( argc != 3 ) |
---|
| 330 | { |
---|
| 331 | printf(" usage: display txt txt_id\n"); |
---|
| 332 | return; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | txt_id = atoi(argv[2]); |
---|
| 336 | |
---|
| 337 | if( display_txt_processes( txt_id ) ) |
---|
| 338 | { |
---|
[458] | 339 | printf(" error: illegal argument txt_id = %d\n", txt_id ); |
---|
[436] | 340 | } |
---|
| 341 | } |
---|
| 342 | else if( strcmp( argv[1] , "vfs" ) == 0 ) |
---|
| 343 | { |
---|
| 344 | if( argc != 2 ) |
---|
| 345 | { |
---|
| 346 | printf(" usage: display vfs\n"); |
---|
| 347 | return; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | display_vfs(); |
---|
| 351 | } |
---|
| 352 | else if( strcmp( argv[1] , "chdev" ) == 0 ) |
---|
| 353 | { |
---|
| 354 | if( argc != 2 ) |
---|
| 355 | { |
---|
| 356 | printf(" usage: display chdev\n"); |
---|
| 357 | return; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | display_chdev(); |
---|
| 361 | } |
---|
[445] | 362 | else if( strcmp( argv[1] , "dqdt" ) == 0 ) |
---|
| 363 | { |
---|
| 364 | if( argc != 2 ) |
---|
| 365 | { |
---|
| 366 | printf(" usage: display dqdt\n"); |
---|
| 367 | return; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | display_dqdt(); |
---|
| 371 | } |
---|
[436] | 372 | else |
---|
| 373 | { |
---|
| 374 | printf(" usage: display (vmm/sched/process/vfs/chdev/txt) [arg2] [arg3]\n"); |
---|
| 375 | } |
---|
[457] | 376 | |
---|
| 377 | // release semaphore to get next command |
---|
| 378 | sem_post( &semaphore ); |
---|
| 379 | |
---|
[436] | 380 | } // end cmd_display() |
---|
| 381 | |
---|
[427] | 382 | ///////////////////////////////////////// |
---|
| 383 | static void cmd_fg(int argc, char **argv) |
---|
| 384 | { |
---|
| 385 | unsigned int pid; |
---|
| 386 | |
---|
| 387 | if (argc != 2) |
---|
| 388 | { |
---|
| 389 | printf(" usage: %s pid\n", argv[0]); |
---|
| 390 | return; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | pid = atoi( argv[1] ); |
---|
| 394 | |
---|
| 395 | if( pid == 0 ) |
---|
| 396 | { |
---|
[442] | 397 | printf(" error: PID cannot be 0\n" ); |
---|
[427] | 398 | } |
---|
| 399 | |
---|
| 400 | if( fg( pid ) ) |
---|
| 401 | { |
---|
| 402 | printf(" error: cannot find process %x\n", pid ); |
---|
| 403 | } |
---|
[457] | 404 | |
---|
| 405 | // release semaphore to get next command |
---|
| 406 | sem_post( &semaphore ); |
---|
| 407 | |
---|
[436] | 408 | } // end cmd_fg() |
---|
[427] | 409 | |
---|
[407] | 410 | ////////////////////////////////////////////// |
---|
| 411 | static void cmd_help( int argc , char **argv ) |
---|
[230] | 412 | { |
---|
[407] | 413 | unsigned int i; |
---|
[230] | 414 | |
---|
[407] | 415 | if (argc != 1) |
---|
| 416 | { |
---|
[409] | 417 | printf(" usage: %s\n", argv[0]); |
---|
[230] | 418 | return; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | printf("available commands:\n"); |
---|
[407] | 422 | for (i = 0 ; cmd[i].name ; i++) |
---|
| 423 | { |
---|
[230] | 424 | printf("\t%s\t : %s\n", cmd[i].name , cmd[i].desc); |
---|
| 425 | } |
---|
[457] | 426 | |
---|
| 427 | // release semaphore to get next command |
---|
| 428 | sem_post( &semaphore ); |
---|
| 429 | |
---|
[407] | 430 | } // end cmd_help() |
---|
[230] | 431 | |
---|
[407] | 432 | ////////////////////////////////////////////// |
---|
| 433 | static void cmd_kill( int argc , char **argv ) |
---|
[230] | 434 | { |
---|
[407] | 435 | unsigned int pid; |
---|
[230] | 436 | |
---|
[407] | 437 | if (argc != 2) |
---|
| 438 | { |
---|
[409] | 439 | printf(" usage: %s pid\n", argv[0]); |
---|
[230] | 440 | return; |
---|
| 441 | } |
---|
| 442 | |
---|
[427] | 443 | pid = atoi( argv[1] ); |
---|
[230] | 444 | |
---|
[427] | 445 | if( pid == 0 ) |
---|
| 446 | { |
---|
[440] | 447 | printf(" error: kernel process 0 cannot be killed\n" ); |
---|
[427] | 448 | } |
---|
| 449 | |
---|
[416] | 450 | if( kill( pid , SIGKILL ) ) |
---|
[407] | 451 | { |
---|
[440] | 452 | printf(" error: process %x cannot be killed\n", pid ); |
---|
[230] | 453 | } |
---|
[457] | 454 | |
---|
| 455 | // release semaphore to get next command |
---|
| 456 | sem_post( &semaphore ); |
---|
| 457 | |
---|
[407] | 458 | } // end cmd_kill() |
---|
[230] | 459 | |
---|
[407] | 460 | ////////////////////////////////////////////// |
---|
| 461 | static void cmd_load( int argc , char **argv ) |
---|
[230] | 462 | { |
---|
[436] | 463 | int ret_fork; // return value from fork |
---|
| 464 | int ret_exec; // return value from exec |
---|
| 465 | unsigned int ksh_pid; // KSH process PID |
---|
| 466 | char * pathname; // path to .elf file |
---|
| 467 | unsigned int background; // background execution if non zero |
---|
[588] | 468 | unsigned int placement; // placement specified if non zero |
---|
| 469 | unsigned int cxy; // target cluster if placement specified |
---|
[407] | 470 | |
---|
[588] | 471 | if( (argc < 2) || (argc > 4) ) |
---|
[407] | 472 | { |
---|
[588] | 473 | printf(" usage: %s pathname [cxy] [&]\n", argv[0] ); |
---|
[407] | 474 | return; |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | pathname = argv[1]; |
---|
| 478 | |
---|
[588] | 479 | if( argc == 2 ) |
---|
| 480 | { |
---|
| 481 | background = 0; |
---|
| 482 | placement = 0; |
---|
| 483 | cxy = 0; |
---|
| 484 | } |
---|
| 485 | else if( argc == 3 ) |
---|
| 486 | { |
---|
| 487 | if( (argv[2][0] == '&') && (argv[2][1] == 0) ) |
---|
| 488 | { |
---|
| 489 | background = 1; |
---|
| 490 | placement = 0; |
---|
| 491 | cxy = 0; |
---|
| 492 | } |
---|
| 493 | else |
---|
| 494 | { |
---|
| 495 | background = 0; |
---|
| 496 | placement = 1; |
---|
| 497 | cxy = atoi( argv[2] ); |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | else // argc == 4 |
---|
| 501 | { |
---|
| 502 | background = ( (argv[3][0] == '&') && (argv[3][1] == 0) ); |
---|
| 503 | placement = 1; |
---|
| 504 | cxy = atoi( argv[2] ); |
---|
| 505 | } |
---|
[427] | 506 | |
---|
[434] | 507 | // get KSH process PID |
---|
| 508 | ksh_pid = getpid(); |
---|
[407] | 509 | |
---|
[469] | 510 | #if CMD_LOAD_DEBUG |
---|
| 511 | long long unsigned cycle; |
---|
| 512 | get_cycle( &cycle ); |
---|
[588] | 513 | printf("\n[KSH] %s : ksh_pid %x / path %s / bg %d / place %d (%x) / cycle %d\n", |
---|
| 514 | __FUNCTION__, ksh_pid, argv[1], background, placement, cxy, (int)cycle ); |
---|
[457] | 515 | #endif |
---|
| 516 | |
---|
[588] | 517 | // set target cluster if required |
---|
| 518 | if( placement ) place_fork( cxy ); |
---|
| 519 | |
---|
[434] | 520 | // KSH process fork CHILD process |
---|
[436] | 521 | ret_fork = fork(); |
---|
[434] | 522 | |
---|
[457] | 523 | if ( ret_fork < 0 ) // it is a failure reported to KSH |
---|
[407] | 524 | { |
---|
[434] | 525 | printf(" error: ksh process unable to fork\n"); |
---|
[440] | 526 | return; |
---|
[434] | 527 | } |
---|
[457] | 528 | else if (ret_fork == 0) // it is the CHILD process |
---|
[434] | 529 | { |
---|
[469] | 530 | |
---|
| 531 | #if CMD_LOAD_DEBUG |
---|
| 532 | get_cycle( &cycle ); |
---|
[588] | 533 | printf("\n[KSH] %s : child_pid %x after fork, before exec / cycle %d\n", |
---|
[469] | 534 | __FUNCTION__ , getpid(), (int)cycle ); |
---|
| 535 | #endif |
---|
| 536 | |
---|
[434] | 537 | // CHILD process exec NEW process |
---|
[444] | 538 | ret_exec = execve( pathname , NULL , NULL ); |
---|
[434] | 539 | |
---|
[469] | 540 | #if CMD_LOAD_DEBUG |
---|
| 541 | get_cycle( &cycle ); |
---|
[588] | 542 | printf("\n[KSH] %s : child_pid %x after exec / ret_exec %d / cycle %d\n", |
---|
[469] | 543 | __FUNCTION__ , getpid(), ret_exec, (int)cycle ); |
---|
| 544 | #endif |
---|
| 545 | |
---|
[441] | 546 | // this is only executed in case of exec failure |
---|
[436] | 547 | if( ret_exec ) |
---|
[409] | 548 | { |
---|
[441] | 549 | printf(" error: child process unable to exec <%s>\n", pathname ); |
---|
| 550 | exit( 0 ); |
---|
[436] | 551 | } |
---|
| 552 | } |
---|
[457] | 553 | else // it is the KSH process : ret_fork is the new process PID |
---|
[436] | 554 | { |
---|
| 555 | |
---|
[469] | 556 | #if CMD_LOAD_DEBUG |
---|
| 557 | get_cycle( &cycle ); |
---|
[588] | 558 | printf("\n[KSH] %s : ksh_pid %x after fork / ret_fork %x / cycle %d\n", |
---|
[469] | 559 | __FUNCTION__, getpid(), ret_fork, (int)cycle ); |
---|
[457] | 560 | #endif |
---|
| 561 | |
---|
[469] | 562 | if( background ) // child in background => KSH must keep TXT ownership |
---|
[457] | 563 | { |
---|
| 564 | // execve() tranfered TXT ownership to child => give it back to KSH |
---|
| 565 | fg( ksh_pid ); |
---|
| 566 | |
---|
| 567 | // release semaphore to get next command |
---|
| 568 | sem_post( &semaphore ); |
---|
| 569 | } |
---|
[436] | 570 | } |
---|
[407] | 571 | } // end cmd_load |
---|
| 572 | |
---|
| 573 | ///////////////////////////////////////////// |
---|
| 574 | static void cmd_log( int argc , char **argv ) |
---|
| 575 | { |
---|
| 576 | unsigned int i; |
---|
| 577 | |
---|
[473] | 578 | if (argc != 1) |
---|
| 579 | { |
---|
| 580 | printf(" usage: %s\n", argv[0], argc ); |
---|
| 581 | return; |
---|
| 582 | } |
---|
| 583 | |
---|
[230] | 584 | printf("--- registered commands ---\n"); |
---|
[407] | 585 | for (i = 0; i < LOG_DEPTH; i++) |
---|
| 586 | { |
---|
[436] | 587 | printf(" - %d\t: %s\n", i, &log_entries[i].buf); |
---|
[230] | 588 | } |
---|
| 589 | |
---|
[457] | 590 | // release semaphore to get next command |
---|
| 591 | sem_post( &semaphore ); |
---|
| 592 | |
---|
| 593 | } // end cmd_log() |
---|
| 594 | |
---|
| 595 | |
---|
[407] | 596 | //////////////////////////////////////////// |
---|
| 597 | static void cmd_ls( int argc , char **argv ) |
---|
[230] | 598 | { |
---|
[407] | 599 | char * path; |
---|
[230] | 600 | |
---|
[407] | 601 | // struct dirent * file; |
---|
| 602 | // DIR *dir; |
---|
| 603 | |
---|
| 604 | if (argc == 1) |
---|
| 605 | { |
---|
[230] | 606 | path = "."; |
---|
[407] | 607 | } |
---|
| 608 | else if (argc == 2) |
---|
| 609 | { |
---|
[230] | 610 | path = argv[1]; |
---|
[407] | 611 | } |
---|
| 612 | else |
---|
| 613 | { |
---|
[409] | 614 | printf(" usage: ls [path]\n"); |
---|
[230] | 615 | return; |
---|
| 616 | } |
---|
| 617 | |
---|
[409] | 618 | printf(" error: not implemented yet\n"); |
---|
[407] | 619 | /* |
---|
| 620 | dir = opendir( path ); |
---|
[230] | 621 | while ((file = readdir(dir)) != NULL) |
---|
| 622 | { |
---|
| 623 | printf(" %s\n", file->d_name); |
---|
| 624 | } |
---|
| 625 | closedir(dir); |
---|
[407] | 626 | */ |
---|
[230] | 627 | |
---|
[457] | 628 | // release semaphore to get next command |
---|
| 629 | sem_post( &semaphore ); |
---|
| 630 | |
---|
| 631 | } // end cmd_ls() |
---|
| 632 | |
---|
[407] | 633 | /////////////////////////////////////////////// |
---|
| 634 | static void cmd_mkdir( int argc , char **argv ) |
---|
[230] | 635 | { |
---|
[407] | 636 | char * pathname; |
---|
[230] | 637 | |
---|
[407] | 638 | if (argc != 2) |
---|
| 639 | { |
---|
[409] | 640 | printf(" usage: mkdir pathname\n"); |
---|
[230] | 641 | return; |
---|
| 642 | } |
---|
| 643 | |
---|
[407] | 644 | pathname = argv[1]; |
---|
[230] | 645 | |
---|
[409] | 646 | printf(" error: not implemented yet\n"); |
---|
[230] | 647 | |
---|
[457] | 648 | // release semaphore to get next command |
---|
| 649 | sem_post( &semaphore ); |
---|
| 650 | |
---|
| 651 | } // end cmd_mkdir() |
---|
| 652 | |
---|
[407] | 653 | //////////////////////////////////////////// |
---|
| 654 | static void cmd_mv( int argc , char **argv ) |
---|
[230] | 655 | { |
---|
[407] | 656 | |
---|
[230] | 657 | if (argc < 3) |
---|
| 658 | { |
---|
| 659 | printf(" usage : %s src_pathname dst_pathname\n", argv[0]); |
---|
| 660 | return; |
---|
| 661 | } |
---|
| 662 | |
---|
[409] | 663 | printf(" error: not implemented yet\n"); |
---|
[407] | 664 | |
---|
[457] | 665 | // release semaphore to get next command |
---|
| 666 | sem_post( &semaphore ); |
---|
[407] | 667 | |
---|
[457] | 668 | } // end cmd_mv |
---|
[230] | 669 | |
---|
[588] | 670 | |
---|
| 671 | //////////////////////////////////////////// |
---|
| 672 | static void cmd_ps( int argc , char **argv ) |
---|
| 673 | { |
---|
| 674 | unsigned int x_size; |
---|
| 675 | unsigned int y_size; |
---|
| 676 | unsigned int ncores; |
---|
| 677 | unsigned int x; |
---|
| 678 | unsigned int y; |
---|
| 679 | |
---|
| 680 | if (argc != 1) |
---|
| 681 | { |
---|
| 682 | printf(" usage: %s\n", argv[0]); |
---|
| 683 | return; |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | // get platform config |
---|
| 687 | get_config( &x_size , &y_size , &ncores ); |
---|
| 688 | |
---|
| 689 | // scan all clusers |
---|
| 690 | for( x = 0 ; x < x_size ; x++ ) |
---|
| 691 | { |
---|
| 692 | for( y = 0 ; y < y_size ; y++ ) |
---|
| 693 | { |
---|
| 694 | display_cluster_processes( HAL_CXY_FROM_XY(x,y), 1 ); // only owned processes |
---|
| 695 | } |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | // release semaphore to get next command |
---|
| 699 | sem_post( &semaphore ); |
---|
| 700 | |
---|
| 701 | } // end cmd_ps() |
---|
| 702 | |
---|
[407] | 703 | ///////////////////////////////////////////// |
---|
| 704 | static void cmd_pwd( int argc , char **argv ) |
---|
[230] | 705 | { |
---|
[407] | 706 | char buf[1024]; |
---|
[230] | 707 | |
---|
[407] | 708 | if (argc != 1) |
---|
| 709 | { |
---|
[473] | 710 | printf(" usage: %s\n", argv[0]); |
---|
[407] | 711 | return; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | if ( getcwd( buf , 1024 ) ) |
---|
| 715 | { |
---|
[409] | 716 | printf(" error: unable to get current directory\n"); |
---|
[407] | 717 | } |
---|
| 718 | else |
---|
| 719 | { |
---|
| 720 | printf("%s\n", buf); |
---|
| 721 | } |
---|
| 722 | |
---|
[457] | 723 | // release semaphore to get next command |
---|
| 724 | sem_post( &semaphore ); |
---|
| 725 | |
---|
| 726 | } // end cmd_pwd() |
---|
| 727 | |
---|
[407] | 728 | //////////////////////////////////////////// |
---|
| 729 | static void cmd_rm( int argc , char **argv ) |
---|
| 730 | { |
---|
| 731 | char * pathname; |
---|
| 732 | |
---|
| 733 | if (argc != 2) |
---|
| 734 | { |
---|
[473] | 735 | printf(" usage: %s pathname\n", argv[0]); |
---|
[230] | 736 | return; |
---|
| 737 | } |
---|
| 738 | |
---|
[407] | 739 | pathname = argv[1]; |
---|
[230] | 740 | |
---|
[409] | 741 | printf(" error: not implemented yet\n"); |
---|
[407] | 742 | |
---|
[457] | 743 | // release semaphore to get next command |
---|
| 744 | sem_post( &semaphore ); |
---|
[230] | 745 | |
---|
[457] | 746 | } // end_cmd_rm() |
---|
| 747 | |
---|
[407] | 748 | /////////////////////////////////////////////// |
---|
| 749 | static void cmd_rmdir( int argc , char **argv ) |
---|
[230] | 750 | { |
---|
[457] | 751 | // same as cmd_rm() |
---|
[230] | 752 | cmd_rm(argc, argv); |
---|
| 753 | } |
---|
| 754 | |
---|
[442] | 755 | /////////////////////////////////////////////// |
---|
| 756 | static void cmd_trace( int argc , char **argv ) |
---|
| 757 | { |
---|
| 758 | unsigned int cxy; |
---|
| 759 | unsigned int lid; |
---|
| 760 | |
---|
| 761 | if (argc != 3) |
---|
| 762 | { |
---|
| 763 | printf(" usage: trace cxy lid \n"); |
---|
| 764 | return; |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | cxy = atoi(argv[1]); |
---|
| 768 | lid = atoi(argv[2]); |
---|
| 769 | |
---|
| 770 | if( trace( 1 , cxy , lid ) ) |
---|
| 771 | { |
---|
| 772 | printf(" error: core[%x,%d] not found\n", cxy, lid ); |
---|
| 773 | } |
---|
| 774 | |
---|
[457] | 775 | // release semaphore to get next command |
---|
| 776 | sem_post( &semaphore ); |
---|
| 777 | |
---|
| 778 | } // end cmd_trace |
---|
| 779 | |
---|
[442] | 780 | /////////////////////////////////////////////// |
---|
| 781 | static void cmd_untrace( int argc , char **argv ) |
---|
| 782 | { |
---|
| 783 | unsigned int cxy; |
---|
| 784 | unsigned int lid; |
---|
| 785 | |
---|
| 786 | if (argc != 3) |
---|
| 787 | { |
---|
| 788 | printf(" usage: untrace cxy lid \n"); |
---|
| 789 | return; |
---|
| 790 | } |
---|
| 791 | |
---|
| 792 | cxy = atoi(argv[1]); |
---|
| 793 | lid = atoi(argv[2]); |
---|
| 794 | |
---|
| 795 | if( trace( 0 , cxy , lid ) ) |
---|
| 796 | { |
---|
| 797 | printf(" error: core[%x,%d] not found\n", cxy, lid ); |
---|
| 798 | } |
---|
| 799 | |
---|
[457] | 800 | // release semaphore to get next command |
---|
| 801 | sem_post( &semaphore ); |
---|
| 802 | |
---|
| 803 | } // end cmd_untrace() |
---|
| 804 | |
---|
| 805 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 806 | // Array of commands |
---|
[457] | 807 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 808 | |
---|
[407] | 809 | ksh_cmd_t cmd[] = |
---|
[230] | 810 | { |
---|
[435] | 811 | { "cat", "display file content", cmd_cat }, |
---|
| 812 | { "cd", "change current directory", cmd_cd }, |
---|
| 813 | { "cp", "replicate a file in file system", cmd_cp }, |
---|
| 814 | { "fg", "put a process in foreground", cmd_fg }, |
---|
| 815 | { "display", "display vmm/sched/process/vfs/chdev/txt", cmd_display }, |
---|
| 816 | { "load", "load an user application", cmd_load }, |
---|
| 817 | { "help", "list available commands", cmd_help }, |
---|
[457] | 818 | { "kill", "kill a process (all threads)", cmd_kill }, |
---|
[435] | 819 | { "log", "list registered commands", cmd_log }, |
---|
| 820 | { "ls", "list directory entries", cmd_ls }, |
---|
| 821 | { "mkdir", "create a new directory", cmd_mkdir }, |
---|
| 822 | { "mv", "move a file in file system", cmd_mv }, |
---|
| 823 | { "pwd", "print current working directory", cmd_pwd }, |
---|
[588] | 824 | { "ps", "display all processes", cmd_ps }, |
---|
[435] | 825 | { "rm", "remove a file from file system", cmd_rm }, |
---|
| 826 | { "rmdir", "remove a directory from file system", cmd_rmdir }, |
---|
[442] | 827 | { "trace", "activate trace for a given core", cmd_trace }, |
---|
| 828 | { "untrace", "desactivate trace for a given core", cmd_untrace }, |
---|
[435] | 829 | { NULL, NULL, NULL } |
---|
[230] | 830 | }; |
---|
| 831 | |
---|
[407] | 832 | //////////////////////////////////////////////////////////////////////////////////// |
---|
[457] | 833 | // This function analyses one command (with arguments), executes it, and returns. |
---|
[407] | 834 | //////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 835 | static void __attribute__ ((noinline)) parse( char * buf ) |
---|
[230] | 836 | { |
---|
| 837 | int argc = 0; |
---|
| 838 | char *argv[MAX_ARGS]; |
---|
| 839 | int i; |
---|
| 840 | int len = strlen(buf); |
---|
| 841 | |
---|
| 842 | // build argc/argv |
---|
[407] | 843 | for (i = 0; i < len; i++) |
---|
| 844 | { |
---|
| 845 | if (buf[i] == ' ') |
---|
| 846 | { |
---|
[230] | 847 | buf[i] = '\0'; |
---|
[407] | 848 | } |
---|
| 849 | else if (i == 0 || buf[i - 1] == '\0') |
---|
| 850 | { |
---|
| 851 | if (argc < MAX_ARGS) |
---|
| 852 | { |
---|
[230] | 853 | argv[argc] = &buf[i]; |
---|
| 854 | argc++; |
---|
| 855 | } |
---|
| 856 | } |
---|
| 857 | } |
---|
| 858 | |
---|
[469] | 859 | // analyse command type |
---|
[407] | 860 | if (argc > 0) |
---|
| 861 | { |
---|
[230] | 862 | int found = 0; |
---|
| 863 | |
---|
| 864 | argv[argc] = NULL; |
---|
| 865 | |
---|
[407] | 866 | // try to match typed command |
---|
[469] | 867 | for (i = 0 ; cmd[i].name ; i++) |
---|
[407] | 868 | { |
---|
| 869 | if (strcmp(argv[0], cmd[i].name) == 0) |
---|
| 870 | { |
---|
[230] | 871 | cmd[i].fn(argc, argv); |
---|
| 872 | found = 1; |
---|
| 873 | break; |
---|
| 874 | } |
---|
| 875 | } |
---|
| 876 | |
---|
[457] | 877 | if (!found) // undefined command |
---|
[407] | 878 | { |
---|
[457] | 879 | printf(" error : undefined command <%s>\n", argv[0]); |
---|
| 880 | |
---|
| 881 | // release semaphore to get next command |
---|
| 882 | sem_post( &semaphore ); |
---|
[230] | 883 | } |
---|
| 884 | } |
---|
[446] | 885 | } // end parse() |
---|
[230] | 886 | |
---|
[574] | 887 | /////////////////////////////// |
---|
[503] | 888 | static void interactive( void ) |
---|
[230] | 889 | { |
---|
[469] | 890 | char c; // read character |
---|
| 891 | char buf[CMD_MAX_SIZE]; // buffer for one command |
---|
| 892 | unsigned int end_command; // last character found in a command |
---|
| 893 | unsigned int count; // pointer in command buffer |
---|
| 894 | unsigned int i; // index for loops |
---|
| 895 | unsigned int state; // escape sequence state |
---|
[230] | 896 | |
---|
[457] | 897 | |
---|
[588] | 898 | /* To lauch one application without interactive mode |
---|
| 899 | |
---|
| 900 | if( sem_wait( &semaphore ) ) |
---|
[469] | 901 | { |
---|
[588] | 902 | printf("\n[ksh error] cannot found semafore\n" ); |
---|
| 903 | exit( 1 ); |
---|
[469] | 904 | } |
---|
[588] | 905 | else |
---|
| 906 | { |
---|
| 907 | printf("\n[ksh] for fft\n"); |
---|
| 908 | } |
---|
[469] | 909 | |
---|
[588] | 910 | strcpy( buf , "load /bin/user/fft.elf" ); |
---|
| 911 | parse( buf ); |
---|
| 912 | |
---|
[469] | 913 | */ |
---|
| 914 | |
---|
[407] | 915 | enum fsm_states |
---|
| 916 | { |
---|
[457] | 917 | NORMAL = 0, |
---|
| 918 | ESCAPE = 1, |
---|
| 919 | BRAKET = 2, |
---|
[230] | 920 | }; |
---|
| 921 | |
---|
[457] | 922 | // This lexical analyser writes one command line in the command buffer. |
---|
| 923 | // It is implemented as a 3 states FSM to handle the following escape sequences: |
---|
[230] | 924 | // - ESC [ A : up arrow |
---|
| 925 | // - ESC [ B : down arrow |
---|
| 926 | // - ESC [ C : right arrow |
---|
| 927 | // - ESC [ D : left arrow |
---|
[407] | 928 | // The three states have the following semantic: |
---|
[230] | 929 | // - NORMAL : no (ESC) character has been found |
---|
| 930 | // - ESCAPE : the character (ESC) has been found |
---|
| 931 | // - BRAKET : the wo characters (ESC,[) have been found |
---|
[436] | 932 | |
---|
[457] | 933 | // external loop on the commands |
---|
| 934 | // the in teractive thread should not exit this loop |
---|
[230] | 935 | while (1) |
---|
| 936 | { |
---|
[457] | 937 | // initialize command buffer |
---|
| 938 | memset( buf, 0x20 , sizeof(buf) ); // TODO useful ? |
---|
| 939 | count = 0; |
---|
| 940 | state = NORMAL; |
---|
[230] | 941 | |
---|
[469] | 942 | // decrement semaphore, and block if the KSH process is not the TXT owner |
---|
| 943 | if ( sem_wait( &semaphore ) ) |
---|
| 944 | { |
---|
| 945 | printf("\n[ksh error] cannot found semafore\n" ); |
---|
| 946 | exit( 1 ); |
---|
| 947 | } |
---|
[407] | 948 | |
---|
[457] | 949 | // display prompt on a new line |
---|
| 950 | printf("\n[ksh] "); |
---|
| 951 | |
---|
| 952 | end_command = 0; |
---|
| 953 | |
---|
| 954 | // internal loop on characters in one command |
---|
| 955 | while( end_command == 0 ) |
---|
| 956 | { |
---|
| 957 | // get one character from TXT_RX |
---|
| 958 | c = (char)getchar(); |
---|
| 959 | |
---|
| 960 | if( c == 0 ) continue; |
---|
| 961 | |
---|
| 962 | if( state == NORMAL ) // we are not in an escape sequence |
---|
| 963 | { |
---|
[230] | 964 | if ((c == '\b') || (c == 0x7F)) // backspace => remove one character |
---|
| 965 | { |
---|
[457] | 966 | if (count > 0) |
---|
| 967 | { |
---|
| 968 | printf("\b \b"); |
---|
| 969 | count--; |
---|
| 970 | } |
---|
[230] | 971 | } |
---|
[457] | 972 | else if (c == '\n') // new line => end of command |
---|
[230] | 973 | { |
---|
[457] | 974 | if (count > 0) // analyse & execute command |
---|
| 975 | { |
---|
| 976 | // complete command with NUL character |
---|
| 977 | buf[count] = 0; |
---|
| 978 | count++; |
---|
[230] | 979 | |
---|
[457] | 980 | // register command in log arrays |
---|
| 981 | strcpy(log_entries[ptw].buf, buf); |
---|
| 982 | log_entries[ptw].count = count; |
---|
| 983 | ptw = (ptw + 1) % LOG_DEPTH; |
---|
| 984 | ptr = ptw; |
---|
[230] | 985 | |
---|
[457] | 986 | // echo character |
---|
| 987 | putchar( c ); |
---|
[230] | 988 | |
---|
[457] | 989 | // call parser to analyse and execute command |
---|
| 990 | parse( buf ); |
---|
| 991 | } |
---|
| 992 | else // no command registered |
---|
[441] | 993 | { |
---|
[457] | 994 | // release semaphore to get next command |
---|
| 995 | sem_post( &semaphore ); |
---|
[441] | 996 | } |
---|
[457] | 997 | |
---|
| 998 | // exit internal loop on characters |
---|
| 999 | end_command = 1; |
---|
| 1000 | } |
---|
| 1001 | else if (c == '\t') // tabulation => do nothing |
---|
| 1002 | { |
---|
| 1003 | } |
---|
| 1004 | else if (c == (char)0x1B) // ESC => start an escape sequence |
---|
| 1005 | { |
---|
| 1006 | state = ESCAPE; |
---|
| 1007 | } |
---|
| 1008 | else // normal character |
---|
[230] | 1009 | { |
---|
[457] | 1010 | if (count < sizeof(buf) - 1) |
---|
| 1011 | { |
---|
| 1012 | // register character in command buffer |
---|
| 1013 | buf[count] = c; |
---|
| 1014 | count++; |
---|
| 1015 | |
---|
| 1016 | // echo character |
---|
| 1017 | putchar( c ); |
---|
[230] | 1018 | } |
---|
| 1019 | } |
---|
| 1020 | } |
---|
[457] | 1021 | else if( state == ESCAPE ) |
---|
[230] | 1022 | { |
---|
| 1023 | if (c == '[') // valid sequence => continue |
---|
| 1024 | { |
---|
| 1025 | state = BRAKET; |
---|
| 1026 | } |
---|
| 1027 | else // invalid sequence => do nothing |
---|
| 1028 | { |
---|
| 1029 | state = NORMAL; |
---|
| 1030 | } |
---|
| 1031 | } |
---|
[457] | 1032 | else if( state == BRAKET ) |
---|
[230] | 1033 | { |
---|
| 1034 | if (c == 'D') // valid LEFT sequence => move buf pointer left |
---|
| 1035 | { |
---|
| 1036 | if (count > 0) |
---|
| 1037 | { |
---|
| 1038 | printf("\b"); |
---|
| 1039 | count--; |
---|
| 1040 | } |
---|
| 1041 | |
---|
| 1042 | // get next user char |
---|
| 1043 | state = NORMAL; |
---|
| 1044 | } |
---|
| 1045 | else if (c == 'C') // valid RIGHT sequence => move buf pointer right |
---|
| 1046 | { |
---|
| 1047 | if (count < sizeof(buf) - 1) |
---|
| 1048 | { |
---|
| 1049 | printf("%c", buf[count]); |
---|
| 1050 | count++; |
---|
| 1051 | } |
---|
| 1052 | |
---|
| 1053 | // get next user char |
---|
| 1054 | state = NORMAL; |
---|
| 1055 | } |
---|
| 1056 | else if (c == 'A') // valid UP sequence => move log pointer backward |
---|
| 1057 | { |
---|
| 1058 | // cancel current command |
---|
| 1059 | for (i = 0; i < count; i++) printf("\b \b"); |
---|
| 1060 | count = 0; |
---|
| 1061 | |
---|
| 1062 | // copy log command into buf |
---|
| 1063 | ptr = (ptr - 1) % LOG_DEPTH; |
---|
| 1064 | strcpy(buf, log_entries[ptr].buf); |
---|
[458] | 1065 | count = log_entries[ptr].count - 1; |
---|
[230] | 1066 | |
---|
| 1067 | // display log command |
---|
| 1068 | printf("%s", buf); |
---|
| 1069 | |
---|
| 1070 | // get next user char |
---|
| 1071 | state = NORMAL; |
---|
| 1072 | } |
---|
| 1073 | else if (c == 'B') // valid DOWN sequence => move log pointer forward |
---|
| 1074 | { |
---|
| 1075 | // cancel current command |
---|
| 1076 | for (i = 0 ; i < count; i++) printf("\b \b"); |
---|
| 1077 | count = 0; |
---|
| 1078 | |
---|
| 1079 | // copy log command into buf |
---|
| 1080 | ptr = (ptr + 1) % LOG_DEPTH; |
---|
| 1081 | strcpy(buf, log_entries[ptr].buf); |
---|
| 1082 | count = log_entries[ptr].count; |
---|
| 1083 | |
---|
| 1084 | // display log command |
---|
| 1085 | printf("%s", buf); |
---|
| 1086 | |
---|
| 1087 | // get next user char |
---|
| 1088 | state = NORMAL; |
---|
| 1089 | } |
---|
| 1090 | else // other character => do nothing |
---|
| 1091 | { |
---|
| 1092 | // get next user char |
---|
| 1093 | state = NORMAL; |
---|
| 1094 | } |
---|
| 1095 | } |
---|
[457] | 1096 | } // end internal while loop on characters |
---|
| 1097 | } // end external while loop on commands |
---|
[446] | 1098 | } // end interactive() |
---|
| 1099 | |
---|
[574] | 1100 | //////////////// |
---|
[503] | 1101 | int main( void ) |
---|
[446] | 1102 | { |
---|
| 1103 | unsigned int cxy; // owner cluster identifier for this KSH process |
---|
| 1104 | unsigned int lid; // core identifier for this KSH main thread |
---|
| 1105 | int status; // child process termination status |
---|
[457] | 1106 | int child_pid; // child process identifier |
---|
[469] | 1107 | int parent_pid; // parent process identifier (i.e. this process) |
---|
| 1108 | pthread_t trdid; // interactive thread identifier (unused) |
---|
[457] | 1109 | unsigned int is_owner; // non-zero if KSH process is TXT owner |
---|
[446] | 1110 | |
---|
| 1111 | // initialize log buffer |
---|
| 1112 | memset( &log_entries , 0, sizeof(log_entries)); |
---|
| 1113 | ptw = 0; |
---|
| 1114 | ptr = 0; |
---|
| 1115 | |
---|
[457] | 1116 | // get KSH process pid and core |
---|
| 1117 | parent_pid = getpid(); |
---|
[459] | 1118 | get_core( &cxy , &lid ); |
---|
[457] | 1119 | |
---|
[574] | 1120 | #if MAIN_DEBUG |
---|
| 1121 | printf("\n[ksh] main started on core[%x,%d]\n", cxy , lid ); |
---|
| 1122 | #endif |
---|
| 1123 | |
---|
| 1124 | // initializes the semaphore used to synchronize with interactive thread |
---|
[469] | 1125 | if ( sem_init( &semaphore , 0 , 1 ) ) |
---|
| 1126 | { |
---|
| 1127 | printf("\n[KSH ERROR] cannot initialize semaphore\n" ); |
---|
| 1128 | exit( 1 ); |
---|
| 1129 | } |
---|
[457] | 1130 | |
---|
[574] | 1131 | #if MAIN_DEBUG |
---|
| 1132 | printf("\n[ksh] main initialized semaphore\n" ); |
---|
| 1133 | #endif |
---|
| 1134 | |
---|
[446] | 1135 | // initialize interactive thread attributes |
---|
| 1136 | attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED; |
---|
| 1137 | attr.cxy = cxy; |
---|
| 1138 | |
---|
| 1139 | // lauch the interactive thread |
---|
| 1140 | pthread_create( &trdid, |
---|
| 1141 | &attr, |
---|
| 1142 | &interactive, // entry function |
---|
| 1143 | NULL ); |
---|
[574] | 1144 | #if MAIN_DEBUG |
---|
| 1145 | printf("\n[ksh] main launched interactive thread => wait children termination\n" ); |
---|
| 1146 | #endif |
---|
[588] | 1147 | |
---|
| 1148 | // signal INIT process |
---|
| 1149 | kill( 1 , SIGCONT ); |
---|
[446] | 1150 | |
---|
| 1151 | // enter infinite loop monitoring children processes termination |
---|
| 1152 | while( 1 ) |
---|
| 1153 | { |
---|
[457] | 1154 | // wait children termination |
---|
| 1155 | child_pid = wait( &status ); |
---|
[446] | 1156 | |
---|
[574] | 1157 | #if MAIN_DEBUG |
---|
| 1158 | if( WIFEXITED (status) ) printf("\n[ksh] child process %x exit\n" , child_pid ); |
---|
| 1159 | if( WIFSIGNALED(status) ) printf("\n[ksh] child process %x killed\n" , child_pid ); |
---|
| 1160 | if( WIFSTOPPED (status) ) printf("\n[ksh] child process %x stopped\n", child_pid ); |
---|
[446] | 1161 | #endif |
---|
| 1162 | |
---|
[457] | 1163 | // release semaphore if KSH process is TXT owner, to unblock interactive thread |
---|
| 1164 | is_fg( parent_pid , &is_owner ); |
---|
| 1165 | if( is_owner ) sem_post( &semaphore ); |
---|
| 1166 | |
---|
[446] | 1167 | } |
---|
[436] | 1168 | } // end main() |
---|
[230] | 1169 | |
---|
[446] | 1170 | |
---|