#include <stdlib.h>
#include <unistd.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
void printAppCtrlMenu()
{
printf("\nUsage: app_ctrl_example <test#> <arg1> <arg2> ... \n\n");
printf("Test 1: Create a new Application Process Table entry for the given file name: app_ctrl_example 1 <app-name> \n");
printf("Test 2: Delete an Application Process Table entry for the given file name: app_ctrl_example 2 <app-name> \n");
printf("Test 3: Get start on boot, auto-restart, CPU sharing and memory limit settings for the process entry: app_ctrl_example 3 <app-name> \n");
printf("Test 4: Set start on boot, auto-restart, CPU sharing and memory limit settings for the process entry: app_ctrl_example 4 <app-name> <start-on-boot> <auto-restart> <cpu-share> <max-memory> \n");
printf("Test 5: Start the process with Process Manager using the parameters configured in the process entry: app_ctrl_example 5 <app-name> \n");
printf("Test 6: Stop the process with Process Manager: app_ctrl_example 6 <app-name> \n");
printf("Test 7: Get application status for the process entry: app_ctrl_example 7 <app-name> \n");
printf("Test 8: Set application status for the process entry: app_ctrl_example 8 <app-name> <status-info> \n");
printf("Test 9: Get application version for the process entry: app_ctrl_example 9 <app-name> \n");
printf("Test 10: Set application version for the process entry: app_ctrl_example 10 <app-name> <version-identifier> \n");
printf("\n");
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.
size = strlen(str) + 1;
{
printf("Bad return code trying to create a new application process table entry. (result = %d)\n", result);
}
else
{
printf("New application process table entry created successfully. \n");
}
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
{
printf("Bad return code trying to delete an application process table entry. (result = %d)\n", result);
}
else
{
printf("Application process table entry deleted successfully. \n");
}
return;
}
{
char str[100];
uint32_t cpuShare = 0;
uint32_t maxMemory = 0;
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
{
printf("Bad return code trying to get settings for the process entry. (result = %d)\n", result);
}
else
{
printf(
"Start on boot mode: %s is set\n", (startOnBoot ==
OPEN_ENABLE ?
"Enabled" :
"Disabled"));
printf(
"Auto restart mode: %s is set\n", (autoRestart ==
OPEN_ENABLE ?
"Enabled" :
"Disabled"));
printf("CPU share: (%u)%%\n", cpuShare);
printf("Maximum memory an application is allowed to consume: (%u)MB\n", maxMemory);
}
return;
}
char *startOnBootStr, char *autoRestartStr,
char *cpuShareStr, char *maxMemoryStr)
{
char str[100];
uint32_t *cpuShare = NULL;
uint32_t *maxMemory = NULL;
int val1, val2, val3, val4;
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
if (strcmp(startOnBootStr, "NULL") != 0)
{
val1 = atoi(startOnBootStr);
}
if (strcmp(autoRestartStr, "NULL") != 0)
{
val2 = atoi(autoRestartStr);
}
if (strcmp(cpuShareStr, "NULL") != 0)
{
val3 = atoi(cpuShareStr);
cpuShare = (uint32_t *)&val3;
}
if (strcmp(maxMemoryStr, "NULL") != 0)
{
val4 = atoi(maxMemoryStr);
maxMemory = (uint32_t *)&val4;
}
{
printf("Bad return code trying to set application control attributes for the process entry. (result = %d)\n", result);
}
else
{
printf("Application control attributes set successfully for process entry. \n");
}
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
{
printf("Bad return code trying to start an application. (result = %d)\n", result);
}
else
{
printf("Application start successfully.. \n");
}
return;
}
{
char str[100];
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
{
printf("Bad return code trying to stop an application. (result = %d)\n", result);
}
else
{
printf("Application stop successfully. \n");
}
return;
}
{
char str[100];
char bufStatusInfo[OPEN_APP_CTRL_STATUS_SIZE] = {0};
statusInfo.pstart = bufStatusInfo;
statusInfo.size = sizeof(bufStatusInfo);
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
{
printf("Bad return code trying to get an application status for process entry. (result = %d)\n", result);
}
else
{
printf("Application status for process entry: %s\n", (char *)statusInfo.pstart);
}
return;
}
{
char str[100];
char bufStatusInfo[OPEN_APP_CTRL_STATUS_SIZE];
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
memset(bufStatusInfo, 0, sizeof(bufStatusInfo));
strncpy(bufStatusInfo, statusInformation, (sizeof(bufStatusInfo) - 1));
statusInfo.pstart = bufStatusInfo;
statusInfo.size = strlen(bufStatusInfo) + 1;
{
printf("Bad return code trying to set an application status for process entry. (result = %d)\n", result);
}
else
{
printf("Successfully set an application status for process entry. \n");
}
return;
}
{
char str[100];
char bufVersionIdentifier[OPEN_APP_CTRL_VERSION_SIZE] = {0};
versionIdentifier.pstart = bufVersionIdentifier;
versionIdentifier.size = sizeof(bufVersionIdentifier);
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
{
printf("Bad return code trying to get an application version for process entry. (result = %d)\n", result);
}
else
{
printf("Application version for process entry: %s\n", (char *)versionIdentifier.pstart);
}
return;
}
{
char str[100];
char bufVersionIdentifier[OPEN_APP_CTRL_VERSION_SIZE];
memset(str, 0, sizeof(str));
strncpy(str, appExecName, (sizeof(str) - 1));
buffDesc.pstart = str;
buffDesc.size = strlen(str) + 1;
memset(bufVersionIdentifier, 0, sizeof(bufVersionIdentifier));
strncpy(bufVersionIdentifier, versionId, (sizeof(bufVersionIdentifier) - 1));
versionIdentifier.pstart = bufVersionIdentifier;
versionIdentifier.size = strlen(bufVersionIdentifier) + 1;
{
printf("Bad return code trying to set an application version for process entry. (result = %d)\n", result);
}
else
{
printf("Successfully set an application version for process entry. \n");
}
return;
}
int main(int argc, char **argv)
{
char tmp[256];
uint32_t testNum;
if (argc < 2)
{
printAppCtrlMenu();
exit(1);
}
testNum = atoi(argv[1]);
l7proc_crashlog_register();
{
printf("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
exit(2);
}
{
sleep(1);
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Starting App Control API example application");
memset(tmp, 0, sizeof(tmp));
bufd.pstart = tmp;
bufd.size = sizeof(tmp);
printf("\n");
{
printf("Network OS version = %s\n", tmp);
}
else
{
printf("Network OS version retrieve error\n");
}
{
printf(
"OpEN version = %u.%u.%u.%u\n", version.
release, version.
version, version.
maint_level, version.
build_num);
}
else
{
printf("OpEN version retrieve error.\n");
}
printf("\n");
switch (testNum)
{
case 1:
if (argc != 3)
{
printAppCtrlMenu();
exit(1);
}
appCtrlEntryCreate(&clientHandle, argv[2]);
break;
case 2:
if (argc != 3)
{
printAppCtrlMenu();
exit(1);
}
appCtrlEntryDelete(&clientHandle, argv[2]);
break;
case 3:
if (argc != 3)
{
printAppCtrlMenu();
exit(1);
}
appCtrlAttrGet(&clientHandle, argv[2]);
break;
case 4:
if (argc != 7)
{
printAppCtrlMenu();
exit(1);
}
appCtrlAttrSet(&clientHandle, argv[2], argv[3], argv[4], argv[5], argv[6]);
break;
case 5:
if (argc != 3)
{
printAppCtrlMenu();
exit(1);
}
appCtrlStart(&clientHandle, argv[2]);
break;
case 6:
if (argc != 3)
{
printAppCtrlMenu();
exit(1);
}
appCtrlStop(&clientHandle, argv[2]);
break;
case 7:
if (argc != 3)
{
printAppCtrlMenu();
exit(1);
}
appCtrlStatusGet(&clientHandle, argv[2]);
break;
case 8:
if (argc != 4)
{
printAppCtrlMenu();
exit(1);
}
appCtrlStatusSet(&clientHandle, argv[2], argv[3]);
break;
case 9:
if (argc != 3)
{
printAppCtrlMenu();
exit(1);
}
appCtrlVersionGet(&clientHandle, argv[2]);
break;
case 10:
if (argc != 4)
{
printAppCtrlMenu();
exit(1);
}
appCtrlVersionSet(&clientHandle, argv[2], argv[3]);
break;
default:
printAppCtrlMenu();
break;
}
L7PROC_LOGF(L7PROC_LOG_SEVERITY_INFO, 0, "Stopping App Control API example application");
return 0;
}