Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
baiguerapaolo committed May 24, 2019
2 parents 1a8aa82 + b6abd44 commit 191144a
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 70 deletions.
16 changes: 7 additions & 9 deletions Progetto/src/devices/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ volatile int flag_usr2 = 0;
volatile int flag_term = 0;
volatile int flag_cont = 0;


void term();
void read_msgqueue(int msgid, int* device_pids);

Expand All @@ -38,7 +37,7 @@ void sighandler_int(int sig) {
if (sig == SIGTERM) {
flag_term = 1;
}
if(sig == SIGCONT){
if (sig == SIGCONT) {
flag_cont = 1;
}
}
Expand Down Expand Up @@ -248,22 +247,21 @@ int main(int argc, char* argv[]) {
if (flag_term) {
term();
}
if(flag_cont){
if (flag_cont) {
flag_cont = 0;
printf("SIGCONT\n");
int ret = msgrcv(msgid_pid, &message, sizeof(message), 1, IPC_NOWAIT);
printf("Messaggio: %s\n", message.mesg_text);
if(ret != -1) {
if (ret != -1) {
int pid = atoi(message.mesg_text);
int k = 0;
for (k = 0; k < MAX_CHILDREN; k++) {
if (children_pids[k]== pid) {
if (children_pids[k] == pid) {
printf("FIGLIO ELIMINATO\n");
children_pids[k] = -1;
}
}
}
else{
}
} else {
lprintf("Messaggio dummy\n");
}
}
Expand Down Expand Up @@ -317,7 +315,7 @@ void term() {
}
}
message.mesg_type = 1;

sprintf(message.mesg_text, "%d%s", count, tmp);
msgsnd(msgid, &message, sizeof(message), 0);
key_t key = ftok("/tmp/ipc/shellqueue",1);
Expand Down
100 changes: 92 additions & 8 deletions Progetto/src/devices/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ int device_type = -1;

key_t key;
int msgid;
key_t key_pid;
int msgid_pid;

struct tm tm_start;
struct tm tm_end;
Expand All @@ -26,6 +28,7 @@ volatile int flag_usr1 = 0;
volatile int flag_usr2 = 0;
volatile int flag_term = 0;
volatile int flag_alarm = 0;
volatile int flag_cont = 0;

void switch_child() {
char switch_names[5][MAX_BUF_SIZE];
Expand Down Expand Up @@ -69,6 +72,9 @@ void sighandler_int(int sig) {
if (sig == SIGALRM) {
flag_alarm = 1;
}
if (sig == SIGCONT) {
flag_cont = 1;
}
}

int main(int argc, char* argv[]) {
Expand All @@ -80,6 +86,11 @@ int main(int argc, char* argv[]) {
char** vars = NULL;
int mode;

char* raw_info;
char* shifted_tmp;

int ret;

this_pipe = argv[2];
pid = getpid();
__index = atoi(argv[1]);
Expand Down Expand Up @@ -115,17 +126,14 @@ int main(int argc, char* argv[]) {
tm_end.tm_hour, tm_end.tm_min,
children_pids[0] != -1);
if (children_pids[0] != -1) {
char* raw_info = get_raw_device_info(children_pids[0]);
raw_info = get_raw_device_info(children_pids[0]);
if (raw_info != NULL) {
strcat(tmp, raw_info);
strcat(tmp, "|!|");
free(raw_info);
}
}
strcat(tmp, "!>"); /*
write(fd, tmp, MAX_BUF_SIZE);
*/

strcat(tmp, "!>");
message.mesg_type = 1;
sprintf(message.mesg_text, "%s", tmp);
msgsnd(msgid, &message, sizeof(message), 0);
Expand Down Expand Up @@ -156,7 +164,7 @@ int main(int argc, char* argv[]) {

flag_alarm = 1;
} else if (mode == 1) {
char* shifted_tmp = malloc(MAX_BUF_SIZE * sizeof(shifted_tmp));
shifted_tmp = malloc(MAX_BUF_SIZE * sizeof(shifted_tmp));
strcpy(shifted_tmp, tmp);
shifted_tmp = shifted_tmp + 2;
vars = split(shifted_tmp);
Expand All @@ -176,8 +184,17 @@ int main(int argc, char* argv[]) {
}

if (flag_term) {
msgctl(msgid, IPC_RMID, NULL);
exit(0);
term();
}

if (flag_cont) {
flag_cont = 0;
ret = msgrcv(msgid_pid, &message, sizeof(message), 1, IPC_NOWAIT);
if (ret != -1) {
if (children_pids[0] == atoi(message.mesg_text)) {
children_pids[0] = -1;
}
}
}

if (flag_alarm) {
Expand All @@ -189,3 +206,70 @@ int main(int argc, char* argv[]) {

return 0;
}

void term() {
int done = 1;
int i;
char tmp[MAX_BUF_SIZE - sizeof(int)]; /* POI VA CONCATENATO */

int count = 0;
char intern[MAX_BUF_SIZE];
char* info;

sprintf(tmp, "-");
if (children_pids[0] != -1) {
count++;
info = get_raw_device_info(children_pids[0]);
sprintf(intern, "-%s", info);
strcat(tmp, intern);
kill(children_pids[0], SIGTERM);
}
message.mesg_type = 1;

sprintf(message.mesg_text, "%d%s", count, tmp);
msgsnd(msgid, &message, sizeof(message), 0);

if (done) {
exit(0);
} else {
printf("Errore nell'eliminazione\n");
}
}

void read_msgqueue(int msgid, int* device_pids) {
int n_devices;
int ret;
int q, j;
char n_dev_str[100];
int __count;
char tmp_buf[MAX_BUF_SIZE];
char** vars;
char** son_j;

ret = msgrcv(msgid, &message, sizeof(message), 1, IPC_NOWAIT);
if (ret != -1) {
q = 0;
while (!(message.mesg_text[q] == '-')) {
n_dev_str[q] = message.mesg_text[q];
q++;
}
n_dev_str[q] = '\0';
n_devices = atoi(n_dev_str);
if (n_devices > 0) {
__count = n_devices;
sprintf(tmp_buf, "%s", message.mesg_text);
vars = NULL;
vars = split_sons(tmp_buf, __count);
j = 0;
while (j <= __count) {
if (j >= 1) {
printf("\nVars %d: %s\n", j, vars[j]);
son_j = split(vars[j]);
__add_ex(son_j, children_pids);
printf("\nADD_EX GOOD\n");
}
j++;
}
}
}
}
117 changes: 64 additions & 53 deletions Progetto/src/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,37 @@ int main(int argc, char *argv[]) {
children_pids[j] = -1; /* se è -1 non contiene nulla */
}

if (argc != 2 || strcmp(argv[1], "--no-wrapper") != 0) {
get_pipe_name((int)getpid(), pipe);
mkfifo(pipe, 0666);
fd = open(pipe, O_RDWR);
get_pipe_name((int)getpid(), pipe);
mkfifo(pipe, 0666);
fd = open(pipe, O_RDWR);

/* PID del launcher. */
ppid = atoi(argv[1]);
/* PID del launcher. */
ppid = atoi(argv[1]);

/* Credo message queue tra shell e launcher */
key = ftok("/tmp", 100000);
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;
/* Credo message queue tra shell e launcher */
key = ftok("/tmp", 100000);
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;

/*Creo message queue per comunicare shellpid */
key_sh = ftok("/tmp", 200000);
msgid_sh = msgget(key_sh, 0666 | IPC_CREAT);
message.mesg_type = 1;
/*Creo message queue per comunicare shellpid */
key_sh = ftok("/tmp", 200000);
msgid_sh = msgget(key_sh, 0666 | IPC_CREAT);
message.mesg_type = 1;

key_shell = ftok("/tmp/ipc/shellqueue",1);
msgid_shell = msgget(key_shell, 0666 | IPC_CREAT);
message.mesg_type = 1;
key_shell = ftok("/tmp/ipc/shellqueue", 1);
msgid_shell = msgget(key_shell, 0666 | IPC_CREAT);
message.mesg_type = 1;

sprintf(message.mesg_text, "%d", (int)getpid());
msgsnd(msgid_sh, &message, MAX_BUF_SIZE, 0);
}
sprintf(message.mesg_text, "%d", (int)getpid());
msgsnd(msgid_sh, &message, MAX_BUF_SIZE, 0);

/* Ready */
system("clear");

while (1) {
if (stato) {
/*Scrive numero devices e elenco dei pid a launcher. */
if ((argc != 2 || strcmp(argv[1], "--no-wrapper") != 0) && changed) {
if (changed) {
/*Ripulisco Forzatamente. */
msgrcv(msgid, &message, sizeof(message), 1, IPC_NOWAIT);

Expand Down Expand Up @@ -187,16 +186,16 @@ int add_shell(char buf[][MAX_BUF_SIZE], int *device_i, int *children_pids, char

void cleanup_sig(int sig) {
printf("Chiusura della centralina in corso...\n");
int i=0;
for(i=1; i< max_index;i++ ){
int i = 0;
for (i = 1; i < max_index; i++) {
key_t key = ftok("/tmp/ipc/mqueues", i);
int msgid = msgget(key, 0666 | IPC_CREAT);
msgctl(msgid, IPC_RMID, NULL);
}
key_t key_shell = ftok("/tmp/ipc/shellqueue",1);
int msgid_shell = msgget(key_shell, 0666 | IPC_CREAT);
key_t key_shell = ftok("/tmp/ipc/shellqueue", 1);
int msgid_shell = msgget(key_shell, 0666 | IPC_CREAT);
message.mesg_type = 1;
msgctl(msgid_shell,IPC_RMID,NULL );
msgctl(msgid_shell, IPC_RMID, NULL);
kill(ppid, SIGTERM);
kill(0, SIGKILL);
}
Expand All @@ -217,6 +216,7 @@ void stop_sig(int sig) {
}

void link_child(int signal) {
<<<<<<< HEAD
printf("ARRRIVATO SIGUSR2 FLAG: %d\n",flag);
if(flag){
int n_devices;
Expand All @@ -227,37 +227,48 @@ void link_child(int signal) {
char tmp_buf[MAX_BUF_SIZE];
char** vars;
char** son_j;
=======
if (flag) {
int n_devices;
int ret;
int q, j;
char n_dev_str[100];
int __count;
char tmp_buf[MAX_BUF_SIZE];
char **vars;
char **son_j;
>>>>>>> b6abd4485d6edac12a751501ade6164089bc2f0f

key_t key_shell = ftok("/tmp/ipc/shellqueue",1);
int msgid_shell = msgget(key_shell, 0666 | IPC_CREAT);
message.mesg_type = 1;
key_t key_shell = ftok("/tmp/ipc/shellqueue", 1);
int msgid_shell = msgget(key_shell, 0666 | IPC_CREAT);
message.mesg_type = 1;

ret = msgrcv(msgid_shell, &message, sizeof(message), 1, IPC_NOWAIT);
if (ret != -1) {
q = 0;
while (!(message.mesg_text[q] == '-')) {
n_dev_str[q] = message.mesg_text[q];
q++;
}
n_dev_str[q] = '\0';
n_devices = atoi(n_dev_str);
if (n_devices > 0) {
__count = n_devices;
sprintf(tmp_buf, "%s", message.mesg_text);
vars = NULL;
vars = split_sons(tmp_buf, __count);
j = 0;
while (j <= __count) {
if (j >= 1) {
printf("\nVars %d: %s\n", j, vars[j]);
son_j = split(vars[j]);
__add_ex(son_j, children_pids);
printf("\nADD_EX GOOD\n");
ret = msgrcv(msgid_shell, &message, sizeof(message), 1, IPC_NOWAIT);
if (ret != -1) {
q = 0;
while (!(message.mesg_text[q] == '-')) {
n_dev_str[q] = message.mesg_text[q];
q++;
}
n_dev_str[q] = '\0';
n_devices = atoi(n_dev_str);
if (n_devices > 0) {
__count = n_devices;
sprintf(tmp_buf, "%s", message.mesg_text);
vars = NULL;
vars = split_sons(tmp_buf, __count);
j = 0;
while (j <= __count) {
if (j >= 1) {
printf("\nVars %d: %s\n", j, vars[j]);
son_j = split(vars[j]);
__add_ex(son_j, children_pids);
printf("\nADD_EX GOOD\n");
}
j++;
}
j++;
}
}
}
flag = 0;
flag = 0;
}
}
Loading

0 comments on commit 191144a

Please sign in to comment.