diff --git a/VMEncryption/volume-notif-svc/UdevVolNotif.c b/VMEncryption/volume-notif-svc/UdevVolNotif.c index b88857783..cde15c7a0 100644 --- a/VMEncryption/volume-notif-svc/UdevVolNotif.c +++ b/VMEncryption/volume-notif-svc/UdevVolNotif.c @@ -11,11 +11,6 @@ #include #include #include -#include -#include -#include -#include -#include #define LOG_PRIORITY 3 #define LOG_LEVEL 3 @@ -24,11 +19,13 @@ #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; @@ -36,15 +33,72 @@ time_t ade_finished; 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& 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) @@ -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. @@ -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; @@ -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); } @@ -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&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&dq){ - using itdeque=std::deque::iterator; - std::vector 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&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[]) { @@ -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 dq; while (1) { fd_set fds; struct timeval tv; @@ -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); diff --git a/VMEncryption/volume-notif-svc/azure-diskencryption-vol-notif.service b/VMEncryption/volume-notif-svc/azure-diskencryption-vol-notif.service index cbdced018..2480328c1 100644 --- a/VMEncryption/volume-notif-svc/azure-diskencryption-vol-notif.service +++ b/VMEncryption/volume-notif-svc/azure-diskencryption-vol-notif.service @@ -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 diff --git a/VMEncryption/volume-notif-svc/build.sh b/VMEncryption/volume-notif-svc/build.sh index be650722e..b98e577b4 100755 --- a/VMEncryption/volume-notif-svc/build.sh +++ b/VMEncryption/volume-notif-svc/build.sh @@ -1 +1 @@ -gcc -x c++ UdevVolNotif.c -o ade-volume-notif-svc -lstdc++ -ludev \ No newline at end of file +gcc UdevVolNotif.c -o ade-volume-notif-svc -ludev diff --git a/VMEncryption/volume-notif-svc/ade_daemon_delay.sh b/VMEncryption/volume-notif-svc/daemon_delay.sh similarity index 100% rename from VMEncryption/volume-notif-svc/ade_daemon_delay.sh rename to VMEncryption/volume-notif-svc/daemon_delay.sh