Skip to content

Commit

Permalink
Revert "updating c++ code and build using c++."
Browse files Browse the repository at this point in the history
This reverts commit 4c9b33d.
  • Loading branch information
pankajosh committed May 9, 2023
1 parent 4c9b33d commit 13883df
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 78 deletions.
164 changes: 89 additions & 75 deletions VMEncryption/volume-notif-svc/UdevVolNotif.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <deque>
#include<vector>
#include <string>
#include<sstream>
#include<algorithm>

#define LOG_PRIORITY 3
#define LOG_LEVEL 3
Expand All @@ -24,27 +19,86 @@
#define LOG_DIRETORY "/tmp"
#define LOG_FILE_TEMPLATE "%s/ade_vol_notif-%s.log"
#define ADE_EVENT_THRESHOLD 4
#define ADE_COUNT_WAIT_TIME_SEC 30
#define ADE_WAIT_TIME_SEC 900
#define ADE_COUNT_WAIT_TIME 30
#define ADE_WAIT_TIME 900
#define ADE_NOT_STARTED "not_started";
#define ADE_FINISHED "finished";
#define ADE_RUNNING "running";

enum class AdeStatus{ADE_NOT_STARTED,ADE_RUNNING,ADE_FINISHED};
AdeStatus ade_status = AdeStatus::ADE_NOT_STARTED;
char* ade_status = ADE_NOT_STARTED;
time_t ade_finished;


// TODO: Make this a parameter to the program
static char log_file_path[1024];
void custom_log(const char *format, ...);

struct device{
std::string syspath;
std::string action;
struct Node{
struct Node* next;
char* syspath;
};

void addNode(std::deque<device>& dq, const char* syspath, const char* action){
dq.push_back({syspath,action});
struct Node* createNode(const char* syspath){
if(syspath == NULL) return NULL;
struct Node* tmp = (struct Node*)malloc(sizeof(struct Node*));
tmp->next = NULL;
tmp->syspath = (char*) malloc(sizeof(char)*strlen(syspath));
strcpy(tmp->syspath,syspath);
return tmp;
}

struct Node** nextNode(struct Node* node){
if(node==NULL)return NULL;
return &node->next;
}

void removeNode(struct Node** node){
if(*node==NULL) {
custom_log("node is null\n");
return;}
custom_log("removing syspath: %s\n", (*node)->syspath);
struct Node* tmp = *node;
if(tmp->next==NULL){
*node = NULL;
free(tmp->syspath);
free(tmp);
}else{
*node=tmp->next;
free(tmp->syspath);
free(tmp);
}
}

void addNode(struct Node** first, const char* ch){
struct Node* node = createNode(ch);
if(node==NULL) return;
if(*first==NULL){
*first = node;
}else{
node->next = *first;
*first=node;
}
}

int lstLength(struct Node* first){
struct Node* tmp = first;
int count =0;
while(tmp!=NULL){
tmp=tmp->next;
count++;
}
return count;
}
int is_devnode_added(struct Node* first, const char* syspath){
struct Node* tmp = first;
while(tmp!=NULL){
if(strcmp(syspath,tmp->syspath)==0) return 1;
tmp=tmp->next;
}
return 0;
}


void custom_log(const char *format, ...) {
FILE *log_file = fopen(log_file_path, "a");
if (!log_file)
Expand Down Expand Up @@ -125,7 +179,7 @@ void daemonize(int argc, char *argv[])
}

void invoke_ade(const char* path){
ade_status = AdeStatus::ADE_RUNNING;
ade_status = ADE_RUNNING;
pid_t pAde = fork();
if(pAde==0){
// in case of child process.
Expand All @@ -151,8 +205,8 @@ void invoke_ade(const char* path){
custom_log("child process %d created, checking daemon", getpid());
custom_log("changing directory to %s", path);
chdir(path);
execl( "ade_daemon_delay.sh",
"ade_daemon_delay.sh",
execl( "daemon_delay.sh",
"daemon_delay.sh",
"extension_shim.sh",
NULL);
int err = errno;
Expand All @@ -164,7 +218,7 @@ void invoke_ade(const char* path){
pid_t childPid = wait(NULL);
custom_log("child process %d is completed",childPid);
}
ade_status = AdeStatus::ADE_FINISHED;
ade_status = ADE_FINISHED;
ade_finished = time(NULL);
}

Expand All @@ -177,46 +231,17 @@ int is_device_crypted_from_syspath(const char* syspath){
custom_log("get_dev_fsUsage_from_syspath: syspath %s usage status is %s",syspath, fs_usage);
if(fs_usage!=NULL && strcmp(fs_usage,"crypto")==0) ret = 1;
udev_device_unref(dev);
udev_unref(udev);
return ret;
}

bool is_devnode_added(std::deque<device>&dq, const char* syspath, const char* action){
for(auto it:dq)
if( strcmp(it.syspath.c_str(),syspath)==0 &&
strcmp(it.action.c_str(),action)==0) return true;
return false;
}

void cleanCryptedDevFromList(std::deque<device>&dq){
using itdeque=std::deque<device>::iterator;
std::vector<itdeque> cryptedDevices;
//removing crypted changed devices,
for(itdeque it=dq.begin(); it!=dq.end(); it++){
if( it->action =="change" &&
is_device_crypted_from_syspath(it->syspath.c_str())){
cryptedDevices.push_back(it);
void cleanCryptedDevFromList(struct Node** first){
struct Node** tmp = first;
while(*tmp!=NULL){
struct Node** rNode = tmp ;
tmp =&((*tmp)->next);
if(is_device_crypted_from_syspath((*rNode)->syspath)){
removeNode(rNode);
}
}
//remove previously add crypted devices, which are mounted now.
for(itdeque it=dq.begin(); it!=dq.end(); it++){
for(auto itd:cryptedDevices){
if(it==itd) continue;
if(it->action!="change" && it->syspath==itd->syspath){
cryptedDevices.push_back(it); break;
}
}
}
for(auto it:cryptedDevices){
dq.erase(it);
}
}

void printdq(std::deque<device>&dq){
custom_log("deque item count: %d",dq.size());
for(auto it:dq){
custom_log("deque item: action %s, syspath %s",it.action.c_str(), it.syspath.c_str());
}
}

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -292,7 +317,6 @@ int main(int argc, char *argv[]) {
first_dev_node = time(NULL);
ade_finished = time(NULL);
int dev_node_count;
std::deque<device> dq;
while (1) {
fd_set fds;
struct timeval tv;
Expand Down Expand Up @@ -331,55 +355,45 @@ int main(int argc, char *argv[]) {
custom_log("Syspath : %s", syspath);

if ( action != NULL &&
(strcmp(action, "change") == 0 ||
strcmp(action, "add") == 0) &&
strcmp(action, "change") == 0 &&
fsUsage != NULL &&
(strcmp(fsUsage, "filesystem") == 0||
strcmp(fsUsage, "crypto") == 0)){
if(!is_devnode_added(dq,syspath,action)){
strcmp(fsUsage, "filesystem") == 0){
if(is_devnode_added(first,syspath)==0){
custom_log("adding Device node %s in list\n",devnode);
addNode(dq,syspath,action);
addNode(&first,syspath);
if(dev_node_count ==0){
first_dev_node = time(NULL);
}
}
}
//ADE generated dev events must be removed from list.
cleanCryptedDevFromList(dq);
printdq(dq);
cleanCryptedDevFromList(&first);

custom_log("Processing udev monitoring event is done!\n");
udev_device_unref(dev);
}
usleep(250*1000);
dev_node_count = dq.size();
dev_node_count = lstLength(first);
int diff_for_ade_loop = (int)difftime(time(NULL),ade_finished);
int diff_for_dev_nodes = dev_node_count>0?(int)difftime(time(NULL),first_dev_node):0;
bool ade_invoked = false;
//Logic to invoke ADE.
if (dev_node_count >= ADE_EVENT_THRESHOLD){
custom_log("dev node count %d max to trigger %d for running ADE!"
,dev_node_count,ADE_EVENT_THRESHOLD);
invoke_ade(current_working_directory);
ade_invoked=true;
}else if(diff_for_ade_loop>=ADE_WAIT_TIME_SEC){
}else if(diff_for_ade_loop>=ADE_WAIT_TIME){
custom_log("running ADE in every %d sec, last ade run was at %s, diff: %d, dev nodes in list %d"
,ADE_WAIT_TIME_SEC,ctime(&ade_finished),diff_for_ade_loop,dev_node_count);
,ADE_WAIT_TIME,ctime(&ade_finished),diff_for_ade_loop,dev_node_count);
custom_log("diff_for_ade_loop: %d, diff_for_dev_nodes: %d",diff_for_ade_loop,diff_for_dev_nodes);
invoke_ade(current_working_directory);
ade_invoked=true;
}else if(dev_node_count>0 && diff_for_dev_nodes>ADE_COUNT_WAIT_TIME_SEC){
}else if(dev_node_count>0 && diff_for_dev_nodes>ADE_COUNT_WAIT_TIME){
custom_log("running ADE, dev nodes in list %d, last ade run was at %s, diff: %d"
,dev_node_count,ctime(&first_dev_node),diff_for_dev_nodes);
custom_log("diff_for_ade_loop: %d, diff_for_dev_nodes: %d",diff_for_ade_loop,diff_for_dev_nodes);
invoke_ade(current_working_directory);
ade_invoked=true;
}else{
printf("...");
}
if(ade_invoked){
first_dev_node = time(NULL);
}

}
udev_monitor_unref(mon);
udev_unref(udev);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ RestartSec=3
TimeoutSec=30
IgnoreSIGPIPE=no
KillMode=control-group
WorkingDirectory=WorkingDirectory
ExecStart=WorkingDirectory/ade-volume-notif-svc -d
WorkingDirectory=/var/lib/waagent/Microsoft.Azure.Security.Edp.AzureDiskEncryptionForLinuxTest1-1.2.0.105
ExecStart=/var/lib/waagent/Microsoft.Azure.Security.Edp.AzureDiskEncryptionForLinuxTest1-1.2.0.105/ade-volume-notif-svc -d
#ExecStop=/bin/bash -c '$$(which kill) -15 $MAINPID'
Delegate=yes
MemoryMax=50MB
Expand Down
2 changes: 1 addition & 1 deletion VMEncryption/volume-notif-svc/build.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gcc -x c++ UdevVolNotif.c -o ade-volume-notif-svc -lstdc++ -ludev
gcc UdevVolNotif.c -o ade-volume-notif-svc -ludev

0 comments on commit 13883df

Please sign in to comment.