[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 |
---|
[624] | 16 | // attached to the same TXT terminal as the parent KSH process. |
---|
[469] | 17 | // A child process can be lauched in foreground or in background: |
---|
[624] | 18 | // . when the child process is launched in foreground, the KSH process loses |
---|
[469] | 19 | // the TXT terminal ownership, that is transfered to the child process. |
---|
[624] | 20 | // . when the child process is launched in background, the KSH process keeps |
---|
[469] | 21 | // the TXT terminal ownership. |
---|
| 22 | // |
---|
[619] | 23 | // We use a semaphore to synchronize the two KSH threads. After each command |
---|
| 24 | // completion, the interactive thread check the TXT ownership (with a sem_wait), |
---|
| 25 | // and blocks, if the KSH process loosed the TXT ownership (after a load, |
---|
| 26 | // or for any other cause). It is unblocked with the following policy: |
---|
[469] | 27 | // . if the command is "not a load", the semaphore is incremented by the |
---|
[619] | 28 | // cmd_***() function when the command is completed, to get the next command |
---|
| 29 | // in the while loop. |
---|
[469] | 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> |
---|
[611] | 45 | #include <dirent.h> |
---|
[445] | 46 | #include <almosmkh.h> |
---|
[457] | 47 | #include <semaphore.h> |
---|
[588] | 48 | #include <hal_macros.h> |
---|
[596] | 49 | #include <sys/stat.h> |
---|
| 50 | #include <sys/mman.h> |
---|
| 51 | #include <fcntl.h> |
---|
[230] | 52 | |
---|
[407] | 53 | #define CMD_MAX_SIZE (256) // max number of characters in one command |
---|
[436] | 54 | #define LOG_DEPTH (32) // max number of registered commands |
---|
[407] | 55 | #define MAX_ARGS (32) // max number of arguments in a command |
---|
[230] | 56 | |
---|
[611] | 57 | #define DEBUG_MAIN 0 |
---|
[619] | 58 | #define DEBUG_INTER 0 |
---|
| 59 | #define DEBUG_PARSE 0 |
---|
[624] | 60 | #define DEBUG_CMD_CAT 0 |
---|
[611] | 61 | #define DEBUG_CMD_CP 0 |
---|
| 62 | #define DEBUG_CMD_LOAD 0 |
---|
| 63 | #define DEBUG_CMD_LS 0 |
---|
[619] | 64 | #define DEBUG_CMD_PS 0 |
---|
[457] | 65 | |
---|
[469] | 66 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 67 | // Structures |
---|
[469] | 68 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 69 | |
---|
[407] | 70 | // one entry in the registered commands array |
---|
| 71 | typedef struct log_entry_s |
---|
| 72 | { |
---|
| 73 | char buf[CMD_MAX_SIZE]; |
---|
| 74 | unsigned int count; |
---|
| 75 | } |
---|
| 76 | log_entry_t; |
---|
[230] | 77 | |
---|
[407] | 78 | // one entry in the supported command types array |
---|
| 79 | typedef struct ksh_cmd_s |
---|
| 80 | { |
---|
| 81 | char * name; |
---|
| 82 | char * desc; |
---|
| 83 | void (*fn)( int , char ** ); |
---|
| 84 | } |
---|
| 85 | ksh_cmd_t; |
---|
| 86 | |
---|
| 87 | |
---|
[469] | 88 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 89 | // Global Variables |
---|
[469] | 90 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 91 | |
---|
[619] | 92 | ksh_cmd_t command[]; // array of supported commands |
---|
[230] | 93 | |
---|
[407] | 94 | log_entry_t log_entries[LOG_DEPTH]; // array of registered commands |
---|
[230] | 95 | |
---|
[407] | 96 | unsigned int ptw; // write pointer in log_entries[] |
---|
| 97 | unsigned int ptr; // read pointer in log_entries[] |
---|
[230] | 98 | |
---|
[457] | 99 | pthread_attr_t attr; // interactive thread attributes |
---|
[446] | 100 | |
---|
[457] | 101 | sem_t semaphore; // block interactive thread when zero |
---|
| 102 | |
---|
[619] | 103 | pthread_t trdid; // interactive thread identifier |
---|
| 104 | |
---|
[469] | 105 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 106 | // Shell Commands |
---|
[469] | 107 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 108 | |
---|
[407] | 109 | ///////////////////////////////////////////// |
---|
| 110 | static void cmd_cat( int argc , char **argv ) |
---|
[230] | 111 | { |
---|
[407] | 112 | char * path; |
---|
[611] | 113 | struct stat st; |
---|
[596] | 114 | int fd; |
---|
| 115 | int size; |
---|
| 116 | char * buf; |
---|
[230] | 117 | |
---|
[619] | 118 | #if DEBUG_CMD_CAT |
---|
| 119 | char string[64]; |
---|
| 120 | #endif |
---|
| 121 | |
---|
[407] | 122 | if (argc != 2) |
---|
| 123 | { |
---|
[409] | 124 | printf(" usage: cat pathname\n"); |
---|
[623] | 125 | |
---|
| 126 | sem_post( &semaphore ); |
---|
| 127 | return; |
---|
[596] | 128 | } |
---|
[407] | 129 | |
---|
[596] | 130 | path = argv[1]; |
---|
[230] | 131 | |
---|
[596] | 132 | // open the file |
---|
| 133 | fd = open( path , O_RDONLY , 0 ); |
---|
| 134 | if (fd < 0) |
---|
| 135 | { |
---|
[611] | 136 | printf(" error: cannot open file <%s>\n", path); |
---|
[623] | 137 | |
---|
| 138 | sem_post( &semaphore ); |
---|
| 139 | return; |
---|
[596] | 140 | } |
---|
[407] | 141 | |
---|
[611] | 142 | #if DEBUG_CMD_CAT |
---|
[623] | 143 | snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, path ); |
---|
[619] | 144 | display_string( string ); |
---|
[596] | 145 | #endif |
---|
| 146 | |
---|
| 147 | // get file stats |
---|
| 148 | if ( stat( path , &st ) == -1) |
---|
[407] | 149 | { |
---|
[611] | 150 | printf(" error: cannot stat <%s>\n", path); |
---|
[623] | 151 | |
---|
| 152 | close(fd); |
---|
| 153 | sem_post( &semaphore ); |
---|
| 154 | return; |
---|
[596] | 155 | } |
---|
[230] | 156 | |
---|
[596] | 157 | if ( S_ISDIR(st.st_mode) ) |
---|
[407] | 158 | { |
---|
[611] | 159 | printf(" error: <%s> is a directory\n", path); |
---|
[623] | 160 | |
---|
| 161 | close(fd); |
---|
| 162 | sem_post( &semaphore ); |
---|
| 163 | return; |
---|
[596] | 164 | } |
---|
[230] | 165 | |
---|
[596] | 166 | // get file size |
---|
| 167 | size = st.st_size; |
---|
[230] | 168 | |
---|
[611] | 169 | #if DEBUG_CMD_CAT |
---|
[623] | 170 | snprintf( string , 64 , "[ksh] %s : size = %d", __FUNCTION__, size ); |
---|
[619] | 171 | display_string( string ); |
---|
[596] | 172 | #endif |
---|
[230] | 173 | |
---|
[623] | 174 | if( size == 0 ) |
---|
| 175 | { |
---|
| 176 | printf(" error: size = 0 for <%s>\n", path); |
---|
| 177 | |
---|
| 178 | close(fd); |
---|
| 179 | sem_post( &semaphore ); |
---|
| 180 | return; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | // mapping type is MAP_FILE when MAP_ANON and MAP_REMOTE are not specified |
---|
[596] | 184 | buf = mmap( NULL , size , PROT_READ|PROT_WRITE , MAP_PRIVATE , fd , 0 ); |
---|
[230] | 185 | |
---|
[596] | 186 | if ( buf == NULL ) |
---|
| 187 | { |
---|
[611] | 188 | printf(" error: cannot map file <%s>\n", path ); |
---|
[623] | 189 | |
---|
| 190 | close(fd); |
---|
| 191 | sem_post( &semaphore ); |
---|
| 192 | return; |
---|
[596] | 193 | } |
---|
| 194 | |
---|
[611] | 195 | #if DEBUG_CMD_CAT |
---|
[623] | 196 | snprintf( string , 64 , "[ksh] %s : maped file %d to buffer %x", __FUNCTION__, fd , buf ); |
---|
[619] | 197 | display_string( string ); |
---|
[623] | 198 | // unsigned int pid = getpid(); |
---|
| 199 | // unsigned int cxy = pid >> 16; |
---|
| 200 | // display_vmm( cxy , pid ); |
---|
[596] | 201 | #endif |
---|
| 202 | |
---|
| 203 | // display the file content on TXT terminal |
---|
| 204 | write( 1 , buf , size ); |
---|
| 205 | |
---|
[623] | 206 | // unmap te file |
---|
| 207 | if( munmap( buf , size ) ) |
---|
| 208 | { |
---|
| 209 | printf(" error: cannot unmap file <%s>\n", path ); |
---|
| 210 | } |
---|
[596] | 211 | |
---|
[623] | 212 | #if DEBUG_CMD_CAT |
---|
| 213 | snprintf( string , 64 , "[ksh] %s : unmaped file %d from buffer %x", __FUNCTION__, fd , buf ); |
---|
| 214 | display_string( string ); |
---|
| 215 | // display_vmm( cxy , pid ); |
---|
| 216 | #endif |
---|
[596] | 217 | |
---|
[623] | 218 | // close the file |
---|
| 219 | if( close( fd ) ) |
---|
| 220 | { |
---|
| 221 | printf(" error: cannot close file <%s>\n", path ); |
---|
| 222 | } |
---|
[596] | 223 | |
---|
[457] | 224 | // release semaphore to get next command |
---|
| 225 | sem_post( &semaphore ); |
---|
| 226 | |
---|
[407] | 227 | } // end cmd_cat() |
---|
| 228 | |
---|
| 229 | //////////////////////////////////////////// |
---|
| 230 | static void cmd_cd( int argc , char **argv ) |
---|
| 231 | { |
---|
| 232 | char * path; |
---|
| 233 | |
---|
| 234 | if (argc != 2) |
---|
| 235 | { |
---|
[409] | 236 | printf(" usage: cd pathname\n"); |
---|
[407] | 237 | } |
---|
[596] | 238 | else |
---|
| 239 | { |
---|
| 240 | path = argv[1]; |
---|
[407] | 241 | |
---|
[610] | 242 | // call the relevant syscall |
---|
| 243 | if( chdir( path ) ) |
---|
| 244 | { |
---|
| 245 | printf(" error: cannot found <%s> directory\n", path ); |
---|
| 246 | } |
---|
[596] | 247 | } |
---|
[407] | 248 | |
---|
[457] | 249 | // release semaphore to get next command |
---|
| 250 | sem_post( &semaphore ); |
---|
[407] | 251 | |
---|
| 252 | } // end cmd_cd() |
---|
| 253 | |
---|
| 254 | ///////////////////////////////////////// |
---|
[230] | 255 | static void cmd_cp(int argc, char **argv) |
---|
| 256 | { |
---|
[611] | 257 | int src_fd; |
---|
| 258 | int dst_fd; |
---|
| 259 | char * srcpath; |
---|
| 260 | char * dstpath; |
---|
| 261 | int size; // source file size |
---|
| 262 | int bytes; // number of transfered bytes |
---|
| 263 | char buf[4096]; |
---|
| 264 | struct stat st; |
---|
[230] | 265 | |
---|
[619] | 266 | #if DEBUG_CMD_CP |
---|
| 267 | char string[64]; |
---|
| 268 | #endif |
---|
| 269 | |
---|
[407] | 270 | if (argc != 3) |
---|
| 271 | { |
---|
[596] | 272 | src_fd = -1; |
---|
| 273 | dst_fd = -1; |
---|
[409] | 274 | printf(" usage: cp src_pathname dst_pathname\n"); |
---|
[611] | 275 | goto cmd_cp_exit; |
---|
[230] | 276 | } |
---|
| 277 | |
---|
[596] | 278 | srcpath = argv[1]; |
---|
| 279 | dstpath = argv[2]; |
---|
[407] | 280 | |
---|
[596] | 281 | // open the src file |
---|
| 282 | src_fd = open( srcpath , O_RDONLY , 0 ); |
---|
[230] | 283 | |
---|
[596] | 284 | if ( src_fd < 0 ) |
---|
| 285 | { |
---|
| 286 | dst_fd = -1; |
---|
[611] | 287 | printf(" error: cannot open <%s>\n", srcpath ); |
---|
| 288 | goto cmd_cp_exit; |
---|
[596] | 289 | } |
---|
[230] | 290 | |
---|
[611] | 291 | #if DEBUG_CMD_CP |
---|
[623] | 292 | snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, srcpath ); |
---|
[619] | 293 | display_string( string ); |
---|
[610] | 294 | #endif |
---|
| 295 | |
---|
[596] | 296 | // get file stats |
---|
| 297 | if ( stat( srcpath , &st ) ) |
---|
| 298 | { |
---|
| 299 | dst_fd = -1; |
---|
[611] | 300 | printf(" error: cannot stat <%s>\n", srcpath); |
---|
| 301 | goto cmd_cp_exit; |
---|
[596] | 302 | } |
---|
| 303 | |
---|
[611] | 304 | #if DEBUG_CMD_CP |
---|
[623] | 305 | snprintf( string , 64 , "[ksh] %s : got stats for %s", __FUNCTION__, srcpath ); |
---|
[619] | 306 | display_string( string ); |
---|
[610] | 307 | #endif |
---|
| 308 | |
---|
[596] | 309 | if ( S_ISDIR(st.st_mode) ) |
---|
| 310 | { |
---|
| 311 | dst_fd = -1; |
---|
[611] | 312 | printf(" error: <%s> is a directory\n", srcpath); |
---|
| 313 | goto cmd_cp_exit; |
---|
[230] | 314 | } |
---|
[596] | 315 | |
---|
| 316 | // get src file size |
---|
[230] | 317 | size = st.st_size; |
---|
| 318 | |
---|
[407] | 319 | // open the dst file |
---|
[596] | 320 | dst_fd = open( dstpath , O_CREAT|O_TRUNC|O_RDWR , 0 ); |
---|
| 321 | |
---|
| 322 | if ( dst_fd < 0 ) |
---|
| 323 | { |
---|
[611] | 324 | printf(" error: cannot open <%s>\n", dstpath ); |
---|
| 325 | goto cmd_cp_exit; |
---|
[230] | 326 | } |
---|
[596] | 327 | |
---|
[611] | 328 | #if DEBUG_CMD_CP |
---|
[623] | 329 | snprintf( string , 64 , "[ksh] %s : file %s open", __FUNCTION__, dstpath ); |
---|
[619] | 330 | display_string( string ); |
---|
[610] | 331 | #endif |
---|
| 332 | |
---|
[596] | 333 | if ( stat( dstpath , &st ) ) |
---|
| 334 | { |
---|
[611] | 335 | printf(" error: cannot stat <%s>\n", dstpath ); |
---|
| 336 | goto cmd_cp_exit; |
---|
[230] | 337 | } |
---|
[596] | 338 | |
---|
[611] | 339 | #if DEBUG_CMD_CP |
---|
[623] | 340 | snprintf( string , 64 , "[ksh] %s : got stats for %s", __FUNCTION__, dstpath ); |
---|
[619] | 341 | display_string( string ); |
---|
[610] | 342 | #endif |
---|
| 343 | |
---|
[596] | 344 | if ( S_ISDIR(st.st_mode ) ) |
---|
| 345 | { |
---|
[611] | 346 | printf(" error: <%s> is a directory\n", dstpath ); |
---|
| 347 | goto cmd_cp_exit; |
---|
[230] | 348 | } |
---|
| 349 | |
---|
[596] | 350 | bytes = 0; |
---|
| 351 | |
---|
| 352 | while (bytes < size) |
---|
[230] | 353 | { |
---|
[608] | 354 | int len = ((size - bytes) < 4096) ? (size - bytes) : 4096; |
---|
[230] | 355 | |
---|
[407] | 356 | // read the source |
---|
[608] | 357 | if ( read( src_fd , buf , len ) != len ) |
---|
[596] | 358 | { |
---|
[611] | 359 | printf(" error: cannot read from file <%s>\n", srcpath); |
---|
| 360 | goto cmd_cp_exit; |
---|
[230] | 361 | } |
---|
| 362 | |
---|
[611] | 363 | #if DEBUG_CMD_CP |
---|
[623] | 364 | snprintf( string , 64 , "[ksh] %s : read %d bytes from %s", __FUNCTION__, len, srcpath ); |
---|
[619] | 365 | display_string( string ); |
---|
[610] | 366 | #endif |
---|
| 367 | |
---|
[407] | 368 | // write to the destination |
---|
[608] | 369 | if ( write( dst_fd , buf , len ) != len ) |
---|
[596] | 370 | { |
---|
[611] | 371 | printf(" error: cannot write to file <%s>\n", dstpath); |
---|
| 372 | goto cmd_cp_exit; |
---|
[230] | 373 | } |
---|
| 374 | |
---|
[611] | 375 | #if DEBUG_CMD_CP |
---|
[623] | 376 | snprintf( string , 64 , "[ksh] %s : write %d bytes to %s", __FUNCTION__, len, dstpath ); |
---|
[619] | 377 | display_string( string ); |
---|
[610] | 378 | #endif |
---|
| 379 | |
---|
[608] | 380 | bytes += len; |
---|
[230] | 381 | } |
---|
| 382 | |
---|
[611] | 383 | cmd_cp_exit: |
---|
[596] | 384 | |
---|
[407] | 385 | if (src_fd >= 0) close(src_fd); |
---|
| 386 | if (dst_fd >= 0) close(dst_fd); |
---|
[230] | 387 | |
---|
[457] | 388 | // release semaphore to get next command |
---|
| 389 | sem_post( &semaphore ); |
---|
| 390 | |
---|
[407] | 391 | } // end cmd_cp() |
---|
| 392 | |
---|
[436] | 393 | ///////////////////////////////////////////////// |
---|
| 394 | static void cmd_display( int argc , char **argv ) |
---|
| 395 | { |
---|
[596] | 396 | if( argc < 2 ) |
---|
[436] | 397 | { |
---|
[611] | 398 | printf(" usage: display vmm cxy pid\n" |
---|
| 399 | " display sched cxy lid\n" |
---|
| 400 | " display process cxy\n" |
---|
| 401 | " display txt txtid\n" |
---|
| 402 | " display vfs\n" |
---|
| 403 | " display chdev\n" |
---|
| 404 | " display dqdt\n" |
---|
| 405 | " display locks pid trdid\n" |
---|
[623] | 406 | " display barrier pid\n" |
---|
[611] | 407 | " display mapper path page_id nbytes\n"); |
---|
[596] | 408 | } |
---|
| 409 | //////////////////////////////////// |
---|
| 410 | else if( strcmp( argv[1] , "vmm" ) == 0 ) |
---|
| 411 | { |
---|
[442] | 412 | if( argc != 4 ) |
---|
[436] | 413 | { |
---|
[442] | 414 | printf(" usage: display vmm cxy pid\n"); |
---|
[436] | 415 | } |
---|
[596] | 416 | else |
---|
| 417 | { |
---|
| 418 | unsigned int cxy = atoi(argv[2]); |
---|
| 419 | unsigned int pid = atoi(argv[3]); |
---|
[436] | 420 | |
---|
[596] | 421 | if( display_vmm( cxy , pid ) ) |
---|
| 422 | { |
---|
| 423 | printf(" error: no process %x in cluster %x\n", pid , cxy ); |
---|
| 424 | } |
---|
[436] | 425 | } |
---|
| 426 | } |
---|
[596] | 427 | /////////////////////////////////////////// |
---|
[436] | 428 | else if( strcmp( argv[1] , "sched" ) == 0 ) |
---|
| 429 | { |
---|
| 430 | if( argc != 4 ) |
---|
| 431 | { |
---|
| 432 | printf(" usage: display sched cxy lid\n"); |
---|
| 433 | } |
---|
[596] | 434 | else |
---|
| 435 | { |
---|
| 436 | unsigned int cxy = atoi(argv[2]); |
---|
| 437 | unsigned int lid = atoi(argv[3]); |
---|
[436] | 438 | |
---|
[596] | 439 | if( display_sched( cxy , lid ) ) |
---|
| 440 | { |
---|
| 441 | printf(" error: illegal arguments cxy = %x / lid = %d\n", cxy, lid ); |
---|
| 442 | } |
---|
[436] | 443 | } |
---|
| 444 | } |
---|
[596] | 445 | ///////////////////////////////////////////// |
---|
[436] | 446 | else if( strcmp( argv[1] , "process" ) == 0 ) |
---|
| 447 | { |
---|
| 448 | if( argc != 3 ) |
---|
| 449 | { |
---|
| 450 | printf(" usage: display process cxy\n"); |
---|
| 451 | } |
---|
[596] | 452 | else |
---|
| 453 | { |
---|
| 454 | unsigned int cxy = atoi(argv[2]); |
---|
[436] | 455 | |
---|
[596] | 456 | if( display_cluster_processes( cxy , 0 ) ) |
---|
| 457 | { |
---|
| 458 | printf(" error: illegal argument cxy = %x\n", cxy ); |
---|
| 459 | } |
---|
[436] | 460 | } |
---|
| 461 | } |
---|
[596] | 462 | ///////////////////////////////////////// |
---|
[436] | 463 | else if( strcmp( argv[1] , "txt" ) == 0 ) |
---|
| 464 | { |
---|
| 465 | if( argc != 3 ) |
---|
| 466 | { |
---|
| 467 | printf(" usage: display txt txt_id\n"); |
---|
| 468 | } |
---|
[596] | 469 | else |
---|
| 470 | { |
---|
| 471 | unsigned int txtid = atoi(argv[2]); |
---|
[436] | 472 | |
---|
[596] | 473 | if( display_txt_processes( txtid ) ) |
---|
| 474 | { |
---|
| 475 | printf(" error: illegal argument txtid = %d\n", txtid ); |
---|
| 476 | } |
---|
[436] | 477 | } |
---|
| 478 | } |
---|
[596] | 479 | ///////////////////////////////////////// |
---|
[436] | 480 | else if( strcmp( argv[1] , "vfs" ) == 0 ) |
---|
| 481 | { |
---|
| 482 | if( argc != 2 ) |
---|
| 483 | { |
---|
| 484 | printf(" usage: display vfs\n"); |
---|
| 485 | } |
---|
[596] | 486 | else |
---|
| 487 | { |
---|
| 488 | display_vfs(); |
---|
| 489 | } |
---|
[436] | 490 | } |
---|
[596] | 491 | ////////////////////////////////////////// |
---|
[436] | 492 | else if( strcmp( argv[1] , "chdev" ) == 0 ) |
---|
| 493 | { |
---|
| 494 | if( argc != 2 ) |
---|
| 495 | { |
---|
| 496 | printf(" usage: display chdev\n"); |
---|
| 497 | } |
---|
[596] | 498 | else |
---|
| 499 | { |
---|
| 500 | display_chdev(); |
---|
| 501 | } |
---|
[436] | 502 | } |
---|
[596] | 503 | ////////////////////////////////////////// |
---|
[445] | 504 | else if( strcmp( argv[1] , "dqdt" ) == 0 ) |
---|
| 505 | { |
---|
| 506 | if( argc != 2 ) |
---|
| 507 | { |
---|
| 508 | printf(" usage: display dqdt\n"); |
---|
| 509 | } |
---|
[596] | 510 | else |
---|
| 511 | { |
---|
| 512 | display_dqdt(); |
---|
| 513 | } |
---|
| 514 | } |
---|
| 515 | /////////////////////////////////////////// |
---|
| 516 | else if( strcmp( argv[1] , "locks" ) == 0 ) |
---|
| 517 | { |
---|
| 518 | if( argc != 4 ) |
---|
| 519 | { |
---|
| 520 | printf(" usage: display locks pid trdid\n"); |
---|
| 521 | } |
---|
| 522 | else |
---|
| 523 | { |
---|
| 524 | unsigned int pid = atoi(argv[2]); |
---|
| 525 | unsigned int trdid = atoi(argv[3]); |
---|
[445] | 526 | |
---|
[596] | 527 | if( display_busylocks( pid , trdid ) ) |
---|
| 528 | { |
---|
| 529 | printf(" error: illegal arguments pid = %x / trdid = %x\n", pid, trdid ); |
---|
| 530 | } |
---|
| 531 | } |
---|
[445] | 532 | } |
---|
[623] | 533 | ///////////////////////////////////////////////// |
---|
| 534 | else if( strcmp( argv[1] , "barrier" ) == 0 ) |
---|
| 535 | { |
---|
| 536 | if( argc != 3 ) |
---|
| 537 | { |
---|
| 538 | printf(" usage: display barrier pid\n"); |
---|
| 539 | } |
---|
| 540 | else |
---|
| 541 | { |
---|
| 542 | unsigned int pid = atoi(argv[2]); |
---|
| 543 | |
---|
| 544 | if( display_barrier( pid ) ) |
---|
| 545 | { |
---|
| 546 | printf(" error: illegal arguments pid = %x\n", pid ); |
---|
| 547 | } |
---|
| 548 | } |
---|
| 549 | } |
---|
[611] | 550 | /////////////////////////////////////////// |
---|
| 551 | else if( strcmp( argv[1] , "mapper" ) == 0 ) |
---|
| 552 | { |
---|
| 553 | if( argc != 5 ) |
---|
| 554 | { |
---|
| 555 | printf(" usage: display mapper path page_id nbytes\n"); |
---|
| 556 | } |
---|
| 557 | else |
---|
| 558 | { |
---|
| 559 | unsigned int page_id = atoi(argv[3]); |
---|
| 560 | unsigned int nbytes = atoi(argv[4]); |
---|
| 561 | |
---|
| 562 | if( display_mapper( argv[2] , page_id, nbytes ) ) |
---|
| 563 | { |
---|
| 564 | printf(" error: cannot display page %d of mapper %s\n", page_id, argv[2] ); |
---|
| 565 | } |
---|
| 566 | } |
---|
| 567 | } |
---|
[436] | 568 | else |
---|
| 569 | { |
---|
[596] | 570 | printf(" error: undefined display request : %s\n", argv[1] ); |
---|
| 571 | } |
---|
[457] | 572 | |
---|
| 573 | // release semaphore to get next command |
---|
| 574 | sem_post( &semaphore ); |
---|
| 575 | |
---|
[436] | 576 | } // end cmd_display() |
---|
| 577 | |
---|
[427] | 578 | ///////////////////////////////////////// |
---|
| 579 | static void cmd_fg(int argc, char **argv) |
---|
| 580 | { |
---|
| 581 | unsigned int pid; |
---|
| 582 | |
---|
| 583 | if (argc != 2) |
---|
| 584 | { |
---|
| 585 | printf(" usage: %s pid\n", argv[0]); |
---|
| 586 | } |
---|
[596] | 587 | else |
---|
[427] | 588 | { |
---|
[596] | 589 | pid = atoi( argv[1] ); |
---|
[427] | 590 | |
---|
[596] | 591 | if( pid == 0 ) |
---|
| 592 | { |
---|
| 593 | printf(" error: PID cannot be 0\n" ); |
---|
| 594 | } |
---|
| 595 | else if( fg( pid ) ) |
---|
| 596 | { |
---|
| 597 | printf(" error: cannot find process %x\n", pid ); |
---|
| 598 | } |
---|
| 599 | } |
---|
[457] | 600 | |
---|
| 601 | // release semaphore to get next command |
---|
| 602 | sem_post( &semaphore ); |
---|
| 603 | |
---|
[436] | 604 | } // end cmd_fg() |
---|
[427] | 605 | |
---|
[407] | 606 | ////////////////////////////////////////////// |
---|
| 607 | static void cmd_help( int argc , char **argv ) |
---|
[230] | 608 | { |
---|
[407] | 609 | unsigned int i; |
---|
[230] | 610 | |
---|
[407] | 611 | if (argc != 1) |
---|
| 612 | { |
---|
[409] | 613 | printf(" usage: %s\n", argv[0]); |
---|
[230] | 614 | } |
---|
[596] | 615 | else |
---|
[407] | 616 | { |
---|
[619] | 617 | printf("available commands:\n"); |
---|
| 618 | for (i = 0 ; command[i].name ; i++) |
---|
[596] | 619 | { |
---|
[619] | 620 | printf("\t%s\t : %s\n", command[i].name , command[i].desc); |
---|
[596] | 621 | } |
---|
| 622 | } |
---|
[457] | 623 | |
---|
| 624 | // release semaphore to get next command |
---|
| 625 | sem_post( &semaphore ); |
---|
| 626 | |
---|
[407] | 627 | } // end cmd_help() |
---|
[230] | 628 | |
---|
[407] | 629 | ////////////////////////////////////////////// |
---|
| 630 | static void cmd_kill( int argc , char **argv ) |
---|
[230] | 631 | { |
---|
[407] | 632 | unsigned int pid; |
---|
[230] | 633 | |
---|
[407] | 634 | if (argc != 2) |
---|
| 635 | { |
---|
[409] | 636 | printf(" usage: %s pid\n", argv[0]); |
---|
[230] | 637 | } |
---|
[596] | 638 | else |
---|
| 639 | { |
---|
| 640 | pid = atoi( argv[1] ); |
---|
[230] | 641 | |
---|
[596] | 642 | if( pid == 0 ) |
---|
| 643 | { |
---|
| 644 | printf(" error: kernel process 0 cannot be killed\n" ); |
---|
| 645 | } |
---|
[230] | 646 | |
---|
[596] | 647 | else if( kill( pid , SIGKILL ) ) |
---|
| 648 | { |
---|
| 649 | printf(" error: process %x cannot be killed\n", pid ); |
---|
| 650 | } |
---|
| 651 | } |
---|
[427] | 652 | |
---|
[457] | 653 | // release semaphore to get next command |
---|
| 654 | sem_post( &semaphore ); |
---|
| 655 | |
---|
[407] | 656 | } // end cmd_kill() |
---|
[230] | 657 | |
---|
[407] | 658 | ////////////////////////////////////////////// |
---|
| 659 | static void cmd_load( int argc , char **argv ) |
---|
[230] | 660 | { |
---|
[436] | 661 | int ret_fork; // return value from fork |
---|
| 662 | int ret_exec; // return value from exec |
---|
| 663 | unsigned int ksh_pid; // KSH process PID |
---|
| 664 | char * pathname; // path to .elf file |
---|
| 665 | unsigned int background; // background execution if non zero |
---|
[588] | 666 | unsigned int placement; // placement specified if non zero |
---|
| 667 | unsigned int cxy; // target cluster if placement specified |
---|
[407] | 668 | |
---|
[619] | 669 | #if DEBUG_CMD_LOAD |
---|
| 670 | char string[64]; |
---|
| 671 | #endif |
---|
| 672 | |
---|
[588] | 673 | if( (argc < 2) || (argc > 4) ) |
---|
[407] | 674 | { |
---|
[588] | 675 | printf(" usage: %s pathname [cxy] [&]\n", argv[0] ); |
---|
[407] | 676 | } |
---|
[596] | 677 | else |
---|
| 678 | { |
---|
| 679 | pathname = argv[1]; |
---|
[407] | 680 | |
---|
[596] | 681 | if( argc == 2 ) |
---|
[588] | 682 | { |
---|
[596] | 683 | background = 0; |
---|
[588] | 684 | placement = 0; |
---|
| 685 | cxy = 0; |
---|
| 686 | } |
---|
[596] | 687 | else if( argc == 3 ) |
---|
[588] | 688 | { |
---|
[596] | 689 | if( (argv[2][0] == '&') && (argv[2][1] == 0) ) |
---|
| 690 | { |
---|
| 691 | background = 1; |
---|
| 692 | placement = 0; |
---|
| 693 | cxy = 0; |
---|
| 694 | } |
---|
| 695 | else |
---|
| 696 | { |
---|
| 697 | background = 0; |
---|
| 698 | placement = 1; |
---|
| 699 | cxy = atoi( argv[2] ); |
---|
| 700 | } |
---|
| 701 | } |
---|
| 702 | else // argc == 4 |
---|
| 703 | { |
---|
| 704 | background = ( (argv[3][0] == '&') && (argv[3][1] == 0) ); |
---|
[588] | 705 | placement = 1; |
---|
| 706 | cxy = atoi( argv[2] ); |
---|
| 707 | } |
---|
[427] | 708 | |
---|
[619] | 709 | /* |
---|
| 710 | // take semaphore to block the interactive thread |
---|
| 711 | if ( sem_wait( &semaphore ) ) |
---|
| 712 | { |
---|
| 713 | printf("\n[ksh error] cannot found semafore\n" ); |
---|
| 714 | exit( 1 ); |
---|
| 715 | } |
---|
| 716 | */ |
---|
[596] | 717 | // get KSH process PID |
---|
| 718 | ksh_pid = getpid(); |
---|
[407] | 719 | |
---|
[611] | 720 | #if DEBUG_CMD_LOAD |
---|
[623] | 721 | snprintf( string , 64 , "[ksh] %s : ksh_pid %x / path %s / bg %d / place %d (%x)\n", |
---|
[619] | 722 | __FUNCTION__, ksh_pid, argv[1], background, placement, cxy ); |
---|
| 723 | display_string( string ); |
---|
[457] | 724 | #endif |
---|
| 725 | |
---|
[596] | 726 | // set target cluster if required |
---|
| 727 | if( placement ) place_fork( cxy ); |
---|
[588] | 728 | |
---|
[596] | 729 | // KSH process fork CHILD process |
---|
| 730 | ret_fork = fork(); |
---|
[434] | 731 | |
---|
[596] | 732 | if ( ret_fork < 0 ) // it is a failure reported to KSH |
---|
| 733 | { |
---|
| 734 | printf(" error: ksh process unable to fork\n"); |
---|
| 735 | } |
---|
| 736 | else if (ret_fork == 0) // it is the CHILD process |
---|
| 737 | { |
---|
[469] | 738 | |
---|
[611] | 739 | #if DEBUG_CMD_LOAD |
---|
[623] | 740 | snprintf( string , 64 , "[ksh] %s : child_pid %x after fork, before exec\n", |
---|
[619] | 741 | __FUNCTION__ , getpid() ); |
---|
| 742 | display_string( string ); |
---|
[469] | 743 | #endif |
---|
| 744 | |
---|
[596] | 745 | // CHILD process exec NEW process |
---|
| 746 | ret_exec = execve( pathname , NULL , NULL ); |
---|
[434] | 747 | |
---|
[611] | 748 | #if DEBUG_CMD_LOAD |
---|
[623] | 749 | snprintf( string , 64 , "[ksh] %s : child_pid %x after exec / ret_exec %x\n", |
---|
[619] | 750 | __FUNCTION__ , getpid(), ret_exec ); |
---|
| 751 | display_string( string ); |
---|
[469] | 752 | #endif |
---|
| 753 | |
---|
[596] | 754 | // this is only executed in case of exec failure |
---|
| 755 | if( ret_exec ) |
---|
| 756 | { |
---|
| 757 | printf(" error: child process unable to exec <%s>\n", pathname ); |
---|
| 758 | exit( 0 ); |
---|
| 759 | } |
---|
| 760 | } |
---|
| 761 | else // it is the KSH process : ret_fork is the new process PID |
---|
[409] | 762 | { |
---|
[436] | 763 | |
---|
[611] | 764 | #if DEBUG_CMD_LOAD |
---|
[623] | 765 | snprintf( string , 64 , "[ksh] %s : ksh_pid %x after fork / ret_fork %x\n", |
---|
[619] | 766 | __FUNCTION__, getpid(), ret_fork ); |
---|
| 767 | display_string( string ); |
---|
[457] | 768 | #endif |
---|
| 769 | |
---|
[619] | 770 | if( background ) // KSH must keep TXT ownership |
---|
[596] | 771 | { |
---|
[619] | 772 | // get back the TXT ownership |
---|
[596] | 773 | fg( ksh_pid ); |
---|
[619] | 774 | |
---|
| 775 | // release semaphore to get next command |
---|
| 776 | sem_post( &semaphore ); |
---|
[596] | 777 | } |
---|
[619] | 778 | else // KSH loosed TXT ownership |
---|
| 779 | { |
---|
| 780 | // semaphore will be released by the KSH main thread |
---|
| 781 | // when the loaded process exit |
---|
| 782 | } |
---|
[457] | 783 | } |
---|
[436] | 784 | } |
---|
[407] | 785 | } // end cmd_load |
---|
| 786 | |
---|
| 787 | ///////////////////////////////////////////// |
---|
| 788 | static void cmd_log( int argc , char **argv ) |
---|
| 789 | { |
---|
| 790 | unsigned int i; |
---|
| 791 | |
---|
[473] | 792 | if (argc != 1) |
---|
| 793 | { |
---|
| 794 | printf(" usage: %s\n", argv[0], argc ); |
---|
| 795 | } |
---|
[596] | 796 | else |
---|
[407] | 797 | { |
---|
[596] | 798 | printf("--- registered commands ---\n"); |
---|
| 799 | for (i = 0; i < LOG_DEPTH; i++) |
---|
| 800 | { |
---|
| 801 | printf(" - %d\t: %s\n", i, &log_entries[i].buf); |
---|
| 802 | } |
---|
| 803 | } |
---|
[230] | 804 | |
---|
[457] | 805 | // release semaphore to get next command |
---|
| 806 | sem_post( &semaphore ); |
---|
| 807 | |
---|
| 808 | } // end cmd_log() |
---|
| 809 | |
---|
| 810 | |
---|
[407] | 811 | //////////////////////////////////////////// |
---|
| 812 | static void cmd_ls( int argc , char **argv ) |
---|
[230] | 813 | { |
---|
[616] | 814 | char * pathname = NULL; |
---|
[611] | 815 | struct dirent * entry; |
---|
| 816 | DIR * dir; |
---|
[230] | 817 | |
---|
[619] | 818 | #if DEBUG_CMD_LS |
---|
| 819 | char string[64]; |
---|
| 820 | #endif |
---|
| 821 | |
---|
[612] | 822 | if (argc > 2 ) |
---|
[407] | 823 | { |
---|
[596] | 824 | printf(" usage: ls [path]\n"); |
---|
[407] | 825 | } |
---|
[596] | 826 | else |
---|
[407] | 827 | { |
---|
[612] | 828 | // handle case with no argument |
---|
[230] | 829 | |
---|
[611] | 830 | // get target directory path |
---|
[612] | 831 | if ( argc == 1 ) strcpy( pathname , "." ); |
---|
| 832 | else pathname = argv[1]; |
---|
[611] | 833 | |
---|
| 834 | // open target directory |
---|
| 835 | dir = opendir( pathname ); |
---|
| 836 | |
---|
| 837 | #if DEBUG_CMD_LS |
---|
[623] | 838 | snprintf( string , 64 , "[ksh] %s : directory <%s> open / DIR %x\n", |
---|
[611] | 839 | __FUNCTION__, pathname , dir ); |
---|
[619] | 840 | display_string( string ); |
---|
[611] | 841 | #endif |
---|
| 842 | |
---|
| 843 | if( dir == NULL) |
---|
| 844 | { |
---|
| 845 | printf(" error : directory <%s> not found\n", pathname ); |
---|
[623] | 846 | |
---|
| 847 | sem_post( &semaphore ); |
---|
| 848 | return; |
---|
[611] | 849 | } |
---|
| 850 | |
---|
| 851 | // loop on directory entries |
---|
| 852 | while ( (entry = readdir(dir)) != NULL ) |
---|
| 853 | { |
---|
[612] | 854 | printf("%s\n", entry->d_name); |
---|
[611] | 855 | } |
---|
| 856 | |
---|
| 857 | // close target directory |
---|
| 858 | closedir( dir ); |
---|
| 859 | |
---|
| 860 | #if DEBUG_CMD_LS |
---|
[623] | 861 | snprintf( string , 64 , "[ksh] %s : directory <%s> closed\n", |
---|
[611] | 862 | __FUNCTION__, pathname ); |
---|
[619] | 863 | display_string( string ); |
---|
[611] | 864 | #endif |
---|
| 865 | |
---|
[596] | 866 | } |
---|
[230] | 867 | |
---|
[457] | 868 | // release semaphore to get next command |
---|
| 869 | sem_post( &semaphore ); |
---|
| 870 | |
---|
| 871 | } // end cmd_ls() |
---|
| 872 | |
---|
[407] | 873 | /////////////////////////////////////////////// |
---|
| 874 | static void cmd_mkdir( int argc , char **argv ) |
---|
[230] | 875 | { |
---|
[407] | 876 | char * pathname; |
---|
[230] | 877 | |
---|
[407] | 878 | if (argc != 2) |
---|
| 879 | { |
---|
[409] | 880 | printf(" usage: mkdir pathname\n"); |
---|
[230] | 881 | } |
---|
[596] | 882 | else |
---|
| 883 | { |
---|
| 884 | pathname = argv[1]; |
---|
[230] | 885 | |
---|
[610] | 886 | mkdir( pathname , 0x777 ); |
---|
[596] | 887 | } |
---|
[230] | 888 | |
---|
[457] | 889 | // release semaphore to get next command |
---|
| 890 | sem_post( &semaphore ); |
---|
| 891 | |
---|
| 892 | } // end cmd_mkdir() |
---|
| 893 | |
---|
[407] | 894 | //////////////////////////////////////////// |
---|
| 895 | static void cmd_mv( int argc , char **argv ) |
---|
[230] | 896 | { |
---|
[610] | 897 | char * old_path; |
---|
| 898 | char * new_path; |
---|
[407] | 899 | |
---|
[610] | 900 | if (argc != 3) |
---|
| 901 | { |
---|
| 902 | printf(" usage: mv old_pathname new_pathname\n"); |
---|
[230] | 903 | } |
---|
[596] | 904 | else |
---|
| 905 | { |
---|
[610] | 906 | old_path = argv[1]; |
---|
| 907 | new_path = argv[2]; |
---|
| 908 | |
---|
| 909 | // call the relevant syscall |
---|
| 910 | if( rename( old_path , new_path ) ) |
---|
| 911 | { |
---|
| 912 | printf(" error: unable to rename <%s> to <%s>\n", old_path, new_path ); |
---|
| 913 | } |
---|
[596] | 914 | } |
---|
[610] | 915 | |
---|
[457] | 916 | // release semaphore to get next command |
---|
| 917 | sem_post( &semaphore ); |
---|
[407] | 918 | |
---|
[457] | 919 | } // end cmd_mv |
---|
[230] | 920 | |
---|
[588] | 921 | |
---|
| 922 | //////////////////////////////////////////// |
---|
| 923 | static void cmd_ps( int argc , char **argv ) |
---|
| 924 | { |
---|
| 925 | unsigned int x_size; |
---|
| 926 | unsigned int y_size; |
---|
| 927 | unsigned int ncores; |
---|
| 928 | unsigned int x; |
---|
| 929 | unsigned int y; |
---|
| 930 | |
---|
[619] | 931 | #if DEBUG_CMD_PS |
---|
| 932 | char string[64]; |
---|
| 933 | #endif |
---|
| 934 | |
---|
[588] | 935 | if (argc != 1) |
---|
| 936 | { |
---|
| 937 | printf(" usage: %s\n", argv[0]); |
---|
| 938 | } |
---|
[596] | 939 | else |
---|
| 940 | { |
---|
| 941 | // get platform config |
---|
| 942 | get_config( &x_size , &y_size , &ncores ); |
---|
[588] | 943 | |
---|
[596] | 944 | // scan all clusters |
---|
| 945 | for( x = 0 ; x < x_size ; x++ ) |
---|
[588] | 946 | { |
---|
[596] | 947 | for( y = 0 ; y < y_size ; y++ ) |
---|
| 948 | { |
---|
[619] | 949 | |
---|
| 950 | #if DEBUG_CMD_PS |
---|
[623] | 951 | snprintf( string , 64 , "\n[ksh] %s : call display_cluster_process()", __FUNCTION__ ); |
---|
[619] | 952 | display_string( string ); |
---|
| 953 | #endif |
---|
| 954 | |
---|
[596] | 955 | // display only owned processes |
---|
| 956 | display_cluster_processes( HAL_CXY_FROM_XY(x,y), 1 ); |
---|
| 957 | } |
---|
[588] | 958 | } |
---|
| 959 | } |
---|
| 960 | |
---|
| 961 | // release semaphore to get next command |
---|
| 962 | sem_post( &semaphore ); |
---|
| 963 | |
---|
| 964 | } // end cmd_ps() |
---|
| 965 | |
---|
[407] | 966 | ///////////////////////////////////////////// |
---|
| 967 | static void cmd_pwd( int argc , char **argv ) |
---|
[230] | 968 | { |
---|
[407] | 969 | char buf[1024]; |
---|
[230] | 970 | |
---|
[407] | 971 | if (argc != 1) |
---|
| 972 | { |
---|
[473] | 973 | printf(" usage: %s\n", argv[0]); |
---|
[407] | 974 | } |
---|
| 975 | else |
---|
| 976 | { |
---|
[596] | 977 | if ( getcwd( buf , 1024 ) ) |
---|
| 978 | { |
---|
| 979 | printf(" error: unable to get current directory\n"); |
---|
| 980 | } |
---|
| 981 | else |
---|
| 982 | { |
---|
| 983 | printf("%s\n", buf); |
---|
| 984 | } |
---|
| 985 | } |
---|
[407] | 986 | |
---|
[457] | 987 | // release semaphore to get next command |
---|
| 988 | sem_post( &semaphore ); |
---|
| 989 | |
---|
| 990 | } // end cmd_pwd() |
---|
| 991 | |
---|
[407] | 992 | //////////////////////////////////////////// |
---|
| 993 | static void cmd_rm( int argc , char **argv ) |
---|
| 994 | { |
---|
| 995 | char * pathname; |
---|
| 996 | |
---|
| 997 | if (argc != 2) |
---|
| 998 | { |
---|
[473] | 999 | printf(" usage: %s pathname\n", argv[0]); |
---|
[230] | 1000 | } |
---|
[596] | 1001 | else |
---|
| 1002 | { |
---|
| 1003 | pathname = argv[1]; |
---|
[230] | 1004 | |
---|
[608] | 1005 | if ( unlink( pathname ) ) |
---|
| 1006 | { |
---|
[611] | 1007 | printf(" error: unable to remove <%s>\n", pathname ); |
---|
[608] | 1008 | } |
---|
[596] | 1009 | } |
---|
[230] | 1010 | |
---|
[457] | 1011 | // release semaphore to get next command |
---|
| 1012 | sem_post( &semaphore ); |
---|
[230] | 1013 | |
---|
[457] | 1014 | } // end_cmd_rm() |
---|
| 1015 | |
---|
[407] | 1016 | /////////////////////////////////////////////// |
---|
| 1017 | static void cmd_rmdir( int argc , char **argv ) |
---|
[230] | 1018 | { |
---|
[457] | 1019 | // same as cmd_rm() |
---|
[230] | 1020 | cmd_rm(argc, argv); |
---|
| 1021 | } |
---|
| 1022 | |
---|
[442] | 1023 | /////////////////////////////////////////////// |
---|
| 1024 | static void cmd_trace( int argc , char **argv ) |
---|
| 1025 | { |
---|
| 1026 | unsigned int cxy; |
---|
| 1027 | unsigned int lid; |
---|
| 1028 | |
---|
| 1029 | if (argc != 3) |
---|
| 1030 | { |
---|
| 1031 | printf(" usage: trace cxy lid \n"); |
---|
| 1032 | } |
---|
[596] | 1033 | else |
---|
| 1034 | { |
---|
| 1035 | cxy = atoi(argv[1]); |
---|
| 1036 | lid = atoi(argv[2]); |
---|
[442] | 1037 | |
---|
[596] | 1038 | if( trace( 1 , cxy , lid ) ) |
---|
| 1039 | { |
---|
| 1040 | printf(" error: core[%x,%d] not found\n", cxy, lid ); |
---|
| 1041 | } |
---|
[442] | 1042 | } |
---|
| 1043 | |
---|
[457] | 1044 | // release semaphore to get next command |
---|
| 1045 | sem_post( &semaphore ); |
---|
| 1046 | |
---|
| 1047 | } // end cmd_trace |
---|
| 1048 | |
---|
[442] | 1049 | /////////////////////////////////////////////// |
---|
| 1050 | static void cmd_untrace( int argc , char **argv ) |
---|
| 1051 | { |
---|
| 1052 | unsigned int cxy; |
---|
| 1053 | unsigned int lid; |
---|
| 1054 | |
---|
| 1055 | if (argc != 3) |
---|
| 1056 | { |
---|
| 1057 | printf(" usage: untrace cxy lid \n"); |
---|
| 1058 | } |
---|
[596] | 1059 | else |
---|
| 1060 | { |
---|
| 1061 | cxy = atoi(argv[1]); |
---|
| 1062 | lid = atoi(argv[2]); |
---|
[442] | 1063 | |
---|
[596] | 1064 | if( trace( 0 , cxy , lid ) ) |
---|
| 1065 | { |
---|
| 1066 | printf(" error: core[%x,%d] not found\n", cxy, lid ); |
---|
| 1067 | } |
---|
[442] | 1068 | } |
---|
| 1069 | |
---|
[457] | 1070 | // release semaphore to get next command |
---|
| 1071 | sem_post( &semaphore ); |
---|
| 1072 | |
---|
| 1073 | } // end cmd_untrace() |
---|
| 1074 | |
---|
| 1075 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 1076 | // Array of commands |
---|
[457] | 1077 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[230] | 1078 | |
---|
[619] | 1079 | ksh_cmd_t command[] = |
---|
[230] | 1080 | { |
---|
[435] | 1081 | { "cat", "display file content", cmd_cat }, |
---|
| 1082 | { "cd", "change current directory", cmd_cd }, |
---|
| 1083 | { "cp", "replicate a file in file system", cmd_cp }, |
---|
| 1084 | { "fg", "put a process in foreground", cmd_fg }, |
---|
| 1085 | { "display", "display vmm/sched/process/vfs/chdev/txt", cmd_display }, |
---|
| 1086 | { "load", "load an user application", cmd_load }, |
---|
| 1087 | { "help", "list available commands", cmd_help }, |
---|
[457] | 1088 | { "kill", "kill a process (all threads)", cmd_kill }, |
---|
[435] | 1089 | { "log", "list registered commands", cmd_log }, |
---|
| 1090 | { "ls", "list directory entries", cmd_ls }, |
---|
| 1091 | { "mkdir", "create a new directory", cmd_mkdir }, |
---|
| 1092 | { "mv", "move a file in file system", cmd_mv }, |
---|
| 1093 | { "pwd", "print current working directory", cmd_pwd }, |
---|
[588] | 1094 | { "ps", "display all processes", cmd_ps }, |
---|
[435] | 1095 | { "rm", "remove a file from file system", cmd_rm }, |
---|
| 1096 | { "rmdir", "remove a directory from file system", cmd_rmdir }, |
---|
[442] | 1097 | { "trace", "activate trace for a given core", cmd_trace }, |
---|
| 1098 | { "untrace", "desactivate trace for a given core", cmd_untrace }, |
---|
[435] | 1099 | { NULL, NULL, NULL } |
---|
[230] | 1100 | }; |
---|
| 1101 | |
---|
[407] | 1102 | //////////////////////////////////////////////////////////////////////////////////// |
---|
[457] | 1103 | // This function analyses one command (with arguments), executes it, and returns. |
---|
[407] | 1104 | //////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 1105 | static void __attribute__ ((noinline)) parse( char * buf ) |
---|
[230] | 1106 | { |
---|
[619] | 1107 | int argc = 0; |
---|
| 1108 | char * argv[MAX_ARGS]; |
---|
| 1109 | int i; |
---|
| 1110 | int len = strlen(buf); |
---|
[230] | 1111 | |
---|
[619] | 1112 | #if DEBUG_PARSE |
---|
| 1113 | char string[64]; |
---|
[623] | 1114 | snprintf( string , 64 , "\n[ksh] %s : <%s>", __FUNCTION__ , buf ); |
---|
[619] | 1115 | display_string( string ); |
---|
| 1116 | #endif |
---|
| 1117 | |
---|
[230] | 1118 | // build argc/argv |
---|
[407] | 1119 | for (i = 0; i < len; i++) |
---|
| 1120 | { |
---|
[619] | 1121 | // convert SPACE to NUL |
---|
[407] | 1122 | if (buf[i] == ' ') |
---|
| 1123 | { |
---|
[230] | 1124 | buf[i] = '\0'; |
---|
[407] | 1125 | } |
---|
| 1126 | else if (i == 0 || buf[i - 1] == '\0') |
---|
| 1127 | { |
---|
| 1128 | if (argc < MAX_ARGS) |
---|
| 1129 | { |
---|
[230] | 1130 | argv[argc] = &buf[i]; |
---|
| 1131 | argc++; |
---|
| 1132 | } |
---|
| 1133 | } |
---|
| 1134 | } |
---|
| 1135 | |
---|
[619] | 1136 | #if DEBUG_PARSE |
---|
[623] | 1137 | snprintf( string , 64 , "\n[ksh] %s : argc = %d for <%s>", __FUNCTION__ , argc , argv[0] ); |
---|
[619] | 1138 | display_string( string ); |
---|
| 1139 | #endif |
---|
| 1140 | |
---|
[469] | 1141 | // analyse command type |
---|
[407] | 1142 | if (argc > 0) |
---|
| 1143 | { |
---|
[230] | 1144 | int found = 0; |
---|
| 1145 | |
---|
[407] | 1146 | // try to match typed command |
---|
[619] | 1147 | for ( i = 0 ; command[i].name ; i++ ) |
---|
[407] | 1148 | { |
---|
[619] | 1149 | if (strcmp(argv[0], command[i].name) == 0) |
---|
[407] | 1150 | { |
---|
[619] | 1151 | command[i].fn(argc, argv); |
---|
[230] | 1152 | found = 1; |
---|
| 1153 | break; |
---|
| 1154 | } |
---|
| 1155 | } |
---|
| 1156 | |
---|
[457] | 1157 | if (!found) // undefined command |
---|
[407] | 1158 | { |
---|
[457] | 1159 | printf(" error : undefined command <%s>\n", argv[0]); |
---|
| 1160 | |
---|
| 1161 | // release semaphore to get next command |
---|
| 1162 | sem_post( &semaphore ); |
---|
[230] | 1163 | } |
---|
| 1164 | } |
---|
[446] | 1165 | } // end parse() |
---|
[230] | 1166 | |
---|
[574] | 1167 | /////////////////////////////// |
---|
[503] | 1168 | static void interactive( void ) |
---|
[230] | 1169 | { |
---|
[469] | 1170 | char c; // read character |
---|
| 1171 | unsigned int end_command; // last character found in a command |
---|
| 1172 | unsigned int count; // pointer in command buffer |
---|
| 1173 | unsigned int i; // index for loops |
---|
| 1174 | unsigned int state; // escape sequence state |
---|
[230] | 1175 | |
---|
[619] | 1176 | char cmd[CMD_MAX_SIZE]; // buffer for one command |
---|
[457] | 1177 | |
---|
[619] | 1178 | #if DEBUG_INTER |
---|
| 1179 | char string[64]; |
---|
| 1180 | #endif |
---|
| 1181 | |
---|
[611] | 1182 | /* To lauch one command without interactive mode |
---|
[588] | 1183 | if( sem_wait( &semaphore ) ) |
---|
[469] | 1184 | { |
---|
[588] | 1185 | printf("\n[ksh error] cannot found semafore\n" ); |
---|
| 1186 | exit( 1 ); |
---|
[469] | 1187 | } |
---|
[588] | 1188 | else |
---|
| 1189 | { |
---|
[619] | 1190 | printf("\n[ksh] load bin/user/sort.elf\n"); |
---|
[588] | 1191 | } |
---|
[469] | 1192 | |
---|
[619] | 1193 | strcpy( cmd , "load bin/user/sort.elf" ); |
---|
| 1194 | parse( cmd ); |
---|
[611] | 1195 | */ |
---|
[469] | 1196 | |
---|
[407] | 1197 | enum fsm_states |
---|
| 1198 | { |
---|
[457] | 1199 | NORMAL = 0, |
---|
| 1200 | ESCAPE = 1, |
---|
| 1201 | BRAKET = 2, |
---|
[230] | 1202 | }; |
---|
| 1203 | |
---|
[457] | 1204 | // This lexical analyser writes one command line in the command buffer. |
---|
| 1205 | // It is implemented as a 3 states FSM to handle the following escape sequences: |
---|
[230] | 1206 | // - ESC [ A : up arrow |
---|
| 1207 | // - ESC [ B : down arrow |
---|
| 1208 | // - ESC [ C : right arrow |
---|
| 1209 | // - ESC [ D : left arrow |
---|
[407] | 1210 | // The three states have the following semantic: |
---|
[230] | 1211 | // - NORMAL : no (ESC) character has been found |
---|
| 1212 | // - ESCAPE : the character (ESC) has been found |
---|
| 1213 | // - BRAKET : the wo characters (ESC,[) have been found |
---|
[436] | 1214 | |
---|
[619] | 1215 | // take the semaphore for the first command |
---|
| 1216 | if ( sem_wait( &semaphore ) ) |
---|
| 1217 | { |
---|
| 1218 | printf("\n[ksh error] cannot found semafore\n" ); |
---|
| 1219 | exit( 1 ); |
---|
| 1220 | } |
---|
| 1221 | |
---|
| 1222 | // display prompt for the first command |
---|
| 1223 | printf("\n[ksh] "); |
---|
| 1224 | |
---|
| 1225 | // external loop on the commands / the interactive thread do not exit this loop |
---|
[230] | 1226 | while (1) |
---|
| 1227 | { |
---|
[457] | 1228 | // initialize command buffer |
---|
[619] | 1229 | memset( cmd, 0x20 , sizeof(cmd) ); // TODO useful ? |
---|
| 1230 | count = 0; |
---|
| 1231 | state = NORMAL; |
---|
| 1232 | end_command = 0; |
---|
[230] | 1233 | |
---|
[619] | 1234 | #if DEBUG_INTER |
---|
| 1235 | unsigned int pid = getpid(); |
---|
[623] | 1236 | snprintf( string , 64 , "\n[ksh] %s : request a new command", __FUNCTION__ ); |
---|
[619] | 1237 | display_string( string ); |
---|
| 1238 | #endif |
---|
[407] | 1239 | |
---|
[457] | 1240 | // internal loop on characters in one command |
---|
| 1241 | while( end_command == 0 ) |
---|
| 1242 | { |
---|
| 1243 | // get one character from TXT_RX |
---|
| 1244 | c = (char)getchar(); |
---|
| 1245 | |
---|
| 1246 | if( c == 0 ) continue; |
---|
| 1247 | |
---|
| 1248 | if( state == NORMAL ) // we are not in an escape sequence |
---|
| 1249 | { |
---|
[230] | 1250 | if ((c == '\b') || (c == 0x7F)) // backspace => remove one character |
---|
| 1251 | { |
---|
[457] | 1252 | if (count > 0) |
---|
| 1253 | { |
---|
| 1254 | printf("\b \b"); |
---|
| 1255 | count--; |
---|
| 1256 | } |
---|
[230] | 1257 | } |
---|
[457] | 1258 | else if (c == '\n') // new line => end of command |
---|
[230] | 1259 | { |
---|
[457] | 1260 | if (count > 0) // analyse & execute command |
---|
| 1261 | { |
---|
| 1262 | // complete command with NUL character |
---|
[619] | 1263 | cmd[count] = 0; |
---|
[457] | 1264 | count++; |
---|
[230] | 1265 | |
---|
[619] | 1266 | // register command in log |
---|
| 1267 | strncpy( log_entries[ptw].buf , cmd , count ); |
---|
[457] | 1268 | log_entries[ptw].count = count; |
---|
| 1269 | ptw = (ptw + 1) % LOG_DEPTH; |
---|
| 1270 | ptr = ptw; |
---|
[230] | 1271 | |
---|
[619] | 1272 | #if DEBUG_INTER |
---|
[623] | 1273 | snprintf( string , 64 , "[ksh] %s : parse and execute <%s>", __FUNCTION__, cmd ); |
---|
[619] | 1274 | display_string( string ); |
---|
| 1275 | #endif |
---|
[457] | 1276 | // echo character |
---|
| 1277 | putchar( c ); |
---|
[230] | 1278 | |
---|
[457] | 1279 | // call parser to analyse and execute command |
---|
[619] | 1280 | parse( cmd ); |
---|
[457] | 1281 | } |
---|
| 1282 | else // no command registered |
---|
[441] | 1283 | { |
---|
[457] | 1284 | // release semaphore to get next command |
---|
| 1285 | sem_post( &semaphore ); |
---|
[441] | 1286 | } |
---|
[457] | 1287 | |
---|
| 1288 | // exit internal loop on characters |
---|
| 1289 | end_command = 1; |
---|
| 1290 | } |
---|
| 1291 | else if (c == '\t') // tabulation => do nothing |
---|
| 1292 | { |
---|
| 1293 | } |
---|
| 1294 | else if (c == (char)0x1B) // ESC => start an escape sequence |
---|
| 1295 | { |
---|
| 1296 | state = ESCAPE; |
---|
| 1297 | } |
---|
| 1298 | else // normal character |
---|
[230] | 1299 | { |
---|
[619] | 1300 | if (count < (sizeof(cmd) - 1) ) |
---|
[457] | 1301 | { |
---|
| 1302 | // register character in command buffer |
---|
[619] | 1303 | cmd[count] = c; |
---|
[457] | 1304 | count++; |
---|
| 1305 | |
---|
| 1306 | // echo character |
---|
| 1307 | putchar( c ); |
---|
[230] | 1308 | } |
---|
[619] | 1309 | else |
---|
| 1310 | { |
---|
| 1311 | printf("\none command cannot exceed %d characters\n", sizeof(cmd) ); |
---|
| 1312 | } |
---|
[230] | 1313 | } |
---|
| 1314 | } |
---|
[457] | 1315 | else if( state == ESCAPE ) |
---|
[230] | 1316 | { |
---|
| 1317 | if (c == '[') // valid sequence => continue |
---|
| 1318 | { |
---|
| 1319 | state = BRAKET; |
---|
| 1320 | } |
---|
| 1321 | else // invalid sequence => do nothing |
---|
| 1322 | { |
---|
| 1323 | state = NORMAL; |
---|
| 1324 | } |
---|
| 1325 | } |
---|
[457] | 1326 | else if( state == BRAKET ) |
---|
[230] | 1327 | { |
---|
[619] | 1328 | if (c == 'D') // valid LEFT sequence => move cmd pointer left |
---|
[230] | 1329 | { |
---|
| 1330 | if (count > 0) |
---|
| 1331 | { |
---|
| 1332 | printf("\b"); |
---|
| 1333 | count--; |
---|
| 1334 | } |
---|
| 1335 | |
---|
| 1336 | // get next user char |
---|
| 1337 | state = NORMAL; |
---|
| 1338 | } |
---|
[619] | 1339 | else if (c == 'C') // valid RIGHT sequence => move cmd pointer right |
---|
[230] | 1340 | { |
---|
[619] | 1341 | if (count < sizeof(cmd) - 1) |
---|
[230] | 1342 | { |
---|
[619] | 1343 | printf("%c", cmd[count]); |
---|
[230] | 1344 | count++; |
---|
| 1345 | } |
---|
| 1346 | |
---|
| 1347 | // get next user char |
---|
| 1348 | state = NORMAL; |
---|
| 1349 | } |
---|
| 1350 | else if (c == 'A') // valid UP sequence => move log pointer backward |
---|
| 1351 | { |
---|
| 1352 | // cancel current command |
---|
| 1353 | for (i = 0; i < count; i++) printf("\b \b"); |
---|
| 1354 | count = 0; |
---|
| 1355 | |
---|
[619] | 1356 | // copy log command into cmd |
---|
[230] | 1357 | ptr = (ptr - 1) % LOG_DEPTH; |
---|
[619] | 1358 | strcpy(cmd, log_entries[ptr].buf); |
---|
[458] | 1359 | count = log_entries[ptr].count - 1; |
---|
[230] | 1360 | |
---|
| 1361 | // display log command |
---|
[619] | 1362 | printf("%s", cmd); |
---|
[230] | 1363 | |
---|
| 1364 | // get next user char |
---|
| 1365 | state = NORMAL; |
---|
| 1366 | } |
---|
| 1367 | else if (c == 'B') // valid DOWN sequence => move log pointer forward |
---|
| 1368 | { |
---|
| 1369 | // cancel current command |
---|
| 1370 | for (i = 0 ; i < count; i++) printf("\b \b"); |
---|
| 1371 | count = 0; |
---|
| 1372 | |
---|
[619] | 1373 | // copy log command into cmd |
---|
[230] | 1374 | ptr = (ptr + 1) % LOG_DEPTH; |
---|
[619] | 1375 | strcpy(cmd, log_entries[ptr].buf); |
---|
[230] | 1376 | count = log_entries[ptr].count; |
---|
| 1377 | |
---|
| 1378 | // display log command |
---|
[619] | 1379 | printf("%s", cmd); |
---|
[230] | 1380 | |
---|
| 1381 | // get next user char |
---|
| 1382 | state = NORMAL; |
---|
| 1383 | } |
---|
| 1384 | else // other character => do nothing |
---|
| 1385 | { |
---|
| 1386 | // get next user char |
---|
| 1387 | state = NORMAL; |
---|
| 1388 | } |
---|
| 1389 | } |
---|
[457] | 1390 | } // end internal while loop on characters |
---|
[619] | 1391 | |
---|
| 1392 | #if DEBUG_INTER |
---|
[623] | 1393 | snprintf( string , 64 , "\n[ksh] %s : complete <%s> command", __FUNCTION__, cmd ); |
---|
[619] | 1394 | display_string( string ); |
---|
| 1395 | #endif |
---|
| 1396 | |
---|
| 1397 | // block interactive thread if KSH loose TXT ownership |
---|
| 1398 | if ( sem_wait( &semaphore ) ) |
---|
| 1399 | { |
---|
| 1400 | printf("\n[ksh error] cannot found semafore\n" ); |
---|
| 1401 | exit( 1 ); |
---|
| 1402 | } |
---|
| 1403 | |
---|
| 1404 | // display prompt for next command |
---|
| 1405 | printf("\n[ksh] "); |
---|
| 1406 | |
---|
[457] | 1407 | } // end external while loop on commands |
---|
[619] | 1408 | |
---|
[446] | 1409 | } // end interactive() |
---|
| 1410 | |
---|
[574] | 1411 | //////////////// |
---|
[503] | 1412 | int main( void ) |
---|
[446] | 1413 | { |
---|
| 1414 | unsigned int cxy; // owner cluster identifier for this KSH process |
---|
| 1415 | unsigned int lid; // core identifier for this KSH main thread |
---|
| 1416 | int status; // child process termination status |
---|
[457] | 1417 | int child_pid; // child process identifier |
---|
[469] | 1418 | int parent_pid; // parent process identifier (i.e. this process) |
---|
[457] | 1419 | unsigned int is_owner; // non-zero if KSH process is TXT owner |
---|
[446] | 1420 | |
---|
| 1421 | // initialize log buffer |
---|
| 1422 | memset( &log_entries , 0, sizeof(log_entries)); |
---|
| 1423 | ptw = 0; |
---|
| 1424 | ptr = 0; |
---|
| 1425 | |
---|
[457] | 1426 | // get KSH process pid and core |
---|
| 1427 | parent_pid = getpid(); |
---|
[459] | 1428 | get_core( &cxy , &lid ); |
---|
[457] | 1429 | |
---|
[611] | 1430 | #if DEBUG_MAIN |
---|
[619] | 1431 | printf("\n[ksh] main thread started on core[%x,%d]\n", cxy , lid ); |
---|
[574] | 1432 | #endif |
---|
| 1433 | |
---|
| 1434 | // initializes the semaphore used to synchronize with interactive thread |
---|
[469] | 1435 | if ( sem_init( &semaphore , 0 , 1 ) ) |
---|
| 1436 | { |
---|
| 1437 | printf("\n[KSH ERROR] cannot initialize semaphore\n" ); |
---|
| 1438 | exit( 1 ); |
---|
| 1439 | } |
---|
[457] | 1440 | |
---|
[611] | 1441 | #if DEBUG_MAIN |
---|
[574] | 1442 | printf("\n[ksh] main initialized semaphore\n" ); |
---|
| 1443 | #endif |
---|
| 1444 | |
---|
[446] | 1445 | // initialize interactive thread attributes |
---|
| 1446 | attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED; |
---|
| 1447 | attr.cxy = cxy; |
---|
| 1448 | |
---|
| 1449 | // lauch the interactive thread |
---|
| 1450 | pthread_create( &trdid, |
---|
| 1451 | &attr, |
---|
| 1452 | &interactive, // entry function |
---|
| 1453 | NULL ); |
---|
[611] | 1454 | #if DEBUG_MAIN |
---|
[619] | 1455 | printf("\n[ksh] main thread launched interactive thread %x\n", trdid ); |
---|
[574] | 1456 | #endif |
---|
[588] | 1457 | |
---|
| 1458 | // signal INIT process |
---|
| 1459 | kill( 1 , SIGCONT ); |
---|
[446] | 1460 | |
---|
| 1461 | // enter infinite loop monitoring children processes termination |
---|
| 1462 | while( 1 ) |
---|
| 1463 | { |
---|
[457] | 1464 | // wait children termination |
---|
| 1465 | child_pid = wait( &status ); |
---|
[446] | 1466 | |
---|
[611] | 1467 | #if DEBUG_MAIN |
---|
[574] | 1468 | if( WIFEXITED (status) ) printf("\n[ksh] child process %x exit\n" , child_pid ); |
---|
| 1469 | if( WIFSIGNALED(status) ) printf("\n[ksh] child process %x killed\n" , child_pid ); |
---|
| 1470 | if( WIFSTOPPED (status) ) printf("\n[ksh] child process %x stopped\n", child_pid ); |
---|
[446] | 1471 | #endif |
---|
| 1472 | |
---|
[457] | 1473 | // release semaphore if KSH process is TXT owner, to unblock interactive thread |
---|
| 1474 | is_fg( parent_pid , &is_owner ); |
---|
| 1475 | if( is_owner ) sem_post( &semaphore ); |
---|
[446] | 1476 | } |
---|
[436] | 1477 | } // end main() |
---|
[230] | 1478 | |
---|
[446] | 1479 | |
---|