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> |
20 | |
21 | static GKeyFile *config; |
22 | |
23 | static const gchar *CONFIG_KEYS[] = { |
fbc0295f |
24 | "PrettyHostname", |
25 | "IconName", |
26 | "ChassisType" |
1c38000a |
27 | }; |
28 | |
29 | /* NULL if key doesn't exist */ |
30 | gchar *get_option(gchar *key, gchar *group) { |
31 | |
fbc0295f |
32 | if(!group) |
33 | group = "default"; |
1c38000a |
34 | |
fbc0295f |
35 | return g_key_file_get_string(config, group, key, NULL); |
1c38000a |
36 | } |
37 | |
38 | /* false if key isn't already defined or value is invalid */ |
39 | gboolean set_option(gchar *key, gchar *value, gchar *group) { |
40 | |
fbc0295f |
41 | if(!group) |
42 | group = "default"; |
1c38000a |
43 | |
fbc0295f |
44 | if(!g_key_file_get_string(config, group, key, NULL)) |
45 | return FALSE; |
1c38000a |
46 | |
fbc0295f |
47 | //TODO safteycheck value |
48 | g_key_file_set_string(config, group, key, value); |
49 | return TRUE; |
1c38000a |
50 | } |
51 | |
52 | /* initial load/check */ |
1c38000a |
53 | gboolean config_init() { |
54 | |
fbc0295f |
55 | if(config) |
56 | return TRUE; //already init'd |
c3b84b0a |
57 | |
fbc0295f |
58 | config = g_key_file_new(); |
59 | |
60 | const gchar *config_path; |
61 | GStatBuf *config_lstat; |
c3b84b0a |
62 | |
fbc0295f |
63 | config_path = "/etc/systemd_compat.conf"; |
c3b84b0a |
64 | |
fbc0295f |
65 | /* does conf exist? */ |
66 | if(g_lstat(config_path, config_lstat)) { |
c3b84b0a |
67 | |
fbc0295f |
68 | /* if not, can we write it */ |
69 | if(g_access("/etc/", W_OK)) { |
70 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
71 | return FALSE; |
72 | } |
c3b84b0a |
73 | |
fbc0295f |
74 | int config_descr; |
75 | config_descr = g_open(config_path, O_CREAT, 644); |
c3b84b0a |
76 | |
fbc0295f |
77 | gchar *posix_hostname; |
78 | posix_hostname = g_malloc(HOST_NAME_MAX); |
c3b84b0a |
79 | |
fbc0295f |
80 | gethostname(posix_hostname, HOST_NAME_MAX); |
c3b84b0a |
81 | |
fbc0295f |
82 | g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname); |
83 | g_key_file_set_string(config, "hostnamed", "PrettyHostname", ""); |
84 | g_key_file_set_string(config, "hostnamed", "IconName", "Computer"); |
85 | g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly |
c3b84b0a |
86 | |
fbc0295f |
87 | if(!g_key_file_save_to_file(config, config_path, NULL)) { |
88 | g_printf("failed to write config to %s!\n", config_path); |
89 | g_free(posix_hostname); |
90 | return FALSE; |
91 | } |
c3b84b0a |
92 | |
fbc0295f |
93 | g_printf("wrote config to %s\n", config_path); |
c3b84b0a |
94 | |
fbc0295f |
95 | g_free(posix_hostname); |
c3b84b0a |
96 | |
fbc0295f |
97 | return TRUE; |
1c38000a |
98 | |
fbc0295f |
99 | /* it does exist, read it */ |
100 | } else { |
1c38000a |
101 | |
fbc0295f |
102 | if(!g_access(config_path, W_OK)) { |
103 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
104 | return FALSE; |
105 | } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) |
106 | return TRUE; |
1c38000a |
107 | |
fbc0295f |
108 | g_printf("could not read config at %s! exiting..", config_path); |
109 | return FALSE; |
110 | } |
1c38000a |
111 | } |