diff --git a/pwrapic/inc/powerapi.h b/pwrapic/inc/powerapi.h index 6eacb409198d4e0e290e79571f2b0b8a698f3aac..3c4c08e87ee976ea502f9d882680b390c25e6b70 100644 --- a/pwrapic/inc/powerapi.h +++ b/pwrapic/inc/powerapi.h @@ -29,6 +29,7 @@ extern "C" { // Common PWR_API int PWR_SetLogCallback(void(LogCallback)(int level, const char *fmt, va_list vl)); PWR_API int PWR_SetServerInfo(const char* socketPath); +PWR_API int PWR_SetClientSockPath(const char* socketPath); PWR_API int PWR_Register(void); PWR_API int PWR_UnRegister(void); PWR_API int PWR_RequestControlAuth(void); diff --git a/pwrapic/inc/sockclient.h b/pwrapic/inc/sockclient.h index dde3f82bdc0630543a9aebb72c27ec0d65e794db..0c0bfe6ee67a0f18ff59475aa3524ee18767cd16 100644 --- a/pwrapic/inc/sockclient.h +++ b/pwrapic/inc/sockclient.h @@ -36,6 +36,7 @@ typedef enum PwrApiStatus { } PwrApiStatus; int SetServerInfo(const char* socketPath); +int SetClientSockPath(const char* socketPath); int InitSockClient(void); int FiniSockClient(void); int SetMetaDataCallback(void(MetaDataCallback)(const PWR_COM_CallbackData *)); diff --git a/pwrapic/src/powerapi.c b/pwrapic/src/powerapi.c index 1eb4eadeb8a687fa4374c61fb440fadaae3a75c9..a20257026c90d34b5a546ddd44b7191b462adc7e 100644 --- a/pwrapic/src/powerapi.c +++ b/pwrapic/src/powerapi.c @@ -98,6 +98,14 @@ int PWR_SetServerInfo(const char* socketPath) return PWR_ERR_NULL_POINTER; } +int PWR_SetClientSockPath(const char* socketPath) +{ + if (socketPath) { + return SetClientSockPath(socketPath); + } + return PWR_ERR_NULL_POINTER; +} + int PWR_Register(void) { if (GetPwrApiStatus() != STATUS_UNREGISTERED) { diff --git a/pwrapic/src/sockclient.c b/pwrapic/src/sockclient.c index ea7b619e78e93577332b74c6c6cd15b888f33126..550a15f528fc4b726db956085dcc073692731560 100644 --- a/pwrapic/src/sockclient.c +++ b/pwrapic/src/sockclient.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include "pwrlog.h" @@ -45,6 +46,7 @@ static PwrMsgBuffer g_sendBuff; // Send queue static PwrMsgBuffer g_recvBuff; // Receive queue static ResultWaitingMsgList g_waitList; // Waiting for results list static char g_serverAddr[MAX_PATH_LEN] = "/etc/sysconfig/pwrapis/pwrserver.sock"; // Default server path +static char cus_clientAddr[MAX_PATH_LEN] = ""; // User defined client path static PwrApiStatus g_status = STATUS_UNREGISTERED; #define CHECK_SOCKET_STATUS() \ @@ -53,10 +55,15 @@ static PwrApiStatus g_status = STATUS_UNREGISTERED; return PWR_ERR_DISCONNECTED; \ } -static char* GetClientSockDir(char *dir) +static char* GetClientSockDir(char *dir, char *cus_dir) { - const char *home = getenv("HOME"); - if (!home || sprintf(dir, "%s/%s", home, CLIENT_ADDR) < 0) { + char *prefix; + if (strlen(cus_dir) > 0) { + prefix = cus_dir; + } else { + prefix = getenv("HOME"); + } + if (!prefix || sprintf(dir, "%s/%s", prefix, CLIENT_ADDR) < 0) { PwrLog(ERROR, "Get Client home dir failed."); } return dir; @@ -299,7 +306,7 @@ static int CreateConnection(void) bzero(&clientAddr, sizeof(clientAddr)); clientAddr.sun_family = AF_UNIX; char sockDir[MAX_PATH_LEN] = CLIENT_ADDR; - strncpy(clientAddr.sun_path, GetClientSockDir(sockDir), sizeof(clientAddr.sun_path) - 1); + strncpy(clientAddr.sun_path, GetClientSockDir(sockDir, cus_clientAddr), sizeof(clientAddr.sun_path) - 1); size_t clen = SUN_LEN(&clientAddr); unlink(clientAddr.sun_path); if (bind(clientFd, (struct sockaddr *)&clientAddr, clen) < 0) { @@ -307,6 +314,11 @@ static int CreateConnection(void) close(clientFd); return PWR_ERR_COMMON; } + mode_t mode = 0400; + if (chmod(clientAddr.sun_path, mode) == -1) { + PwrLog(ERROR, "set permission error"); + return PWR_ERR_SYS_EXCEPTION; + } // connect struct sockaddr_un serverAddr; bzero(&serverAddr, sizeof(serverAddr)); @@ -417,6 +429,15 @@ int SetServerInfo(const char* socketPath) return PWR_SUCCESS; } +int SetClientSockPath(const char* socketPath) +{ + if (access(socketPath, W_OK) != 0) { + return PWR_ERR_PATH_VERIFY; + } + strncpy(cus_clientAddr, socketPath, sizeof(cus_clientAddr) - 1); + return PWR_SUCCESS; +} + int InitSockClient(void) { InitPwrMsgBuffer(&g_sendBuff); diff --git a/pwrapic/test/demo_main.c b/pwrapic/test/demo_main.c index b711cca97537321f28be431d77f6cf5516295fb7..82f64da88b80bafe54cc4922eafa7bfe8763d99a 100644 --- a/pwrapic/test/demo_main.c +++ b/pwrapic/test/demo_main.c @@ -167,6 +167,14 @@ static void TEST_PWR_SetServerInfo(void) PrintResult("PWR_SetServerInfo", ret); } +static void TEST_PWR_SetClientSockPath(void) +{ + int ret = -1; + char str[] = "/opt/pwrapic"; + ret = PWR_SetClientSockPath(str); + PrintResult("PWR_SetClientSockPath", ret); +} + static void TEST_PWR_Register(void) { while (PWR_Register() != PWR_SUCCESS) { @@ -611,6 +619,7 @@ int main(int argc, const char *args[]) { /********** Common **********/ TEST_PWR_SetServerInfo(); + TEST_PWR_SetClientSockPath(); TEST_PWR_SetLogCallback(); TEST_PWR_SetEventCallback(); TEST_PWR_Register();