baa40e28 |
1 | /* |
2 | * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc> |
3 | * |
4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above |
6 | * copyright notice and this permission notice appear in all copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ |
16 | |
1c38000a |
17 | #include <unistd.h> |
18 | #include <fcntl.h> |
19 | #include <gio/gio.h> |
e0453635 |
20 | #include "config.h" |
1c38000a |
21 | |
22 | static GKeyFile *config; |
7d10ea85 |
23 | static int config_descr; |
4241079f |
24 | static gchar *data_dir; |
25 | |
1c38000a |
26 | /* NULL if key doesn't exist */ |
27 | gchar *get_option(gchar *key, gchar *group) { |
28 | |
fbc0295f |
29 | if(!group) |
30 | group = "default"; |
1c38000a |
31 | |
fbc0295f |
32 | return g_key_file_get_string(config, group, key, NULL); |
1c38000a |
33 | } |
34 | |
35 | /* false if key isn't already defined or value is invalid */ |
36 | gboolean set_option(gchar *key, gchar *value, gchar *group) { |
37 | |
fbc0295f |
38 | if(!group) |
39 | group = "default"; |
1c38000a |
40 | |
fbc0295f |
41 | if(!g_key_file_get_string(config, group, key, NULL)) |
42 | return FALSE; |
1c38000a |
43 | |
fbc0295f |
44 | //TODO safteycheck value |
45 | g_key_file_set_string(config, group, key, value); |
46 | return TRUE; |
1c38000a |
47 | } |
48 | |
49 | /* initial load/check */ |
1c38000a |
50 | gboolean config_init() { |
51 | |
fbc0295f |
52 | config = g_key_file_new(); |
53 | |
54 | const gchar *config_path; |
55 | GStatBuf *config_lstat; |
e0453635 |
56 | int config_lstat_ret; |
c3b84b0a |
57 | |
fbc0295f |
58 | config_path = "/etc/systemd_compat.conf"; |
e0453635 |
59 | config_lstat_ret = g_lstat(config_path, config_lstat); |
c3b84b0a |
60 | |
fbc0295f |
61 | /* does conf exist? */ |
e0453635 |
62 | if(config_lstat_ret) { |
c3b84b0a |
63 | |
e0453635 |
64 | /* if not, can we write to where it should be? */ |
fbc0295f |
65 | if(g_access("/etc/", W_OK)) { |
66 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
67 | return FALSE; |
68 | } |
c3b84b0a |
69 | |
fbc0295f |
70 | int config_descr; |
71 | config_descr = g_open(config_path, O_CREAT, 644); |
c3b84b0a |
72 | |
fbc0295f |
73 | gchar *posix_hostname; |
74 | posix_hostname = g_malloc(HOST_NAME_MAX); |
c3b84b0a |
75 | |
fbc0295f |
76 | gethostname(posix_hostname, HOST_NAME_MAX); |
c3b84b0a |
77 | |
fbc0295f |
78 | g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname); |
79 | g_key_file_set_string(config, "hostnamed", "PrettyHostname", ""); |
80 | g_key_file_set_string(config, "hostnamed", "IconName", "Computer"); |
81 | g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly |
c3b84b0a |
82 | |
fbc0295f |
83 | if(!g_key_file_save_to_file(config, config_path, NULL)) { |
84 | g_printf("failed to write config to %s!\n", config_path); |
85 | g_free(posix_hostname); |
86 | return FALSE; |
87 | } |
c3b84b0a |
88 | |
fbc0295f |
89 | g_printf("wrote config to %s\n", config_path); |
c3b84b0a |
90 | |
fbc0295f |
91 | g_free(posix_hostname); |
c3b84b0a |
92 | |
fbc0295f |
93 | return TRUE; |
1c38000a |
94 | |
fbc0295f |
95 | /* it does exist, read it */ |
96 | } else { |
1c38000a |
97 | |
c9e361de |
98 | if(g_access(config_path, W_OK)) { |
fbc0295f |
99 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
100 | return FALSE; |
101 | } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) |
102 | return TRUE; |
1c38000a |
103 | |
fbc0295f |
104 | g_printf("could not read config at %s! exiting..", config_path); |
105 | return FALSE; |
106 | } |
e3756876 |
107 | } |
94304e78 |
108 | |
109 | gboolean init_xml() { |
110 | |
e0453635 |
111 | const gchar * const *data_dir_prefix; |
112 | int xml_lstat_ret; |
113 | GStatBuf *xml_lstat; |
94304e78 |
114 | |
115 | data_dir_prefix = g_get_system_data_dirs(); |
116 | data_dir = g_strconcat(data_dir_prefix[0], "systemd_compat", NULL); |
e0453635 |
117 | |
118 | xml_lstat_ret = g_lstat(data_dir, xml_lstat); |
94304e78 |
119 | |
120 | /* does xml dir exist? */ |
e0453635 |
121 | if(xml_lstat_ret) { |
94304e78 |
122 | |
e0453635 |
123 | /* if not, can we write to where it should be? */ |
94304e78 |
124 | if(g_access(data_dir_prefix[0], W_OK)) { |
125 | g_printf("no write permissions for %s! exiting...\n", data_dir_prefix[0]); |
126 | return FALSE; |
127 | } |
128 | |
129 | g_printf("creating xml data directory %s...\n", data_dir); |
e0453635 |
130 | |
131 | if(g_mkdir(data_dir, 644) || g_access) { |
94304e78 |
132 | g_printf("failed to create dir %s...\n", data_dir); |
133 | return FALSE; |
134 | } |
135 | |
e0453635 |
136 | /* read in xml files from conf/ */ |
137 | if(!read_xml_from_installconf()) { |
138 | g_printf("failed to read xml configs in conf/...\n"); |
94304e78 |
139 | return FALSE; |
140 | } |
141 | |
e0453635 |
142 | /* write our configs to system data dir */ |
143 | if(!populate_xml_data_dir()) { |
144 | g_printf("failed to write xml configs to %s...\n", data_dir); |
145 | return FALSE; |
146 | } |
94304e78 |
147 | |
e0453635 |
148 | /* get descriptors from freshly-installed configs */ |
149 | if(!set_xml_descriptors()) { |
150 | g_printf("failed to fopen xml configs...\n"); |
151 | return FALSE; |
152 | } |
94304e78 |
153 | |
154 | return TRUE; |
e0453635 |
155 | |
156 | /* it does exist, read it */ |
94304e78 |
157 | } else { |
158 | |
e0453635 |
159 | if(!set_xml_descriptors()) { |
160 | g_printf("failed to fopen xml configs...\n"); |
94304e78 |
161 | return FALSE; |
94304e78 |
162 | } |
163 | |
e0453635 |
164 | return TRUE; |
94304e78 |
165 | } |
94304e78 |
166 | } |
167 | |
168 | void clean_config() { |
169 | |
170 | //TODO g_ptr_array all of this |
171 | g_free(config); |
172 | g_free(data_dir); |
173 | g_close(config_descr, NULL); |
e3756876 |
174 | } |