Merge "Use system hostname first" into tizen
[platform/upstream/connman.git] / src / utsname.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <glib.h>
27
28 #include "connman.h"
29
30 static GSList *driver_list = NULL;
31
32 static gint compare_priority(gconstpointer a, gconstpointer b)
33 {
34         const struct connman_utsname_driver *driver1 = a;
35         const struct connman_utsname_driver *driver2 = b;
36
37         return driver2->priority - driver1->priority;
38 }
39
40 /**
41  * connman_utsname_driver_register:
42  * @driver: utsname driver definition
43  *
44  * Register a new utsname driver
45  *
46  * Returns: %0 on success
47  */
48 int connman_utsname_driver_register(struct connman_utsname_driver *driver)
49 {
50         DBG("driver %p name %s", driver, driver->name);
51
52         driver_list = g_slist_insert_sorted(driver_list, driver,
53                                                         compare_priority);
54
55         return 0;
56 }
57
58 /**
59  * connman_utsname_driver_unregister:
60  * @driver: utsname driver definition
61  *
62  * Remove a previously registered utsname driver
63  */
64 void connman_utsname_driver_unregister(struct connman_utsname_driver *driver)
65 {
66         DBG("driver %p name %s", driver, driver->name);
67
68         driver_list = g_slist_remove(driver_list, driver);
69 }
70
71 /**
72  * connman_utsname_get_hostname:
73  *
74  * Returns current hostname
75  */
76 const char *connman_utsname_get_hostname(void)
77 {
78         GSList *list;
79
80         DBG("");
81
82         for (list = driver_list; list; list = list->next) {
83                 struct connman_utsname_driver *driver = list->data;
84                 const char *hostname;
85
86                 DBG("driver %p name %s", driver, driver->name);
87
88                 if (!driver->get_hostname)
89                         continue;
90
91                 hostname = driver->get_hostname();
92                 if (hostname)
93                         return hostname;
94         }
95
96         return NULL;
97 }
98
99 int __connman_utsname_set_hostname(const char *hostname)
100 {
101         GSList *list;
102
103         DBG("hostname %s", hostname);
104
105         for (list = driver_list; list; list = list->next) {
106                 struct connman_utsname_driver *driver = list->data;
107
108                 DBG("driver %p name %s", driver, driver->name);
109
110                 if (!driver->set_hostname)
111                         continue;
112
113                 if (driver->set_hostname(hostname) == 0)
114                         break;
115         }
116
117         return 0;
118 }
119
120 int __connman_utsname_set_domainname(const char *domainname)
121 {
122         GSList *list;
123
124         DBG("domainname %s", domainname);
125
126         for (list = driver_list; list; list = list->next) {
127                 struct connman_utsname_driver *driver = list->data;
128
129                 DBG("driver %p name %s", driver, driver->name);
130
131                 if (!driver->set_domainname)
132                         continue;
133
134                 if (driver->set_domainname(domainname) == 0)
135                         break;
136         }
137
138         return 0;
139 }