Create initial commit
[platform/core/system/storaged.git] / src / core / dbus_main.c
1 /*
2  * storaged
3  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"),
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <dbus/dbus.h>
22 #include <stdbool.h>
23 #include <limits.h>
24 #include "fd_handler.h"
25 #include "dbus.h"
26 #include "log.h"
27
28 /* -1 is a default timeout value, it's converted to 25*1000 internally. */
29 #define DBUS_REPLY_TIMEOUT  (-1)
30
31 int set_dbus_connection(const char *bus)
32 {
33         static DBusConnection *conn;
34         static int already;
35         DBusError err;
36         int ret;
37
38         if (conn && already)
39                 return 0;
40
41         dbus_error_init(&err);
42
43         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
44         if (!conn) {
45                 if (dbus_error_is_set(&err)) {
46                         _E("dbus_bus_get error(%s, %s)", err.name, err.message);
47                         dbus_error_free(&err);
48                 } else
49                         _E("dbus_bus_get error");
50                 return -ECOMM;
51         }
52
53         if (!already) {
54                 already = 1;
55                 dbus_error_init(&err);
56
57                 ret = dbus_bus_request_name(conn, bus,
58                                  DBUS_NAME_FLAG_REPLACE_EXISTING | DBUS_NAME_FLAG_DO_NOT_QUEUE,
59                                  &err);
60                 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
61                         if (dbus_error_is_set(&err)) {
62                                 _E("Failed to request dbus name (%s, %s, %s)", bus, err.name, err.message);
63                                 dbus_error_free(&err);
64                         } else
65                                 _E("Failed to request dbus name (%s)", bus);
66                         return -ECOMM;
67                 }
68         }
69
70         return 0;
71 }