fix build error with tizen6 toolchain(gcc-9)
[platform/core/system/edge-orchestration.git] / CMain / src / main.c
1 /*******************************************************************************
2  * Copyright 2019 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *******************************************************************************/
17
18 #include <stdio.h>
19 #include <gio/gio.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23 #include <string.h>
24
25 #include <orchestration_server.h>
26
27 int get_process_name_by_pid(char* process_name, int pid) {
28         char fname[1024] = {0, };
29         char line[1024] = {0, };
30         ssize_t fname_len = snprintf(NULL, 0, "/proc/%d/status", pid);
31
32         snprintf(fname, fname_len + 1, "/proc/%d/status", pid);
33         FILE *fp = fopen(fname, "r");
34         if (fp == NULL) {
35                 DEBUG("%d Process not found!!!\n", pid);
36                 return -1;
37         }
38         while (!feof(fp)) {
39                 fgets(line, 1024, fp);
40                 if (strstr(line, "Name:") != NULL) {
41                         int index = 0;
42                         char *ptr = strchr(line, ':');
43                         while (1) {
44                                 ptr++;
45                                 if (*ptr == '\n' || *ptr == '\0')
46                                         break;
47                                 if (*ptr != ' ' && *ptr != '\t')
48                                         process_name[index++] = *ptr;
49                         }
50                         process_name[index] = '\0';
51                         printf("process_name = %s\n", process_name);
52                         fclose(fp);
53                         return 0;
54                 }
55         }
56         printf("%d Process status have no Process name!!\n", pid);
57         fclose(fp);
58         return -1;
59 }
60
61 int request_cb(char* app_name, bool self_select, RequestServiceInfo service_info[], int count, int client_pid){
62         int ret = ORCH_ERROR_NONE;
63         char process_name[128] = {0, };
64         const char unknown[] = "Unknown";
65         ResponseService result;
66
67         if (get_process_name_by_pid(process_name, client_pid) != 0) {
68                 DEBUG("could not get Process name");
69                 strncpy(process_name, unknown, strlen(unknown));
70         }
71
72         DEBUG("[orchestration_server]\n")
73         DEBUG("\t app_name : %s\n", app_name);
74         DEBUG("\t self_select : %s\n", self_select ? "true" : "false");
75         DEBUG("\t count : %d\n", count);
76         DEBUG("\t client_pid : %d\n", client_pid);
77         DEBUG("\t process_name : %s\n", process_name);
78         for (int ix = 0; ix < count; ix++) {
79                 DEBUG("\t service_info[%d].ExecutionType : %s\n", ix, service_info[ix].ExecutionType);
80                 DEBUG("\t service_info[%d].ExeCmd : %s\n", ix, service_info[ix].ExeCmd);
81         }
82
83         result = OrchestrationRequestService(app_name, self_select, process_name, service_info, count);
84         DEBUG("result = %s\n", result.Message);
85
86         /* TO DO : parsing API response */
87         return ret;
88 }
89
90 void* thread_orchestration() {
91         DEBUG("Start OrchestrationInit Thread !!\n");
92         OrchestrationInit();
93         return NULL;
94 }
95
96 int main(void){
97         int result;
98         pthread_t p_thread;
99
100         GMainLoop *loop;
101
102         /* DEBUG for log can be used after OrchestrationInit */
103         if (pthread_create(&p_thread, NULL, thread_orchestration, NULL) != 0) {
104                 DEBUG("Fail to start OrchestrationInit Thread !!");
105                 return -1;
106         }
107
108         set_default_dbus_interface();
109         DEBUG("orchestration_server_initialize call\n");
110         result = orchestration_server_initialize(request_cb);
111         if(result != ORCH_ERROR_NONE){
112                 DEBUG("orchestration_server_initialize failed\n");
113                 orchestration_server_finish();
114         }
115
116         loop = g_main_loop_new(NULL, FALSE);
117         if (!loop)
118                 return -1;
119
120         g_main_loop_run (loop);
121         orchestration_server_finish();
122
123
124         return 0;
125 }
126
127 void printLog(const char *format, ...) {
128         int buf_size;
129         char *buffer = NULL;
130         va_list args;
131
132         va_start(args, format);
133         buf_size = vsnprintf(0, 0, format, args);
134         va_end(args);
135
136         buffer = malloc(buf_size + 1);
137         if(!buffer) return;
138
139         va_start(args, format);
140         vsnprintf(buffer, buf_size + 1, format, args);
141         PrintLog(buffer);
142         if (buffer != NULL) free(buffer);
143         va_end(args);
144 }