Move function definition to aul header
[platform/core/appfw/aul-1.git] / src / pkginfo.cc
1 /*
2  * Copyright (c) 2012 - 2022 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_cpp.h>
18 #include <bundle_internal.h>
19 #include <glib-unix.h>
20 #include <glib.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include <atomic>
26 #include <exception>
27 #include <memory>
28 #include <string>
29
30 #include "app_request.h"
31 #include "aul_api.h"
32 #include "aul_error.h"
33 #include "aul_util.h"
34 #include "menu_db_util.h"
35 #include "include/aul.h"
36 #include "include/aul_error.h"
37 #include "include/aul_sock.h"
38
39 using namespace aul::internal;
40
41 namespace {
42
43 constexpr const char* kAppInfoKeys[] = {
44   AUL_K_PID,
45   AUL_K_APPID,
46   AUL_K_EXEC,
47   AUL_K_PKGID,
48   AUL_K_STATUS,
49   AUL_K_IS_SUBAPP,
50 };
51
52 constexpr const char kPathAmdReady[] = "/run/.amd_ready";
53
54 class ResultInfo {
55  public:
56   ResultInfo(pid_t pid, int fd, aul_appid_cb callback, void* user_data)
57       : pid_(pid), fd_(fd), callback_(callback), user_data_(user_data) {}
58
59   ResultInfo(const ResultInfo&) = delete;
60   ResultInfo& operator = (const ResultInfo&) = delete;
61
62   ~ResultInfo() {
63     if (source_ != 0)
64       g_source_remove(source_);
65
66     if (fd_ > -1)
67       close(fd_);
68   }
69
70   bool Watch() {
71     source_ = g_unix_fd_add(fd_,
72         static_cast<GIOCondition>(G_IO_IN | G_IO_HUP | G_IO_ERR),
73         FdSourceFunc, this);
74     if (source_ == 0) {
75       _E("g_unix_fd_add() is failed");
76       return false;
77     }
78
79     return true;
80   }
81
82  private:
83   void ProcessReadEvent() {
84     app_pkt_t* pkt = nullptr;
85     int ret = aul_sock_recv_reply_pkt(fd_, &pkt);
86     if (ret < 0 || pkt == nullptr) {
87       ProcessErrorEvent();
88       return;
89     }
90
91     auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
92         pkt, std::free);
93     if (pkt->cmd != APP_GET_INFO_OK) {
94       ProcessErrorEvent();
95       return;
96     }
97
98     char appid[256] = { 0, };
99     snprintf(appid, sizeof(appid), "%s", pkt->data);
100     SECURE_LOGD("pid: %d, appid: %s", pid_, appid);
101     callback_(AUL_R_OK, pid_, appid, user_data_);
102   }
103
104   void ProcessErrorEvent() {
105     callback_(AUL_R_ERROR, pid_, "", user_data_);
106   }
107
108   static gboolean FdSourceFunc(int fd, GIOCondition condition,
109       gpointer user_data) {
110     auto* info = static_cast<ResultInfo*>(user_data);
111     _E("GIOCondition: %d", static_cast<int>(condition));
112     if (condition & G_IO_IN)
113       info->ProcessReadEvent();
114     else
115       info->ProcessErrorEvent();
116
117     info->source_ = 0;
118     delete info;
119     return G_SOURCE_REMOVE;
120   }
121
122  private:
123   pid_t pid_;
124   int fd_;
125   aul_appid_cb callback_;
126   void* user_data_;
127   guint source_ = 0;
128 };
129
130 bool IsAmdReady() {
131   static std::atomic<bool> amd_ready = false;
132   if (amd_ready)
133     return amd_ready;
134
135   if (access(kPathAmdReady, F_OK) == 0) {
136     amd_ready.exchange(true);
137     return amd_ready;
138   }
139
140   return false;
141 }
142
143 class Context {
144  public:
145   Context() = default;
146
147   void Initialize() {
148     if (initialized_)
149       return;
150
151     auto* appid = getenv("AUL_APPID");
152     if (appid != nullptr)
153       appid_ = std::string(appid);
154
155     auto* pkgid = getenv("AUL_PKGID");
156     if (pkgid != nullptr)
157       pkgid_ = std::string(pkgid);
158
159     auto* root_path = getenv("AUL_ROOT_PATH");
160     if (root_path != nullptr)
161       root_path_ = std::string(root_path);
162
163     initialized_ = true;
164   }
165
166   const std::string& GetPreInitAppId() const {
167     return appid_;
168   }
169
170   const std::string& GetPreInitPkgId() const {
171     return pkgid_;
172   }
173
174   const std::string& GetPreInitRootPath() const {
175     return root_path_;
176   }
177
178   void SetPreInitAppId(std::string appid) {
179     appid_ = std::move(appid);
180   }
181
182   void SetPreInitPkgId(std::string pkgid) {
183     pkgid_ = std::move(pkgid);
184   }
185
186   void SetPreInitRootPath(std::string root_path) {
187     root_path_ = std::move(root_path);
188   }
189
190  private:
191   bool initialized_ = false;
192   std::string appid_;
193   std::string pkgid_;
194   std::string root_path_;
195 };
196
197 int SendAndReceive(int cmd, uid_t uid,
198     std::vector<tizen_base::Bundle>* results) {
199   int fd = AppRequest(cmd, uid).SendSimply(AUL_SOCK_ASYNC);
200   if (fd < 0)
201     return fd;
202
203   int ret = aul_sock_recv_pkt_with_cb(fd,
204       [](app_pkt_t* pkt, void* user_data) {
205         if (pkt == nullptr) {
206           _E("Invalid parameter");
207           return;
208         }
209
210         bundle* b = nullptr;
211         if (pkt->opt & AUL_SOCK_BUNDLE)
212           b = bundle_decode(pkt->data, pkt->len);
213
214         if (b == nullptr)
215           return;
216
217         auto* res = static_cast<std::vector<tizen_base::Bundle>*>(user_data);
218         res->push_back(tizen_base::Bundle(b, false, true));
219       }, results);
220   if (ret < 0)
221     return aul_error_convert(ret);
222
223   return AUL_R_OK;
224 }
225
226 int SetAppInfo(aul_app_info* info, const tizen_base::Bundle& b) {
227 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
228   for (unsigned int i = 0; i < ARRAY_SIZE(kAppInfoKeys); ++i) {
229     if (b.GetType(kAppInfoKeys[i]) == BUNDLE_TYPE_NONE)
230       return -1;
231   }
232
233   info->pid = std::stoi(b.GetString(AUL_K_PID));
234   info->appid = const_cast<char*>(bundle_get_val(b.GetHandle(), AUL_K_APPID));
235   info->app_path = const_cast<char*>(bundle_get_val(b.GetHandle(), AUL_K_EXEC));
236   info->pkgid = const_cast<char*>(bundle_get_val(b.GetHandle(), AUL_K_PKGID));
237   info->instance_id = const_cast<char*>(
238       bundle_get_val(b.GetHandle(), AUL_K_INSTANCE_ID));
239   info->status = std::stoi(b.GetString(AUL_K_STATUS));
240   info->is_sub_app = std::stoi(b.GetString(AUL_K_IS_SUBAPP));
241   info->pkg_name = info->appid;
242   return 0;
243 }
244
245 int GetRunningAppInfoWithCb(int cmd, uid_t uid, aul_app_info_iter_fn cb,
246     void* user_data) {
247   if (cb == nullptr)
248     return AUL_R_EINVAL;
249
250   std::vector<tizen_base::Bundle> results;
251   int ret = SendAndReceive(cmd, uid, &results);
252   if (ret != AUL_R_OK)
253     return ret;
254
255   aul_app_info info;
256   for (auto const& b : results) {
257     if (SetAppInfo(&info, b) != 0)
258       break;
259
260     cb(&info, user_data);
261   }
262
263   return AUL_R_OK;
264 }
265
266 Context context;
267
268 int GetPkgIdFromDB(int pid, char* buf, int len, uid_t uid) {
269   auto appid = context.GetPreInitAppId();
270   if (appid.empty()) {
271     _E("Failed to get pre-initialized appid");
272     return -1;
273   }
274
275   auto* menu_info = _get_app_info_from_db_by_appid_user(appid.c_str(), uid);
276   if (menu_info == nullptr) {
277     _E("Failed to get app info. appid(%s)", appid.c_str());
278     return -1;
279   }
280
281   snprintf(buf, len, "%s", _get_pkgid(menu_info));
282   _free_app_info_from_db(menu_info);
283   return 0;
284 }
285
286 }  // namespace
287
288 extern "C" API int aul_app_get_pid(const char* appid) {
289   return aul_app_get_pid_for_uid(appid, getuid());
290 }
291
292 extern "C" API int aul_app_get_pid_for_uid(const char* appid, uid_t uid) {
293   if (appid == nullptr)
294     return -1;
295
296   return AppRequest(APP_GET_PID, uid)
297       .SetAppId(appid)
298       .SendSimply();
299 }
300
301 extern "C" API int aul_app_is_running(const char* appid) {
302   return aul_app_is_running_for_uid(appid, getuid());
303 }
304
305 extern "C" API int aul_app_is_running_for_uid(const char* appid, uid_t uid) {
306   if (appid == nullptr)
307     return 0;
308
309   int ret = AppRequest(APP_IS_RUNNING, uid)
310       .SetAppId(appid)
311       .SendSimply();
312   return ret > 0;
313 }
314
315 extern "C" API int aul_app_get_running_app_info(aul_app_info_iter_fn iter_fn,
316     void* user_data) {
317   return aul_app_get_running_app_info_for_uid(iter_fn, user_data, getuid());
318 }
319
320 extern "C" API int aul_app_get_running_app_info_for_uid(
321     aul_app_info_iter_fn iter_fn, void* user_data, uid_t uid) {
322   return GetRunningAppInfoWithCb(APP_RUNNING_INFO, uid, iter_fn, user_data);
323 }
324
325 extern "C" API int aul_app_get_all_running_app_info(
326     aul_app_info_iter_fn iter_fn, void* user_data) {
327   return aul_app_get_all_running_app_info_for_uid(iter_fn, user_data, getuid());
328 }
329
330 extern "C" API int aul_app_get_all_running_app_info_for_uid(
331     aul_app_info_iter_fn iter_fn, void* user_data, uid_t uid) {
332   return GetRunningAppInfoWithCb(APP_ALL_RUNNING_INFO, uid, iter_fn, user_data);
333 }
334
335 extern "C" API int aul_app_get_running_app_instance_info(
336     aul_app_info_iter_fn iter_fn, void* user_data) {
337   return aul_app_get_running_app_instance_info_for_uid(iter_fn, user_data,
338       getuid());
339 }
340
341 extern "C" API int aul_app_get_running_app_instance_info_for_uid(
342     aul_app_info_iter_fn iter_fn, void *user_data, uid_t uid) {
343   return GetRunningAppInfoWithCb(APP_RUNNING_INSTANCE_INFO, uid, iter_fn,
344       user_data);
345 }
346
347 extern "C" API void aul_set_preinit_appid(const char* appid) {
348   context.Initialize();
349   context.SetPreInitAppId(appid ? appid : "");
350 }
351
352 extern "C" API void aul_set_preinit_pkgid(const char* pkgid) {
353   context.Initialize();
354   context.SetPreInitPkgId(pkgid ? pkgid : "");
355 }
356
357 extern "C" API void aul_set_preinit_root_path(const char* root_path) {
358   context.Initialize();
359   context.SetPreInitRootPath(root_path ? root_path : "");
360 }
361
362 extern "C" API const char* aul_get_preinit_root_path(void) {
363   context.Initialize();
364   return context.GetPreInitRootPath().c_str();
365 }
366
367 extern "C" API int aul_app_get_pkgname_bypid(int pid, char* pkgname, int len) {
368   return aul_app_get_appid_bypid(pid, pkgname, len);
369 }
370
371 extern "C" API int aul_app_get_appid_bypid(int pid, char* appid, int len) {
372   return aul_app_get_appid_bypid_for_uid(pid, appid, len, getuid());
373 }
374
375 extern "C" API int aul_app_get_appid_bypid_for_uid(int pid, char* appid,
376     int len, uid_t uid) {
377   if (pid < 1 || appid == nullptr) {
378     _E("Invalid parameter");
379     return AUL_R_EINVAL;
380   }
381
382   if (getpid() == pid || getpgid(getpid()) == pid) {
383     context.Initialize();
384     auto& preinit_appid = context.GetPreInitAppId();
385     if (!preinit_appid.empty()) {
386       snprintf(appid, len, "%s", preinit_appid.c_str());
387       return AUL_R_OK;
388     }
389   }
390
391   int fd = AppRequest(APP_GET_APPID_BYPID, uid)
392       .SetPid(pid)
393       .SendSimply(AUL_SOCK_ASYNC);
394   if (fd < 0)
395     return AUL_R_ERROR;
396
397   app_pkt_t* pkt = nullptr;
398   int ret = aul_sock_recv_reply_pkt(fd, &pkt);
399   if (ret < 0 || pkt == nullptr)
400     return AUL_R_ERROR;
401
402   auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
403       pkt, std::free);
404   if (pkt->cmd != APP_GET_INFO_OK)
405     return AUL_R_ERROR;
406
407   snprintf(appid, len, "%s", pkt->data);
408   return AUL_R_OK;
409 }
410
411 extern "C" API int aul_app_get_pkgid_bypid(int pid, char* pkgid, int len) {
412   return aul_app_get_pkgid_bypid_for_uid(pid, pkgid, len, getuid());
413 }
414
415 extern "C" API int aul_app_get_pkgid_bypid_for_uid(int pid, char* pkgid,
416     int len, uid_t uid) {
417   if (pid < 1 || pkgid == nullptr) {
418     _E("Invalid parameter");
419     return AUL_R_EINVAL;
420   }
421
422   if (getpid() == pid || getpgid(getpid()) == pid) {
423     context.Initialize();
424     auto& preinit_pkgid = context.GetPreInitPkgId();
425     if (!preinit_pkgid.empty()) {
426       snprintf(pkgid, len, "%s", preinit_pkgid.c_str());
427       return AUL_R_OK;
428     }
429
430     if (GetPkgIdFromDB(pid, pkgid, len, uid) == 0)
431       return AUL_R_OK;
432   }
433
434   int fd = AppRequest(APP_GET_PKGID_BYPID, uid)
435       .SetPid(pid)
436       .SendSimply(AUL_SOCK_ASYNC);
437   if (fd < 0)
438     return AUL_R_ERROR;
439
440   app_pkt_t* pkt = nullptr;
441   int ret = aul_sock_recv_reply_pkt(fd, &pkt);
442   if (ret < 0 || pkt == nullptr)
443     return AUL_R_ERROR;
444
445   auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
446       pkt, std::free);
447   if (pkt->cmd != APP_GET_INFO_OK)
448     return AUL_R_ERROR;
449
450   snprintf(pkgid, len, "%s", pkt->data);
451   return AUL_R_OK;
452 }
453
454 extern "C" API int aul_update_rua_stat_for_uid(bundle* b, uid_t uid) {
455   if (b == nullptr) {
456     _E("Invalid parameter");
457     return AUL_R_EINVAL;
458   }
459
460   return AppRequest(APP_UPDATE_RUA_STAT, uid)
461       .With(b)
462       .SendSimply();
463 }
464
465 extern "C" API int aul_add_rua_history_for_uid(bundle* b, uid_t uid) {
466   if (b == nullptr) {
467     _E("Invalid parameter");
468     return AUL_R_EINVAL;
469   }
470
471   return AppRequest(APP_ADD_HISTORY, uid)
472       .With(b)
473       .SendSimply();
474 }
475
476 extern "C" API int aul_delete_rua_history_for_uid(bundle* b, uid_t uid) {
477   tizen_base::Bundle kb;
478   if (b != nullptr)
479     kb = std::move(tizen_base::Bundle(b, false, false));
480
481   return AppRequest(APP_REMOVE_HISTORY, uid)
482       .With(std::move(kb))
483       .SendSimply();
484 }
485
486 extern "C" API int aul_set_default_app_by_operation(bundle* b) {
487   if (b == nullptr)
488     return AUL_R_EINVAL;
489
490   return AppRequest(APP_SET_APP_CONTROL_DEFAULT_APP, getuid())
491       .With(b)
492       .SendSimply();
493 }
494
495 extern "C" API int aul_unset_default_app_by_operation(const char* appid) {
496   if (appid == nullptr)
497     return AUL_R_EINVAL;
498
499   int ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
500       APP_UNSET_APP_CONTROL_DEFAULT_APP,
501       reinterpret_cast<unsigned char*>(const_cast<char*>(appid)),
502       strlen(appid), AUL_SOCK_NONE);
503   if (ret != 0)
504     return aul_error_convert(ret);
505
506   return AUL_R_OK;
507 }
508
509 extern "C" API int aul_app_get_last_caller_pid(int pid) {
510   return aul_app_get_last_caller_pid_for_uid(pid, getuid());
511 }
512
513 extern "C" API int aul_app_get_last_caller_pid_for_uid(int pid, uid_t uid) {
514   if (pid < 1) {
515     _E("Invalid parameter");
516     return AUL_R_EINVAL;
517   }
518
519   return AppRequest(APP_GET_LAST_CALLER_PID, uid)
520       .SetPid(pid)
521       .SendSimply();
522 }
523
524 extern "C" API int aul_set_alias_appid(const char* alias_appid,
525     const char* appid) {
526   if (alias_appid == nullptr || appid == nullptr) {
527     _E("Invalid parameter");
528     return AUL_R_EINVAL;
529   }
530
531   tizen_base::Bundle b { { AUL_K_ALIAS_APPID, alias_appid } };
532
533   return AppRequest(APP_SET_ALIAS_APPID, getuid())
534       .With(std::move(b))
535       .SetAppId(appid)
536       .SendSimply();
537 }
538
539 extern "C" API int aul_unset_alias_appid(const char* alias_appid) {
540   if (alias_appid == nullptr) {
541     _E("Invalid parameter");
542     return AUL_R_EINVAL;
543   }
544
545   tizen_base::Bundle b { { AUL_K_ALIAS_APPID, alias_appid } };
546   return AppRequest(APP_UNSET_ALIAS_APPID, getuid())
547       .With(std::move(b))
548       .SendSimply();
549 }
550
551 extern "C" API int aul_enable_alias_info(const char* appid) {
552   if (appid == nullptr) {
553     _E("Invalid parameter");
554     return AUL_R_EINVAL;
555   }
556
557   return AppRequest(APP_ENABLE_ALIAS_INFO, getuid())
558       .SetAppId(appid)
559       .SendSimply();
560 }
561
562 extern "C" API int aul_disable_alias_info(const char* appid) {
563   if (appid == nullptr) {
564     _E("Invalid parameter");
565     return AUL_R_EINVAL;
566   }
567
568   return AppRequest(APP_DISABLE_ALIAS_INFO, getuid())
569       .SetAppId(appid)
570       .SendSimply();
571 }
572
573 extern "C" API int aul_set_auto_restart(bundle* b) {
574   tizen_base::Bundle kb { { AUL_K_AUTO_RESTART, "true" } };
575   if (b != nullptr) {
576     bundle_raw* raw;
577     int len;
578     if (bundle_encode(b, &raw, &len) != BUNDLE_ERROR_NONE)
579       return AUL_R_EINVAL;
580
581     kb.Add(AUL_K_RESTART_EXTRA, reinterpret_cast<char*>(raw));
582     bundle_free_encoded_rawdata(&raw);
583   }
584
585   return AppRequest(APP_SET_AUTO_RESTART, getuid())
586       .With(std::move(kb))
587       .SendSimply();
588 }
589
590 extern "C" API int aul_unset_auto_restart(void) {
591   tizen_base::Bundle kb { { AUL_K_AUTO_RESTART, "false" } };
592   return AppRequest(APP_SET_AUTO_RESTART, getuid())
593       .With(std::move(kb))
594       .SendSimply();
595 }
596
597 extern "C" API int aul_app_get_instance_id_bypid(int pid, char* instance_id,
598     int len) {
599   return aul_app_get_instance_id_bypid_for_uid(pid, instance_id, len, getuid());
600 }
601
602 extern "C" API int aul_app_get_instance_id_bypid_for_uid(int pid,
603     char* instance_id, int len, uid_t uid) {
604   if (pid < 1 || instance_id == nullptr) {
605     _E("Invalid parameter");
606     return AUL_R_EINVAL;
607   }
608
609   int fd = AppRequest(APP_GET_INSTANCE_ID_BYPID, uid)
610       .SetPid(pid)
611       .SendSimply(AUL_SOCK_ASYNC);
612   if (fd < 0)
613     return AUL_R_ERROR;
614
615   app_pkt_t* pkt = nullptr;
616   int ret = aul_sock_recv_reply_pkt(fd, &pkt);
617   if (ret < 0 || pkt == nullptr)
618     return AUL_R_ERROR;
619
620   auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
621       pkt, std::free);
622   if (pkt->cmd != APP_GET_INFO_OK)
623     return AUL_R_ERROR;
624
625   snprintf(instance_id, len, "%s", pkt->data);
626   return AUL_R_OK;
627 }
628
629 extern "C" API int aul_app_is_running_with_instance_id(const char* appid,
630     const char* instance_id, bool* running) {
631   if (appid == nullptr || instance_id == nullptr || running == nullptr) {
632     _E("Invalid parameter");
633     return AUL_R_EINVAL;
634   }
635
636   int ret = AppRequest(APP_IS_RUNNING, getuid())
637       .SetAppId(appid)
638       .SetInstId(instance_id)
639       .SendSimply();
640   if (ret < 0)
641     return ret;
642
643   *running = ret ? true : false;
644   return AUL_R_OK;
645 }
646
647 extern "C" API int aul_get_default_app(bundle* b, char** appid) {
648   if (b == nullptr || appid == nullptr) {
649     _E("Invalid parameter");
650     return AUL_R_EINVAL;
651   }
652
653   int fd = AppRequest(APP_GET_APP_CONTROL_DEFAULT_APP, getuid())
654       .With(b)
655       .SendSimply(AUL_SOCK_ASYNC);
656   if (fd < 0)
657     return fd;
658
659   app_pkt_t* pkt = nullptr;
660   int ret = aul_sock_recv_reply_pkt(fd, &pkt);
661   if (ret < 0 || pkt == nullptr) {
662     _E("aul_sock_recv_reply_pkt() is failed. error(%d)", ret);
663     return aul_error_convert(ret);
664   }
665
666   auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
667       pkt, std::free);
668   if (pkt->cmd != APP_GET_INFO_OK) {
669     _E("Failed to get default app. error(%d)", pkt->cmd);
670     return aul_error_convert(pkt->cmd);
671   }
672
673   if (!(pkt->opt & AUL_SOCK_BUNDLE)) {
674     _E("Invalid protocol");
675     return AUL_R_ERROR;
676   }
677
678   bundle* res_b = bundle_decode(pkt->data, pkt->len);
679   if (res_b == nullptr) {
680     _E("bundle_decode() is failed");
681     return AUL_R_ERROR;
682   }
683
684   tizen_base::Bundle kb(res_b, false, true);
685   auto value = kb.GetString(AUL_K_APPID);
686   if (value.empty()) {
687     _E("Failed to get appid");
688     return AUL_R_ERROR;
689   }
690
691   *appid = strdup(value.c_str());
692   if (*appid == nullptr) {
693     _E("strdup() is failed. appid(%s)", value.c_str());
694     return AUL_R_ENOMEM;
695   }
696
697   return AUL_R_OK;
698 }
699
700 extern "C" API int aul_package_pre_event_send(uid_t uid, bundle* b) {
701   if (b == nullptr) {
702     _E("Invalid paremeter");
703     return AUL_R_EINVAL;
704   }
705
706   if (!IsAmdReady())
707     return AUL_R_OK;
708
709   return AppRequest(PKG_PRE_EVENT_SEND, uid)
710       .With(b)
711       .SendSimply();
712 }
713
714 extern "C" API int aul_app_get_appid_bypid_async(pid_t pid,
715     aul_appid_cb callback, void* user_data) {
716   if (pid < 1 || callback == nullptr) {
717     _E("Invalid parameter");
718     return AUL_R_EINVAL;
719   }
720
721   if (getpid() == pid || getpgid(getpid()) == pid) {
722     context.Initialize();
723     auto& preinit_appid = context.GetPreInitAppId();
724     if (!preinit_appid.empty()) {
725       callback(AUL_R_OK, pid, preinit_appid.c_str(), user_data);
726       return AUL_R_OK;
727     }
728   }
729
730   int fd = AppRequest(APP_GET_APPID_BYPID, getuid())
731       .SetPid(pid)
732       .SendSimply(AUL_SOCK_ASYNC);
733   if (fd < 0)
734     return AUL_R_ERROR;
735
736   try {
737     auto* result_info = new ResultInfo(pid, fd, callback, user_data);
738     result_info->Watch();
739   } catch (const std::exception& e) {
740     _E("Exception occurs. error: %s", e.what());
741     close(fd);
742     return AUL_R_ERROR;
743   }
744
745   return AUL_R_OK;
746 }