2019-10-23 21:39:09 +02:00
|
|
|
#include <cstdlib>
|
2019-10-19 17:12:14 +02:00
|
|
|
#include "SharedMemory.h"
|
2019-11-06 13:07:33 +01:00
|
|
|
#include <iostream>
|
2019-11-12 17:26:21 +01:00
|
|
|
#include <fstream>
|
2019-10-19 17:12:14 +02:00
|
|
|
|
2019-11-07 17:59:06 +01:00
|
|
|
key_t changedKey()
|
|
|
|
{
|
2019-11-12 17:26:21 +01:00
|
|
|
std::ofstream keyFile;
|
|
|
|
keyFile.open("key.log", std::ofstream::out | std::ofstream::trunc);
|
|
|
|
keyFile.clear();
|
2019-11-07 17:59:06 +01:00
|
|
|
int newKey;
|
|
|
|
std::cout << "Type in a new Key for the Shared Memory Segment" << std::endl;
|
|
|
|
std::cout << "new Key#";
|
|
|
|
std::cin >> newKey;
|
|
|
|
std::cout <<"New Key is -> " << newKey << std::endl;
|
2019-11-12 17:26:21 +01:00
|
|
|
keyFile << newKey;
|
|
|
|
keyFile.close();
|
2019-11-07 17:59:06 +01:00
|
|
|
return key_t(newKey);
|
|
|
|
}
|
2019-11-04 21:02:27 +01:00
|
|
|
|
2019-11-07 17:59:06 +01:00
|
|
|
std::string getAnswerFromUser()
|
|
|
|
{
|
|
|
|
std::cout <<"answer# ";
|
|
|
|
std::string answer;
|
|
|
|
std::cin >> answer;
|
|
|
|
if(answer.compare("y") == 0 || answer.compare("n") == 0|| answer.compare("override") == 0) {
|
|
|
|
return answer;
|
2019-11-06 13:07:33 +01:00
|
|
|
} else {
|
2019-11-07 17:59:06 +01:00
|
|
|
std::cout << "wrong input: pls answer with (y) for yes, (n) for no, or (override)\n";
|
|
|
|
getAnswerFromUser();
|
2019-11-06 13:07:33 +01:00
|
|
|
}
|
2019-11-07 18:05:59 +01:00
|
|
|
return "-0";
|
2019-10-27 19:46:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-07 17:59:06 +01:00
|
|
|
void initSharedMemory(void) {
|
2019-11-13 13:49:02 +01:00
|
|
|
if(vkvm::impl.sharedMemoryKey != key_t(0)) {
|
|
|
|
if (shmget(vkvm::impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0)
|
2019-11-07 17:59:06 +01:00
|
|
|
{
|
2019-11-13 13:49:02 +01:00
|
|
|
std::cout << "Shared Memory with Key: " << vkvm::impl.sharedMemoryKey << " is already in use\n"
|
2019-11-07 17:59:06 +01:00
|
|
|
<<"You can change the Key( type -> y or n ), or you can override the shared memory Segment(type -> override)\n";
|
|
|
|
std::string answer = getAnswerFromUser();
|
|
|
|
if(answer.compare("y") == 0)
|
|
|
|
{
|
2019-11-13 13:49:02 +01:00
|
|
|
vkvm::impl.sharedMemoryKey = changedKey();
|
|
|
|
std::cout << "new Try with Key:c " << vkvm::impl.sharedMemoryKey << std::endl;
|
2019-11-07 17:59:06 +01:00
|
|
|
initSharedMemory();
|
|
|
|
} else if(answer.compare("n") == 0) {
|
|
|
|
std::cout << "This will end shared memory" <<std::endl;
|
|
|
|
exit(0);
|
|
|
|
} else if(answer.compare("override") == 0) {
|
|
|
|
deleteSharedMemory();
|
|
|
|
initSharedMemory();
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-13 13:49:02 +01:00
|
|
|
std::cout << "Shared Memory is (Re-) allocated with Key: " << vkvm::impl.sharedMemoryKey << std::endl;
|
2019-11-07 17:59:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-13 13:49:02 +01:00
|
|
|
std::cerr <<"key -> " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl;
|
|
|
|
vkvm::impl.sharedMemoryKey = changedKey();
|
2019-11-07 17:59:06 +01:00
|
|
|
initSharedMemory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:07:33 +01:00
|
|
|
void deleteSharedMemory(int s)
|
|
|
|
{
|
2019-11-07 17:59:06 +01:00
|
|
|
std::cerr << "programm ended with Code: " << s << std::endl;
|
2019-11-13 13:49:02 +01:00
|
|
|
if (shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 )
|
2019-11-06 13:07:33 +01:00
|
|
|
{
|
2019-11-07 17:59:06 +01:00
|
|
|
std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs, then delete with ipcrm -m $memID " << std::endl;
|
2019-11-06 13:07:33 +01:00
|
|
|
} else {
|
|
|
|
std::cout << "shared Memory deleted" << std::endl;
|
|
|
|
}
|
2019-10-20 20:38:12 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 13:07:33 +01:00
|
|
|
void deleteSharedMemory(void)
|
|
|
|
{
|
2019-11-13 13:49:02 +01:00
|
|
|
if (shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 )
|
2019-11-06 13:07:33 +01:00
|
|
|
{
|
|
|
|
std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs " << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "shared Memory deleted" << std::endl;
|
2019-10-20 20:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|