| 1 | #include "../include/TTY.h" |
|---|
| 2 | |
|---|
| 3 | namespace environment { |
|---|
| 4 | namespace tty { |
|---|
| 5 | |
|---|
| 6 | TTY::TTY (std::string name, |
|---|
| 7 | Parameters * param) |
|---|
| 8 | { |
|---|
| 9 | this->name = name; |
|---|
| 10 | this->param = param; |
|---|
| 11 | |
|---|
| 12 | xtty = new xTTY [param->nb_tty]; |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | for (uint32_t i=0; i<param->nb_tty; i++) |
|---|
| 16 | { |
|---|
| 17 | // ***** Create a log_file ***** |
|---|
| 18 | xtty [i].log_file = fopen(param->name_tty[i].c_str(),"w"); |
|---|
| 19 | |
|---|
| 20 | if (xtty [i].log_file == NULL) |
|---|
| 21 | { |
|---|
| 22 | // Error |
|---|
| 23 | std::cerr << "<Tty> Can't open the file : \"" << param->name_tty[i] << "\"" << std::endl; |
|---|
| 24 | exit(1); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | if (param->with_xtty == true) |
|---|
| 28 | { |
|---|
| 29 | // ***** TTY and pipe init ***** |
|---|
| 30 | canal canal_output; |
|---|
| 31 | |
|---|
| 32 | if (pipe (canal_output.CanalPipe) != 0) |
|---|
| 33 | { |
|---|
| 34 | std::cerr << "<Tty> Error in pipe creation" << std::endl; |
|---|
| 35 | exit(1); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | if ((xtty[i].pid = fork()) == 0) |
|---|
| 39 | { |
|---|
| 40 | // Childen -> transform in a xtty |
|---|
| 41 | close(0); // s |
|---|
| 42 | dup (canal_output.CanalPipe [0]); |
|---|
| 43 | close(1); |
|---|
| 44 | execlp("xtty","xtty", param->name_tty [i].c_str() ,NULL); // devient un tty |
|---|
| 45 | |
|---|
| 46 | std::cerr << "<Tty> Error in the creation of tty" << std::endl |
|---|
| 47 | << "Test if environment's variable $PATH is set with ${MORPHEO_TOPLEVEL}/IPs/systemC/Environment/TTY/xtty" << std::endl; |
|---|
| 48 | exit (1); |
|---|
| 49 | } |
|---|
| 50 | else |
|---|
| 51 | { |
|---|
| 52 | // father |
|---|
| 53 | xtty[i].pipe_output = canal_output.CanalPipe [1]; |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | }//end for |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | TTY::~TTY (void) |
|---|
| 60 | { |
|---|
| 61 | // Kill all tty |
|---|
| 62 | for (unsigned int i=0; i<param->nb_tty; i++) |
|---|
| 63 | { |
|---|
| 64 | if (param->with_xtty == true) |
|---|
| 65 | if (xtty[i].pid) |
|---|
| 66 | kill(xtty[i].pid,SIGTERM); |
|---|
| 67 | fclose(xtty[i].log_file); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | delete [] xtty; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | }; |
|---|
| 74 | }; |
|---|