Add new internal functions to kill loader process
[platform/core/appfw/aul-1.git] / src / launch.cc
1 /*
2  * Copyright (c) 2000 - 2021 Samsung Electronics Co., Ltd 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 #include <bundle_internal.h>
18 #include <ctype.h>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <gio/gio.h>
22 #include <glib-unix.h>
23 #include <glib.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <ttrace.h>
31
32 #include <filesystem>
33 #include <memory>
34 #include <string>
35 #include <utility>
36
37 #include "app_request.h"
38 #include "app_signal.h"
39 #include "aul.h"
40 #include "aul_api.h"
41 #include "aul_app_com.h"
42 #include "aul_error.h"
43 #include "aul_sock.h"
44 #include "aul_util.h"
45 #include "launch.h"
46
47 using namespace aul::internal;
48
49 namespace {
50
51 constexpr const char kRunAulDaemonsPath[] = "/run/aul/daemons/";
52 constexpr const char kLaunchpadProcessPoolSock[] =
53     ".launchpad-process-pool-sock";
54 const int kPadCmdKillLoader = 19;
55 constexpr const int TEP_ISMOUNT_MAX_RETRY_CNT = 20;
56
57 class ResultInfo {
58  public:
59   ResultInfo(int fd, aul_result_cb callback, void* user_data)
60       : fd_(fd), callback_(callback), user_data_(user_data) {}
61
62   ResultInfo(const ResultInfo&) = delete;
63   ResultInfo& operator = (const ResultInfo&) = delete;
64
65   ~ResultInfo() {
66     if (source_ != 0)
67       g_source_remove(source_);
68
69     if (fd_ > -1)
70       close(fd_);
71   }
72
73   bool Watch() {
74     source_ = g_unix_fd_add(fd_,
75         static_cast<GIOCondition>(G_IO_IN | G_IO_HUP | G_IO_ERR),
76         FdSourceFunc, this);
77     if (source_ == 0) {
78       _E("g_unix_fd_add() is failed");
79       return false;
80     }
81
82     return true;
83   }
84
85  private:
86   void ProcessReadEvent() {
87     int res = aul_sock_recv_result_with_fd(fd_);
88     if (res < 0)
89       res = aul_error_convert(res);
90     else
91       res = AUL_R_OK;
92
93     callback_(res, user_data_);
94   }
95
96   void ProcessErrorEvent() {
97     callback_(AUL_R_ERROR, user_data_);
98   }
99
100   static gboolean FdSourceFunc(int fd, GIOCondition condition,
101       void* user_data) {
102     auto* info = static_cast<ResultInfo*>(user_data);
103     if (condition & G_IO_IN)
104       info->ProcessReadEvent();
105     else
106       info->ProcessErrorEvent();
107
108     info->source_ = 0;
109     delete info;
110     return G_SOURCE_REMOVE;
111   }
112
113  private:
114   int fd_;
115   aul_result_cb callback_;
116   void *user_data_;
117   guint source_ = 0;
118 };
119
120 int aul_initialized = 0;
121 int aul_fd;
122 void* window_object = nullptr;
123 void* bg_object = nullptr;
124 void* conformant_object = nullptr;
125
126 int SendCmdForUidOpt(int pid, uid_t uid, int cmd, bundle* kb, int opt) {
127   int res = aul_sock_send_bundle(pid, uid, cmd, kb, opt);
128   if (res < 0)
129     res = aul_error_convert(res);
130
131   return res;
132 }
133
134 int SendCmdNoReplyForUidOpt(int pid, uid_t uid, int cmd, bundle* kb, int opt) {
135   int res = aul_sock_send_bundle(pid, uid, cmd, kb, opt | AUL_SOCK_NOREPLY);
136   if (res < 0)
137     res = aul_error_convert(res);
138
139   return res;
140 }
141
142 int CheckEnvPid() {
143   const char* env_str = getenv("AUL_PID");
144   if (env_str && isdigit(*env_str)) {
145     int env_pid = atoi(env_str);
146     if (env_pid != getpid()) {
147       _W("pid(%d) is not equal to AUL_PID(%d)",
148           getpid(), env_pid);
149       return -1;
150     }
151   }
152
153   return 0;
154 }
155
156 int GetPreInitFd() {
157   int fd = -1;
158
159   if (CheckEnvPid() != 0)
160     return fd;
161
162   const char* listen_fd = getenv("AUL_LISTEN_FD");
163   if (listen_fd) {
164     if (isdigit(*listen_fd))
165       fd = atoi(listen_fd);
166     setenv("AUL_LISTEN_FD", "-1", 1);
167   }
168
169   return fd;
170 }
171
172 }  // namespace
173
174 extern "C" int aul_is_initialized() {
175   return aul_initialized;
176 }
177
178 extern "C" API int app_send_cmd(int pid, int cmd, bundle* kb) {
179   return SendCmdForUidOpt(pid, getuid(), cmd, kb, AUL_SOCK_NONE);
180 }
181
182 extern "C" API int app_send_cmd_for_uid(int pid, uid_t uid, int cmd,
183     bundle* kb) {
184   return SendCmdForUidOpt(pid, uid, cmd, kb, AUL_SOCK_NONE);
185 }
186
187 extern "C" API int app_send_cmd_with_queue_for_uid(int pid, uid_t uid, int cmd,
188     bundle* kb) {
189   return SendCmdForUidOpt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
190 }
191
192 extern "C" API int app_send_cmd_with_queue_noreply_for_uid(int pid, uid_t uid,
193     int cmd, bundle* kb) {
194   return SendCmdNoReplyForUidOpt(pid, uid, cmd, kb, AUL_SOCK_QUEUE);
195 }
196
197 extern "C" API int app_send_cmd_with_noreply(int pid, int cmd, bundle* kb) {
198   return SendCmdForUidOpt(pid, getuid(), cmd, kb, AUL_SOCK_NOREPLY);
199 }
200
201 extern "C" API int app_send_cmd_to_launchpad(const char* pad_type, uid_t uid,
202     int cmd, bundle* kb) {
203   int fd = aul_sock_create_launchpad_client(pad_type, uid);
204   if (fd < 0)
205     return -1;
206
207   int res = aul_sock_send_bundle_with_fd(fd, cmd, kb, AUL_SOCK_ASYNC);
208   if (res < 0) {
209     close(fd);
210     return res;
211   }
212
213 retry_recv:
214   int len = recv(fd, &res, sizeof(int), 0);
215   if (len == -1) {
216     if (errno == EAGAIN) {
217       char buf[1024];
218       _E("recv timeout: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
219       res = -EAGAIN;
220     } else if (errno == EINTR) {
221       char buf[1024];
222       _D("recv: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
223       goto retry_recv;
224     } else {
225       char buf[1024];
226       _E("recv error: %d(%s)", errno, strerror_r(errno, buf, sizeof(buf)));
227       res = -ECOMM;
228     }
229   }
230
231   close(fd);
232   return res;
233 }
234
235 extern "C" int app_request_local(int cmd, bundle* kb) {
236   _E("app_request_to_launchpad : Same Process Send Local");
237   switch (cmd) {
238   case APP_START:
239   case APP_START_RES:
240   case APP_START_ASYNC:
241   case WIDGET_UPDATE:
242   case APP_START_RES_ASYNC:
243   case APP_SEND_LAUNCH_REQUEST:
244     return aul_launch_local(bundle_dup(kb));
245   case APP_OPEN:
246   case APP_RESUME:
247   case APP_RESUME_BY_PID:
248   case APP_RESUME_BY_PID_ASYNC:
249   case APP_SEND_RESUME_REQUEST:
250     return aul_resume_local();
251   default:
252     _E("no support packet");
253     return AUL_R_LOCAL;
254   }
255 }
256
257 extern "C" int app_request_to_launchpad_for_uid(int cmd, const char* appid,
258     bundle* kb, uid_t uid) {
259   return AppRequest(cmd, uid)
260       .With(kb)
261       .SetAppId(appid)
262       .Send();
263 }
264
265 extern "C" int aul_initialize() {
266   if (aul_initialized)
267     return AUL_R_ECANCELED;
268
269   aul_fd = GetPreInitFd();
270   if (aul_fd > 0 && aul_fd < sysconf(_SC_OPEN_MAX)) {
271     int flag = fcntl(aul_fd, F_GETFD);
272     flag |= FD_CLOEXEC;
273     (void)fcntl(aul_fd, F_SETFD, flag);
274   } else {
275     _W("Failed to get preinit fd");
276     aul_fd = aul_sock_create_server(getpid(), getuid());
277     if (aul_fd < 0) {
278       _E("aul_init create sock failed");
279       return AUL_R_ECOMM;
280     }
281   }
282   aul_notify_start();
283
284   aul_initialized = 1;
285   return aul_fd;
286 }
287
288 extern "C" API void aul_finalize() {
289   aul_launch_fini();
290
291   if (aul_initialized) {
292     aul_sock_destroy_server(aul_fd);
293     aul_fd = -1;
294   }
295 }
296
297 extern "C" API int aul_request_data_control_socket_pair(bundle* kb, int* fd) {
298   if (fd == nullptr)
299     return AUL_R_EINVAL;
300
301   int clifd = AppRequest(APP_GET_DC_SOCKET_PAIR)
302       .With(kb)
303       .SendSimply(AUL_SOCK_ASYNC);
304   if (clifd <= 0)
305     return AUL_R_ERROR;
306
307   int ret = aul_sock_recv_result_with_fd(clifd);
308   if (ret < 0) {
309     close(clifd);
310     if (ret == -EILLEGALACCESS) {
311       _E("Illegal access in datacontrol socket pair request");
312       return AUL_R_EILLACC;
313     }
314     return ret;
315   }
316
317   int fds[2] = { 0, };
318   ret = aul_sock_recv_reply_sock_fd(clifd, &fds, 1);
319   if (ret == 0)
320     fd[0] = fds[0];
321
322   return ret;
323 }
324
325 extern "C" API int aul_request_message_port_socket_pair(int* fd) {
326   if (fd == nullptr)
327     return AUL_R_EINVAL;
328
329   int ret = AppRequest(APP_GET_MP_SOCKET_PAIR)
330       .SendCmdOnly(AUL_SOCK_ASYNC);
331   if (ret > 0) {
332     int fds[2] = {0, };
333     ret = aul_sock_recv_reply_sock_fd(ret, &fds, 2);
334     if (ret == 0) {
335       fd[0] = fds[0];
336       fd[1] = fds[1];
337     }
338   }
339
340   return ret;
341 }
342
343 extern "C" API int aul_launch_app(const char* appid, bundle* kb) {
344   return aul_launch_app_for_uid(appid, kb, getuid());
345 }
346
347 extern "C" API int aul_launch_app_for_uid(const char* appid, bundle* kb,
348     uid_t uid) {
349   if (appid == nullptr)
350     return AUL_R_EINVAL;
351
352   return AppRequest(APP_START, uid)
353       .With(kb)
354       .SetAppId(appid)
355       .Send();
356 }
357
358 extern "C" API int aul_open_app(const char* appid) {
359   return aul_open_app_for_uid(appid, getuid());
360 }
361
362 extern "C" API int aul_open_app_for_uid(const char* appid, uid_t uid) {
363   if (appid == nullptr)
364     return AUL_R_EINVAL;
365
366   return AppRequest(APP_OPEN, uid)
367       .SetAppId(appid)
368       .Send();
369 }
370
371 extern "C" API int aul_resume_app(const char* appid) {
372   return aul_resume_app_for_uid(appid, getuid());
373 }
374
375 extern "C" API int aul_resume_app_for_uid(const char* appid, uid_t uid) {
376   if (appid == nullptr)
377     return AUL_R_EINVAL;
378
379   return AppRequest(APP_RESUME, uid)
380       .SetAppId(appid)
381       .Send();
382 }
383
384 extern "C" API int aul_resume_pid(int pid) {
385   return aul_resume_pid_for_uid(pid, getuid());
386 }
387
388 extern "C" API int aul_resume_pid_for_uid(int pid, uid_t uid) {
389   if (pid <= 0)
390     return AUL_R_EINVAL;
391
392   return AppRequest(APP_RESUME_BY_PID, uid)
393       .SetAppIdAsPid(pid)
394       .Send();
395 }
396
397 extern "C" API int aul_terminate_pid(int pid) {
398   return aul_terminate_pid_for_uid(pid, getuid());
399 }
400
401 extern "C" API int aul_terminate_pid_for_uid(int pid, uid_t uid) {
402   if (pid <= 0)
403     return AUL_R_EINVAL;
404
405   int ret = AppRequest(APP_TERM_BY_PID, uid)
406       .SetAppIdAsPid(pid)
407       .Send();
408   if (ret == pid)
409     ret = AUL_R_OK;
410
411   return ret;
412 }
413
414 extern "C" API int aul_terminate_bgapp_pid(int pid) {
415   if (pid <= 0)
416     return AUL_R_EINVAL;
417
418   int ret = AppRequest(APP_TERM_BGAPP_BY_PID)
419       .SetAppIdAsPid(pid)
420       .Send();
421   if (ret == pid)
422     ret = AUL_R_OK;
423
424   return ret;
425 }
426
427 extern "C" API int aul_terminate_pid_without_restart(int pid) {
428   if (pid <= 0)
429     return AUL_R_EINVAL;
430
431   return AppRequest(APP_TERM_BY_PID_WITHOUT_RESTART)
432       .SetAppIdAsPid(pid)
433       .Send();
434 }
435
436 extern "C" API int aul_terminate_pid_sync_without_restart(int pid) {
437   return aul_terminate_pid_sync_without_restart_for_uid(pid, getuid());
438 }
439
440 extern "C" API int aul_terminate_pid_sync_without_restart_for_uid(int pid,
441     uid_t uid) {
442   if (pid <= 0)
443     return AUL_R_EINVAL;
444
445   return AppRequest(APP_TERM_BY_PID_SYNC_WITHOUT_RESTART, uid)
446       .SetAppIdAsPid(pid)
447       .Send();
448 }
449
450 extern "C" API int aul_terminate_pid_async(int pid) {
451   return aul_terminate_pid_async_for_uid(pid, getuid());
452 }
453
454 extern "C" API int aul_terminate_pid_async_for_uid(int pid, uid_t uid) {
455   if (pid <= 0)
456     return AUL_R_EINVAL;
457
458   return AppRequest(APP_TERM_BY_PID_ASYNC, uid)
459       .SetAppIdAsPid(pid)
460       .Send();
461 }
462
463 extern "C" API int aul_kill_pid(int pid) {
464   if (pid <= 0)
465     return AUL_R_EINVAL;
466
467   return AppRequest(APP_KILL_BY_PID)
468       .SetAppIdAsPid(pid)
469       .Send();
470 }
471
472 extern "C" API void aul_set_preinit_window(void* evas_object) {
473   window_object = evas_object;
474 }
475
476 extern "C" API void* aul_get_preinit_window(const char* win_name) {
477   return window_object;
478 }
479
480 extern "C" API void aul_set_preinit_background(void* evas_object) {
481   bg_object = evas_object;
482 }
483
484 extern "C" API void* aul_get_preinit_background(void) {
485   return bg_object;
486 }
487
488 extern "C" API void aul_set_preinit_conformant(void* evas_object) {
489   conformant_object = evas_object;
490 }
491
492 extern "C" API void* aul_get_preinit_conformant(void) {
493   return conformant_object;
494 }
495
496 extern "C" API int aul_pause_app(const char* appid) {
497   return aul_pause_app_for_uid(appid, getuid());
498 }
499
500 extern "C" API int aul_pause_app_for_uid(const char* appid, uid_t uid) {
501   if (appid == nullptr)
502     return AUL_R_EINVAL;
503
504   return AppRequest(APP_PAUSE, uid)
505       .SetAppId(appid)
506       .Send(AUL_SOCK_QUEUE | AUL_SOCK_NOREPLY);
507 }
508
509 extern "C" API int aul_pause_pid(int pid) {
510   return aul_pause_pid_for_uid(pid, getuid());
511 }
512
513 extern "C" API int aul_pause_pid_for_uid(int pid, uid_t uid) {
514   if (pid <= 0)
515     return AUL_R_EINVAL;
516
517   return AppRequest(APP_PAUSE_BY_PID, uid)
518       .SetAppIdAsPid(pid)
519       .Send(AUL_SOCK_QUEUE | AUL_SOCK_NOREPLY);
520 }
521
522 extern "C" API int aul_reload_appinfo(void) {
523   return AppRequest(AMD_RELOAD_APPINFO)
524       .SetAppIdAsPid(getpid())
525       .Send();
526 }
527
528 extern "C" API int aul_is_tep_mount_dbus_done(const char* tep_string) {
529   GError* err = nullptr;
530   GDBusConnection* conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &err);
531   if (conn == nullptr) {
532     _E("g_bus_get_sync() is failed. error(%s)", err ? err->message : "Unknown");
533     g_clear_error(&err);
534     return AUL_R_ERROR;
535   }
536
537   std::unique_ptr<GDBusConnection, decltype(g_object_unref)*> conn_ptr(
538       conn, g_object_unref);
539
540   GDBusMessage* msg = g_dbus_message_new_method_call(TEP_BUS_NAME,
541           TEP_OBJECT_PATH,
542           TEP_INTERFACE_NAME,
543           TEP_IS_MOUNTED_METHOD);
544   if (msg == nullptr) {
545     _E("g_dbus_message_new_method_call() is failed. error(%s)",
546         err ? err->message : "Unknown");
547     return AUL_R_ERROR;
548   }
549
550   std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> msg_ptr(
551       msg, g_object_unref);
552   g_dbus_message_set_body(msg, g_variant_new("(s)", tep_string));
553
554   GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(conn,
555           msg,
556           G_DBUS_SEND_MESSAGE_FLAGS_NONE,
557           500,
558           nullptr,
559           nullptr,
560           &err);
561   if (reply == nullptr || err != nullptr) {
562     _E("g_dbus_connection_send_message_with_reply_sync() is failed. error(%s)",
563         err ? err->message : "Unknown");
564     g_clear_error(&err);
565     return AUL_R_ERROR;
566   }
567
568   std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> reply_ptr(
569       reply, g_object_unref);
570
571   GVariant* body = g_dbus_message_get_body(reply);
572   if (body == nullptr) {
573     _E("g_dbus_message_get_body() is failed.");
574     return AUL_R_ERROR;
575   }
576
577   int ret = AUL_R_ERROR;
578   g_variant_get(body, "(i)", &ret);
579   return ret;
580 }
581
582 extern "C" API int aul_check_tep_mount(const char* tep_path) {
583   if (tep_path) {
584     int ret = -1;
585     int cnt = 0;
586     while (cnt < TEP_ISMOUNT_MAX_RETRY_CNT) {
587       ret = aul_is_tep_mount_dbus_done(tep_path);
588       if (ret == 1)
589         break;
590       usleep(50 * 1000);
591       cnt++;
592     }
593     /* incase after trying 1 sec, not getting mounted then quit */
594     if (ret != 1) {
595       _E("Not able to mount within 1 sec");
596       return -1;
597     }
598   }
599   return 0;
600 }
601
602 extern "C" API int aul_add_loader(const char* loader_path, bundle* kb) {
603   return aul_add_loader_for_uid(loader_path, kb, getuid());
604 }
605
606 extern "C" API int aul_add_loader_for_uid(const char* loader_path, bundle* kb,
607     uid_t uid) {
608   if (loader_path == nullptr)
609     return AUL_R_EINVAL;
610
611   tizen_base::Bundle b = { {AUL_K_LOADER_PATH, loader_path} };
612
613   if (kb) {
614     try {
615       tizen_base::Bundle extra(kb, false, false);
616       auto raw = extra.ToRaw();
617       b.Add(AUL_K_LOADER_EXTRA, (const char*)(raw.first.get()));
618     } catch (const std::bad_alloc&) {
619       return AUL_R_EINVAL;
620     }
621   }
622
623   return AppRequest(APP_ADD_LOADER, uid)
624       .With(std::move(b))
625       .SendSimply();
626 }
627
628 extern "C" API int aul_remove_loader(int loader_id) {
629   return aul_remove_loader_for_uid(loader_id, getuid());
630 }
631
632 extern "C" API int aul_remove_loader_for_uid(int loader_id, uid_t uid) {
633   if (loader_id <= 0)
634     return AUL_R_EINVAL;
635
636   return AppRequest(APP_REMOVE_LOADER, uid)
637       .With({{AUL_K_LOADER_ID, std::to_string(loader_id)}})
638       .SendSimply();
639 }
640
641 extern "C" API int aul_app_register_pid(const char* appid, int pid) {
642   if (appid == nullptr || pid <= 0)
643     return AUL_R_EINVAL;
644
645   return AppRequest(APP_REGISTER_PID)
646       .SetAppId(appid)
647       .SetPid(pid)
648       .SendSimply(AUL_SOCK_NOREPLY);
649 }
650
651 extern "C" API int aul_launch_app_async(const char* appid, bundle* kb) {
652   return aul_launch_app_async_for_uid(appid, kb, getuid());
653 }
654
655 extern "C" API int aul_launch_app_async_for_uid(const char* appid, bundle* kb,
656     uid_t uid) {
657   if (appid == nullptr)
658     return AUL_R_EINVAL;
659
660   return AppRequest(APP_START_ASYNC, uid)
661       .With(kb)
662       .SetAppId(appid)
663       .Send();
664 }
665
666 extern "C" API int aul_prepare_candidate_process(void) {
667   return AppRequest(APP_PREPARE_CANDIDATE_PROCESS)
668       .SendCmdOnly();
669 }
670
671 extern "C" API int aul_terminate_pid_sync(int pid) {
672   return aul_terminate_pid_sync_for_uid(pid, getuid());
673 }
674
675 extern "C" API int aul_terminate_pid_sync_for_uid(int pid, uid_t uid) {
676   if (pid <= 0)
677     return AUL_R_EINVAL;
678
679   return AppRequest(APP_TERM_BY_PID_SYNC, uid)
680       .SetAppIdAsPid(pid)
681       .Send();
682 }
683
684 extern "C" API int aul_resume_pid_async(int pid) {
685   return aul_resume_pid_async_for_uid(pid, getuid());
686 }
687
688 extern "C" API int aul_resume_pid_async_for_uid(int pid, uid_t uid) {
689   if (pid <= 0)
690     return AUL_R_EINVAL;
691
692   return AppRequest(APP_RESUME_BY_PID_ASYNC, uid)
693       .SetAppIdAsPid(pid)
694       .Send();
695 }
696
697 extern "C" API int aul_resume_app_by_instance_id(const char* appid,
698     const char *instance_id) {
699   return aul_resume_app_by_instance_id_for_uid(appid, instance_id, getuid());
700 }
701
702 extern "C" API int aul_resume_app_by_instance_id_for_uid(const char* appid,
703     const char* instance_id, uid_t uid) {
704   if (appid == nullptr || instance_id == nullptr) {
705     _E("Invalid parameter");
706     return AUL_R_EINVAL;
707   }
708
709   return AppRequest(APP_RESUME, uid)
710       .SetAppId(appid)
711       .SetInstId(instance_id)
712       .Send();
713 }
714
715 extern "C" API int aul_terminate_instance_async(const char* instance_id,
716     int pid) {
717   return aul_terminate_instance_async_for_uid(instance_id, pid, getuid());
718 }
719
720 extern "C" API int aul_terminate_instance_async_for_uid(const char* instance_id,
721     int pid, uid_t uid) {
722   if (instance_id == nullptr) {
723     _E("Invalid parameter");
724     return AUL_R_EINVAL;
725   }
726
727   return AppRequest(APP_TERM_INSTANCE_ASYNC, uid)
728       .SetAppIdAsPid(pid)
729       .SetInstId(instance_id)
730       .Send();
731 }
732
733 extern "C" API int aul_terminate_app_with_instance_id(const char* appid,
734     const char* instance_id) {
735   return aul_terminate_app_with_instance_id_for_uid(appid, instance_id,
736       getuid());
737 }
738
739 extern "C" API int aul_terminate_app_with_instance_id_for_uid(const char* appid,
740     const char* instance_id, uid_t uid) {
741   if (appid == nullptr || instance_id == nullptr) {
742     _E("Invalid parameter");
743     return AUL_R_EINVAL;
744   }
745
746   return AppRequest(APP_TERMINATE, uid)
747       .SetAppId(appid)
748       .SetInstId(instance_id)
749       .Send();
750 }
751
752 extern "C" API int aul_terminate_app(const char* appid) {
753   return aul_terminate_app_for_uid(appid, getuid());
754 }
755
756 extern "C" API int aul_terminate_app_for_uid(const char* appid, uid_t uid) {
757   if (appid == nullptr) {
758     _E("Invalid parameter");
759     return AUL_R_EINVAL;
760   }
761
762   return AppRequest(APP_TERMINATE, uid)
763       .SetAppId(appid)
764       .Send();
765 }
766
767 extern "C" API int aul_prepare_app_defined_loader(const char* loader_name) {
768   return aul_prepare_app_defined_loader_for_uid(loader_name, getuid());
769 }
770
771 extern "C" API int aul_prepare_app_defined_loader_for_uid(
772     const char* loader_name, uid_t uid) {
773   if (loader_name == nullptr) {
774     _E("Invalid parameter");
775     return AUL_R_EINVAL;
776   }
777
778   int ret = AppRequest(APP_PREPARE_APP_DEFINED_LOADER, uid)
779       .With({{AUL_K_LOADER_NAME, loader_name}})
780       .SendSimply();
781   if (ret < 0) {
782     _E("Failed to prepare app-defined loader. error(%d)", ret);
783     return ret;
784   }
785
786   _I("loader id(%d)", ret);
787   return ret;
788 }
789
790 extern "C" API int aul_terminate_pid_async_v2(pid_t pid,
791     aul_result_cb callback, void *user_data) {
792   if (pid < 1 || callback == nullptr) {
793     _E("Invalid parameter");
794     return AUL_R_EINVAL;
795   }
796
797   int fd = AppRequest(APP_TERM_BY_PID, getuid())
798       .SetAppIdAsPid(pid)
799       .SendSimply(AUL_SOCK_ASYNC);
800   if (fd < 0) {
801     _E("Failed to send request. error(%d)", fd);
802     return fd;
803   }
804
805   try {
806     auto* info = new ResultInfo(fd, callback, user_data);
807     info->Watch();
808   } catch (const std::exception& e) {
809     _E("Exception occurs. error: %s", e.what());
810     close(fd);
811     return AUL_R_ERROR;
812   }
813
814   return AUL_R_OK;
815 }
816
817 extern "C" API int aul_terminate_app_async(const char* appid,
818     aul_result_cb callback, void *user_data) {
819   if (appid == nullptr || callback == nullptr) {
820     _E("Invalid parameter");
821     return AUL_R_EINVAL;
822   }
823
824   int fd = AppRequest(APP_TERMINATE, getuid())
825       .SetAppId(appid)
826       .SendSimply(AUL_SOCK_ASYNC);
827   if (fd < 0) {
828     _E("Failed to send request. error(%d)", fd);
829     return fd;
830   }
831
832   try {
833     auto* info = new ResultInfo(fd, callback, user_data);
834     info->Watch();
835   } catch (const std::exception& e) {
836     _E("Exception occurs. error: %s", e.what());
837     close(fd);
838     return AUL_R_ERROR;
839   }
840
841   return AUL_R_OK;
842 }
843
844 extern "C" API int aul_kill_pid_async(pid_t pid,
845     aul_result_cb callback, void *user_data) {
846   if (pid < 1 || callback == nullptr) {
847     _E("Invalid parameter");
848     return AUL_R_EINVAL;
849   }
850
851   int fd = AppRequest(APP_KILL_BY_PID, getuid())
852       .SetAppIdAsPid(pid)
853       .SendSimply(AUL_SOCK_ASYNC);
854   if (fd < 0) {
855     _E("Failed to send request. error(%d)", fd);
856     return fd;
857   }
858
859   try {
860     auto* info = new ResultInfo(fd, callback, user_data);
861     info->Watch();
862   } catch (const std::exception& e) {
863     _E("Exception occurs. error: %s", e.what());
864     close(fd);
865     return AUL_R_ERROR;
866   }
867
868   return AUL_R_OK;
869 }
870
871 extern "C" API int aul_kill_loader(const char* loader_name) {
872   return aul_kill_loader_for_uid(loader_name, getuid());
873 }
874
875 extern "C" API int aul_kill_loader_for_uid(const char* loader_name,
876     uid_t uid) {
877   if (loader_name == nullptr) {
878     _E("Invalid parameter");
879     return AUL_R_EINVAL;
880   }
881
882   if (uid < REGULAR_UID_MIN) {
883     uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
884     _W("Use system default user ID(%u)", uid);
885   }
886
887   std::string endpoint = kRunAulDaemonsPath + std::to_string(uid) + "/" +
888                          std::string(kLaunchpadProcessPoolSock);
889   if (!std::filesystem::exists(endpoint)) {
890     _E("launchpad socket is not prepared");
891     return AUL_R_ENOENT;
892   }
893
894   int fd = aul_sock_create_launchpad_client(kLaunchpadProcessPoolSock, uid);
895   if (fd < 0) {
896     _E("Failed to create launchpad client socket. error(%d)", fd);
897     return aul_error_convert(fd);
898   }
899
900   tizen_base::Bundle b = { {AUL_K_LOADER_NAME, loader_name} };
901   int ret = aul_sock_send_bundle_with_fd(fd, kPadCmdKillLoader, b.GetHandle(),
902       AUL_SOCK_NOREPLY);
903   if (ret != AUL_R_OK)
904     return aul_error_convert(ret);
905
906   return AUL_R_OK;
907 }