diff options
Diffstat (limited to 'src/adaptor_util.c')
-rw-r--r-- | src/adaptor_util.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/adaptor_util.c b/src/adaptor_util.c new file mode 100644 index 0000000..8b1e8fb --- /dev/null +++ b/src/adaptor_util.c @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> +#include "sa_common.h" +#include "sa_types.h" +#include "adaptor_util.h" + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> + +#define MAXLINE 1024 +#define LOG_BUF_LEN 2048 +#define SERVER "/var/run/beluga_launcher_notify.sock" + +static int flagConnected = 0; + +int send_message_to_launcher(sa_interface_cmd_e cmd) +{ + int ret = 0; + int sockfd; + int clilen; + struct sockaddr_un serveraddr; + + if (!flagConnected && cmd != SA_START_LAUNCHER_SVC) { + // case of no-valid request to send.. + return -1; + } + + _D("send_message_to_launcher[%d]", cmd); + sockfd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (sockfd < 0) { + _E("socket error : "); + ret = -1; + } else { + bzero(&serveraddr, sizeof(serveraddr)); + serveraddr.sun_family = AF_UNIX; + strncpy(serveraddr.sun_path, SERVER, sizeof(serveraddr.sun_path) - 1); + serveraddr.sun_path[sizeof(serveraddr.sun_path) - 1] = '\0'; + clilen = sizeof(serveraddr); + + _D("send mssage: (%d)", cmd); + do { + if (sendto(sockfd, (void *)&cmd, sizeof(sa_interface_cmd_e), 0, (struct sockaddr *)&serveraddr, clilen) < 0) { + _E("Error sending msg: (%d)", errno); + ret = -2; + sleep(1); + } else { + ret = 0; + } + } while (ret < 0); + + close(sockfd); + + if (cmd == SA_START_LAUNCHER_SVC) + flagConnected = 1; + } + return ret; +} + +/** + * @fn int CLI_command(char *cmd, int fgetOutput, char *ret_buf) + * @brief This function to run command for daemon + * @param *cmd [in] command to run + * @param fgetoutput [in] flag to get ouput of the command + * @param * ret_buf [in] output of the command + * @return int return of function + */ +int CLI_command(char *cmd, int get_output, char **ret_buf) +{ + FILE *stream = NULL; + int ret = 0; + int index = 0; + int ch = 0; + char *buf = NULL; + + if (strlen(cmd) > 0) { + stream = popen(cmd, "r"); + if (stream != NULL) { + switch (get_output) { + case 1: /* to get a specific string */ + _D("get a specific string"); + buf = (char *)malloc(MAXLINE * sizeof(char)); + if (buf != NULL) { + memset(buf, 0x0, MAXLINE * sizeof(char)); + while ((index < MAXLINE) && ((ch = getc(stream)) != EOF) + && (ch != '\n')) { + buf[index++] = ch; + } + //_D("buf:%s(%d)", buf, index); + } + break; + case 2: /* to get logs */ + _D("get logs"); + buf = (char *)malloc(LOG_BUF_LEN * sizeof(char)); + if (buf != NULL) { + memset(buf, 0x0, LOG_BUF_LEN * sizeof(char)); + while ((index < LOG_BUF_LEN) + && ((ch = getc(stream)) != EOF)) { + buf[index++] = ch; + } + //_D("buf:%s(%d)", buf, index); + } + break; + default: + _E("no output message !!"); + break; + } + pclose(stream); + } else { + _E("Error sending msg: (%d)", errno); + ret = -1; + } + } else + ret = -1; + + if (buf) + *ret_buf = buf; + + return ret; +} |