diff --git a/.vscode/settings.json b/.vscode/settings.json index ead0294..ee6f1f1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,14 @@ { "files.associations": { "*.h": "c", - "*.c": "c" + "*.c": "c", + "limits": "c", + "compare": "c", + "type_traits": "c", + "typeinfo": "c", + "*.tcc": "c", + "bit": "c", + "cstdlib": "c" }, + "cmake.configureOnOpen": false, } \ No newline at end of file diff --git a/libc/inc/ipc/ipc.h b/libc/inc/ipc/ipc.h index 6cea578..a72ff09 100644 --- a/libc/inc/ipc/ipc.h +++ b/libc/inc/ipc/ipc.h @@ -39,3 +39,7 @@ struct ipc_perm { /// @param id integer /// @return IPC key key_t ftok(char *path, int id); + +/// @brief Prints System V IPC Informations +/// @return 0 on success, -1 on failure and errno is set to indicate the error. +long ipcs(); \ No newline at end of file diff --git a/libc/inc/ipc/sem.h b/libc/inc/ipc/sem.h index b5d4916..7ebea94 100644 --- a/libc/inc/ipc/sem.h +++ b/libc/inc/ipc/sem.h @@ -23,6 +23,7 @@ #define GETZCNT 15 ///< Get semzcnt. #define SETVAL 16 ///< Set semval. #define SETALL 17 ///< Set all semval's. +#define GETNSEMS 19 ///< Get sem_nsems #define OPERATION_NOT_ALLOWED -18 ///< Return value for blocking operations on semaphores. /// }@ @@ -105,6 +106,10 @@ long sys_semop(int semid, struct sembuf *sops, unsigned nsops); /// @return 0 on success, -1 on failure and errno is set to indicate the error. long sys_semctl(int semid, int semnum, int cmd, union semun *arg); +/// @brief Prints System V semaphores Informations +/// @return 0 on success, -1 on failure and errno is set to indicate the error. +long sys_semipcs(); + #else /// @brief Get a System V semaphore set identifier. @@ -131,4 +136,8 @@ long semop(int semid, struct sembuf *sops, unsigned nsops); /// @return 0 on success, -1 on failure and errno is set to indicate the error. long semctl(int semid, int semnum, int cmd, union semun *arg); +/// @brief Prints System V semaphores Informations +/// @return 0 on success, -1 on failure and errno is set to indicate the error. +long semipcs(); + #endif diff --git a/libc/inc/system/syscall_types.h b/libc/inc/system/syscall_types.h index 6c29f8c..770ce47 100644 --- a/libc/inc/system/syscall_types.h +++ b/libc/inc/system/syscall_types.h @@ -193,7 +193,8 @@ #define __NR_shmctl 197 ///< System-call number for `shmctl` #define __NR_shmdt 198 ///< System-call number for `shmdt` #define __NR_shmget 199 ///< System-call number for `shmget` -#define SYSCALL_NUMBER 200 ///< The total number of system-calls. +#define __NR_semipcs 200 ///< System-call number for `ipcs` +#define SYSCALL_NUMBER 201 ///< The total number of system-calls. /// @brief Handle the value returned from a system call. /// @param type Specifies the type of the returned value. diff --git a/libc/src/ipc/ipc.c b/libc/src/ipc/ipc.c index b6a1404..0185886 100644 --- a/libc/src/ipc/ipc.c +++ b/libc/src/ipc/ipc.c @@ -7,11 +7,13 @@ #include "system/syscall_types.h" #include "sys/errno.h" #include "stddef.h" - +#include "string.h" +#include "io/debug.h" +#include "stdio.h" #include "ipc/sem.h" #include "ipc/shm.h" #include "ipc/msg.h" - +#include "stdlib.h" #include "sys/stat.h" _syscall3(void *, shmat, int, shmid, const void *, shmaddr, int, shmflg) @@ -34,8 +36,73 @@ _syscall5(long, msgrcv, int, msqid, struct msgbuf *, msgp, size_t, msgsz, long, _syscall3(long, msgctl, int, msqid, int, cmd, struct msqid_ds *, buf) + + + +long ipcs(int argc, char** argv){ + + long __res; + __inline_syscall0(__res, semipcs); + __syscall_return(long, __res); + + /*if (argc>4)return -1; + if (argc == 1){ //default + long __res; + __inline_syscall0(__res, semipcs); + return __res; + } + if (argc == 2){ + if (!strcmp(argv[1], "-s")){ //semaphores + long __res; + __inline_syscall0(__res, semipcs); + return __res; + } + + if (!strcmp(argv[1], "-m")) { //shared memories + printf("Not Implemented!\n"); + return 0; + } + + if (!strcmp(argv[1], "-q")){ //message queues + printf("Not Implemented!\n"); + return 0; + } + + return -1; + } + else{ + if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-s")){ + union semun temp; + temp.buf = (struct semid_ds *)malloc(sizeof(struct semid_ds)); + long __res; + __inline_syscall4(__res, semctl, atoi(argv[2]), 0, GETNSEMS, NULL); + temp.buf -> sems = (struct sem *)malloc(sizeof(struct sem) * __res); + + __inline_syscall4(__res, semctl, atoi(argv[2]), 0, IPC_STAT, &temp); + printf("%d\t\t%d\t\t%d\t\t\t%d\n", temp.buf->key, temp.buf->semid, temp.buf->owner, temp.buf->sem_nsems); + if(__res == -1) + return -1; + return __res; + } + if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-m")){ + printf("Not Implemented!\n"); + return 0; + } + if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-q")){ + printf("Not Implemented!\n"); + return 0; + } + return -1; + } + + + return 0;*/ + +} + long semop(int semid, struct sembuf *sops, unsigned nsops) { + long __res; // Check the arguments. if ((nsops <= 0) || (sops == NULL)) { diff --git a/mentos/src/ipc/sem.c b/mentos/src/ipc/sem.c index 047269d..cae5521 100644 --- a/mentos/src/ipc/sem.c +++ b/mentos/src/ipc/sem.c @@ -80,6 +80,13 @@ void semid_print(struct semid_ds *temp) } } +/// @brief prints informations about the struct pointed by temp +/// @param temp the pointer to the semid struct to print +void show_ipcs(struct semid_ds *temp){ + pr_debug("%d\t\t%d\t\t%d\t\t\t%d\n", temp->key, temp->semid, temp->owner, temp->sem_nsems); + +} + /// @brief list of all current active semaphores list_t *current_semaphores; @@ -236,6 +243,7 @@ long sys_semctl(int semid, int semnum, int cmd, union semun *arg) case IPC_STAT: if (arg->buf == NULL || arg->buf->sems == NULL) { /*checking the parameters*/ errno = EINVAL; + pr_debug("Errore SEMCTL\n"); return -1; } @@ -325,6 +333,10 @@ long sys_semctl(int semid, int semnum, int cmd, union semun *arg) return temp->sems[semnum].sem_zcnt; + //return the number of semaphores in the set. + case GETNSEMS: + return temp->sem_nsems; + //not a valid argument. default: errno = EINVAL; @@ -334,4 +346,22 @@ long sys_semctl(int semid, int semnum, int cmd, union semun *arg) return 0; } +long sys_semipcs(){ + + if (current_semaphores == NULL){ + pr_debug("------ Matrici semafori --------\n"); + pr_debug("chiave\t\tsemid\t\tproprietario\t\tnsems\n"); + return 0; + } + + pr_debug("------ Matrici semafori --------\n"); + pr_debug("chiave\t\tsemid\t\tproprietario\t\tnsems\n"); + listnode_foreach(listnode, current_semaphores) + { //iterate through the list + show_ipcs(((struct semid_ds *)listnode->value)); + } + + return 0; +} + ///! @endcond diff --git a/mentos/src/system/syscall.c b/mentos/src/system/syscall.c index a6d1ff8..6f357be 100644 --- a/mentos/src/system/syscall.c +++ b/mentos/src/system/syscall.c @@ -237,6 +237,7 @@ void syscall_init() sys_call_table[__NR_shmctl] = (SystemCall)sys_shmctl; sys_call_table[__NR_shmdt] = (SystemCall)sys_shmdt; sys_call_table[__NR_shmget] = (SystemCall)sys_shmget; + sys_call_table[__NR_semipcs] = (SystemCall)sys_semipcs; isr_install_handler(SYSTEM_CALL, &syscall_handler, "syscall_handler"); } diff --git a/programs/ipcrm.c b/programs/ipcrm.c index 520f462..c25454f 100644 --- a/programs/ipcrm.c +++ b/programs/ipcrm.c @@ -3,8 +3,14 @@ /// @copyright (c) 2014-2023 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include + + int main(int argc, char **argv) { + + printf("Comando...\n"); + #if 0 if (argc != 2) { printf("Bad arguments: you have to specify only IPC id, see ipcs.\n"); diff --git a/programs/ipcs.c b/programs/ipcs.c index e4d4551..30169d0 100644 --- a/programs/ipcs.c +++ b/programs/ipcs.c @@ -3,6 +3,13 @@ /// @copyright (c) 2014-2023 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include +#include +#include +#include +#include + + #if 0 static inline void print_sem_stat() { @@ -28,6 +35,68 @@ static inline void print_shm_stat() int main(int argc, char **argv) { + + /*if (ipcs(argc, argv) == -1){ + printf("Errore\n"); + }*/ + + if (argc>4)return -1; + if (argc == 1){ //default + long __res; + __res = ipcs(); + return __res; + } + if (argc == 2){ + if (!strcmp(argv[1], "-s")){ //semaphores + long __res; + __res = ipcs(); + return __res; + } + + if (!strcmp(argv[1], "-m")) { //shared memories + printf("Not Implemented!\n"); + return 0; + } + + if (!strcmp(argv[1], "-q")){ //message queues + printf("Not Implemented!\n"); + return 0; + } + + return -1; + } + else{ + if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-s")){ + union semun temp; + temp.buf = (struct semid_ds *)malloc(sizeof(struct semid_ds)); + long __res; + __res = semctl(atoi(argv[2]), 0, GETNSEMS, NULL); + //__inline_syscall4(__res, semctl, atoi(argv[2]), 0, GETNSEMS, NULL); + temp.buf -> sems = (struct sem *)malloc(sizeof(struct sem) * __res); + + __res = semctl(atoi(argv[2]), 0, IPC_STAT, &temp); + //__inline_syscall4(__res, semctl, atoi(argv[2]), 0, IPC_STAT, &temp); + printf("%d\t\t%d\t\t%d\t\t\t%d\n", temp.buf->key, temp.buf->semid, temp.buf->owner, temp.buf->sem_nsems); + if(__res == -1) + return -1; + return __res; + } + if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-m")){ + printf("Not Implemented!\n"); + return 0; + } + if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-q")){ + printf("Not Implemented!\n"); + return 0; + } + return -1; + } + + + return 0; + + + #if 0 if (argc > 2) { printf("Too much arguments.\n"); diff --git a/programs/tests/t_semget.c b/programs/tests/t_semget.c index 2ad2712..cf0aba9 100644 --- a/programs/tests/t_semget.c +++ b/programs/tests/t_semget.c @@ -54,8 +54,6 @@ int main() op2.sem_flg = 0; semop(id, &op2, 1); - semctl(id, 0, IPC_RMID, NULL); - exit(0); }