improve line coverage
[platform/core/pim/contacts-service.git] / client / ctsvc_client_utils.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #define _GNU_SOURCE
21 #include <unistd.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
24 #include <systemd/sd-login.h>
25
26 #include "ctsvc_internal.h"
27
28 #define CTSVC_SYSTEM_SLICE "system.slice"
29
30 #define CTSVC_INLINE __attribute__ ((gnu_inline))
31
32 CTSVC_INLINE inline unsigned int ctsvc_client_get_pid()
33 {
34         return (unsigned int)getpid();
35 }
36
37 CTSVC_INLINE inline unsigned int ctsvc_client_get_tid()
38 {
39         return (unsigned int)syscall(SYS_gettid);
40 }
41
42 bool ctsvc_client_is_in_system_session()
43 {
44         int ret = 0;
45         char *slice = NULL;
46
47         ret = sd_pid_get_slice(getpid(), &slice);
48         if (0 <= ret && slice) {
49                 if (STRING_EQUAL == strncmp(slice, CTSVC_SYSTEM_SLICE, strlen(CTSVC_SYSTEM_SLICE))) {
50                         /* LCOV_EXCL_START */
51                         free(slice);
52                         return TRUE;
53                         /* LCOV_EXCL_STOP */
54                 }
55         } else {
56                 /* LCOV_EXCL_START */
57                 ERR("sd_pid_get_slice() Fail(%d)", ret);
58                 /* LCOV_EXCL_STOP */
59         }
60
61         free(slice);
62         return FALSE;
63 }
64
65 /* LCOV_EXCL_START */
66 int ctsvc_client_get_active_uid(uid_t *uid)
67 {
68         int active_user_count = 0;
69         uid_t *active_user_list = NULL;
70
71         active_user_count = sd_get_active_uids(&active_user_list);
72         /* the number of active users is 1 in tizen3.0 */
73         if (1 == active_user_count && active_user_list) {
74                 *uid = active_user_list[0];
75                 INFO("active uid = %d", *uid);
76         } else {
77                 ERR("sd_get_active_uids() Fail(%d)", active_user_count);
78                 free(active_user_list);
79                 return CONTACTS_ERROR_SYSTEM;
80         }
81
82         free(active_user_list);
83         return CONTACTS_ERROR_NONE;
84 }
85 /* LCOV_EXCL_STOP */