Implement ipcs mechanisms.

This commit is contained in:
Enrico Fraccaroli
2023-04-27 10:10:34 +02:00
parent bd3c8fb4fe
commit e19354a9ee
8 changed files with 130 additions and 159 deletions
+74 -111
View File
@@ -3,134 +3,97 @@
/// @copyright (c) 2014-2023 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include <sys/unistd.h>
#include <stdio.h>
#include <ipc/sem.h>
#include <ipc/ipc.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#if 0
static inline void print_sem_stat()
static inline void __print_file_content(const char *path)
{
printf("Semaphores: \n");
printf("%-10s %-10s %-10s %-10s %-10s %-10s \n", "T", "ID", "KEY", "MODE", "OWNER", "GROUP");
printf("%-20s %-10s %-20s \n\n", "", "Semaphores not implemented", "");
// Prepare the buffer for reading.
char buffer[BUFSIZ];
// Clean the buffer.
memset(buffer, 0, BUFSIZ);
// Open the file.
int fd = open(path, O_RDONLY, 42);
if (fd >= 0) {
// Put on the standard output the characters.
while (read(fd, buffer, BUFSIZ) > 0)
puts(buffer);
// Close the file descriptor.
close(fd);
}
}
static inline void print_msg_stat()
{
printf("Message Queues: \n");
printf("%-10s %-10s %-10s %-10s %-10s %-10s \n", "T", "ID", "KEY", "MODE", "OWNER", "GROUP");
printf("%-20s %-10s %-20s \n\n", "", "Message Queues not implemented", "");
}
static inline void print_shm_stat()
{
printf("Shared Memory: \n");
printf("%-10s %-10s %-10s %-10s %-10s %-10s \n", "T", "ID", "KEY", "MODE", "OWNER", "GROUP");
printf("%-20s %-10s %-20s \n\n", "", "Shared Memory not implemented", "");
}
#endif
int main(int argc, char **argv)
{
if (argc>4)
if (argc > 4)
return -1;
if (argc == 1){ /*Default operation, prints all ipcs informations*/
long __res;
__res = ipcs();
return __res;
// Default operation, prints all ipcs informations.
if (argc == 1) {
printf("------ Message Queues --------\n");
__print_file_content("/proc/ipc/msg");
printf("------ Semaphore Arrays --------\n");
__print_file_content("/proc/ipc/sem");
printf("------ Shared Memory Segments --------\n");
__print_file_content("/proc/ipc/shm");
return 0;
}
if (argc == 2){
// all semaphores
if (!strcmp(argv[1], "-s")){
long __res;
__res = semipcs();
return __res;
}
// all shared memories
if (!strcmp(argv[1], "-m")) {
printf("Not Implemented!\n");
return 0;
}
// all message queues
if (!strcmp(argv[1], "-q")){
printf("Not Implemented!\n");
return 0;
}
return -1;
}
else{
// semaphore by id
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);
temp.buf -> sems = (struct sem *)malloc(sizeof(struct sem) * __res);
__res = semctl(atoi(argv[2]), 0, IPC_STAT, &temp);
printf("------ Matrici semafori --------\n");
printf("chiave semid proprietario nsems\n");
printf("%d %d %d %d\n", temp.buf->key, temp.buf->semid, temp.buf->owner, temp.buf->sem_nsems);
if(__res == -1)
return -1;
return __res;
}
// shared memory by id
if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-m")){
printf("Not Implemented!\n");
return 0;
}
// message queue by id
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");
return;
}
char datehour[100] = "";
strdatehour(datehour);
printf("IPC status from " OS_NAME " as of %s\n", datehour);
if (argc == 2) {
if (strcmp(argv[1], "-s") == 0) {
print_sem_stat();
} else if (strcmp(argv[1], "-m") == 0) {
print_shm_stat();
} else if (strcmp(argv[1], "-q") == 0) {
print_msg_stat();
if (!strcmp(argv[1], "-q")) { // Show message queues information.
printf("------ Message Queues --------\n");
__print_file_content("/proc/ipc/msg");
} else if (!strcmp(argv[1], "-s")) { // Show semaphores information.
printf("------ Semaphore Arrays --------\n");
__print_file_content("/proc/ipc/sem");
} else if (!strcmp(argv[1], "-m")) { // Show shared memories information.
printf("------ Shared Memory Segments --------\n");
__print_file_content("/proc/ipc/shm");
} else {
printf("Option not recognize.\n");
printf("%s: Command not found.", argv[0]);
return 1;
}
} else {
print_sem_stat();
print_shm_stat();
print_msg_stat();
return 0;
}
if (!strcmp(argv[1], "-i")) {
// Show details about a specific semaphore.
if (!strcmp(argv[3], "-s")) {
struct semid_ds sem;
union semun temp;
long ret;
// Prepare the data structure.
temp.buf = &sem;
// Retrive the statistics.
ret = semctl(atoi(argv[2]), 0, IPC_STAT, &temp);
// Check if we succeded.
if (!ret) {
//ret = semctl(atoi(argv[2]), 0, GETNSEMS, NULL);
//temp.buf->sems = (struct sem *)malloc(sizeof(struct sem) * ret);
printf("key semid owner perms nsems\n");
printf("%10d %10d %10d %10d %d\n", sem.key, sem.semid, sem.owner, 0, sem.sem_nsems);
return 0;
}
return 1;
}
return;
#endif
// Show details about a specific shared memory.
if (!strcmp(argv[3], "-m")) {
printf("Not Implemented!\n");
return 0;
}
// Show details about a specific message queue.
if (!strcmp(argv[3], "-q")) {
printf("Not Implemented!\n");
return 0;
}
printf("%s: Wrong combination, with `-i` you should provide either `-s`, `-m`, or `-q`.", argv[0]);
return 1;
}
printf("%s: Command not found.", argv[0]);
return 1;
}