Bug fix TIVI-839 ,change the design and permit operation of the music player while...
[profile/ivi/ico-uxf-homescreen.git] / ico-app-framework / ico_apf_appmgr.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   header file of Apprication Framework (Application Manager)
11  *
12  * @date    Feb-28-2013
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <aul/aul.h>
24
25 #include "ico_apf_private.h"
26 #include "ico_uxf_conf.h"
27
28 /*--------------------------------------------------------------------------*/
29 /**
30  * @brief   ico_apf_get_app_id: Get application id(in AppCore) from application process id
31  *
32  * @param[in]   pid                 application process id
33  * @param[out]  appid               application id
34  * @return      result
35  * @retval      ICO_APP_CTL_E_NONE              success
36  * @retval      ICO_APP_CTL_E_INVALID_PARAM     error(pid dose not exist)
37  */
38 /*--------------------------------------------------------------------------*/
39 ICO_APF_API int
40 ico_apf_get_app_id(const int pid, char *appid)
41 {
42     int     fd;
43     int     ppid = pid;
44     int     i, j;
45     int     size;
46     char    procpath[240];
47
48     memset(appid, 0, ICO_UXF_MAX_PROCESS_NAME+1);
49
50     if (ppid == 0)   {
51         ppid = getpid();
52     }
53
54     /* Get applicationId from AppCore(AUL)  */
55     if (aul_app_get_appid_bypid(ppid, appid, ICO_UXF_MAX_PROCESS_NAME)
56                     != AUL_R_OK)    {
57         snprintf(procpath, sizeof(procpath)-1, "/proc/%d/cmdline", ppid);
58         fd = open(procpath, O_RDONLY);
59         if (fd >= 0)    {
60             size = read(fd, procpath, sizeof(procpath));
61             for (; size > 0; size--)    {
62                 if (procpath[size-1])   break;
63             }
64             if (size > 0)   {
65                 /* Get program base name    */
66                 i = 0;
67                 for (j = 0; j < size; j++)  {
68                     if (procpath[j] == 0)   break;
69                     if (procpath[j] == '/') i = j + 1;
70                 }
71                 j = 0;
72                 for (; i < size; i++)   {
73                     appid[j] = procpath[i];
74                     if ((appid[j] == 0) ||
75                         (j >= ICO_UXF_MAX_PROCESS_NAME))    break;
76                     j++;
77                 }
78                 /* Search application number    */
79                 if ((appid[j] == 0) && (j < (ICO_UXF_MAX_PROCESS_NAME-1))) {
80                     for (; i < size; i++)   {
81                         if ((procpath[i] == 0) &&
82                             (procpath[i+1] == '@')) {
83                             strncpy(&appid[j], &procpath[i+1],
84                                     ICO_UXF_MAX_PROCESS_NAME - j - 1);
85                         }
86                     }
87                 }
88             }
89             close(fd);
90         }
91         if (appid[0] == 0)  {
92             apfw_trace("ico_apf_get_app_id: LEAVE(pid=%d dose not exist)", ppid);
93             sprintf(appid, "?%d?", ppid);
94             return ICO_APP_CTL_E_INVALID_PARAM;
95         }
96     }
97     return ICO_APP_CTL_E_NONE;
98 }
99