summaryrefslogtreecommitdiff
path: root/src/adaptor_util.c
blob: 8b1e8fb7c90e036b9837441c2cce745d12f4bfbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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;
}