• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eclipse-bluechi / bluechi / 16572578197

28 Jul 2025 03:00PM UTC coverage: 70.652% (-3.8%) from 74.418%
16572578197

push

github

engelmi
Release BlueChi v1.1.0

Signed-off-by: Michael Engel <mengel@redhat.com>

4858 of 6876 relevant lines covered (70.65%)

947.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/client/method-enable-disable.c
1
/*
2
 * Copyright Contributors to the Eclipse BlueChi project
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
#include "method-enable-disable.h"
7
#include "client.h"
8
#include "method-daemon-reload.h"
9
#include "usage.h"
10

11
#include "libbluechi/common/opt.h"
12

13
static int add_string_array_to_message(sd_bus_message *m, char **units, size_t units_count) {
×
14
        _cleanup_free_ char **units_array = NULL;
×
15
        int r = 0;
×
16

17
        units_array = malloc0_array(0, sizeof(char *), units_count + 1);
×
18
        if (units_array == NULL) {
×
19
                fprintf(stderr, "Failed to allocate memory\n");
×
20
                return -ENOMEM;
21
        }
22
        size_t i = 0;
23
        for (i = 0; i < units_count; i++) {
×
24
                units_array[i] = units[i];
×
25
        }
26

27
        r = sd_bus_message_append_strv(m, units_array);
×
28
        if (r < 0) {
×
29
                fprintf(stderr, "Failed to append the list of units to the message: %s\n", strerror(-r));
×
30
                return r;
31
        }
32

33
        return 0;
34
}
35

36
static int parse_enable_disable_response_from_message(sd_bus_message *m) {
×
37
        int r = 0;
×
38

39
        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
×
40
        if (r < 0) {
×
41
                fprintf(stderr, "Failed to open the strings array container: %s\n", strerror(-r));
×
42
                return r;
×
43
        }
44

45
        for (;;) {
×
46
                const char *op_type = NULL;
×
47
                const char *symlink_file = NULL;
×
48
                const char *symlink_dest = NULL;
×
49

50
                r = sd_bus_message_read(m, "(sss)", &op_type, &symlink_file, &symlink_dest);
×
51
                if (r < 0) {
×
52
                        fprintf(stderr, "Failed to read enabled unit file information: %s\n", strerror(-r));
×
53
                        return r;
×
54
                }
55
                if (r == 0) {
×
56
                        break;
57
                }
58
                if (streq(op_type, "symlink")) {
×
59
                        fprintf(stderr, "Created symlink %s -> %s\n", symlink_file, symlink_dest);
×
60
                } else if (streq(op_type, "unlink")) {
×
61
                        fprintf(stderr, "Removed \"%s\".\n", symlink_file);
×
62
                } else {
63
                        fprintf(stderr, "Unknown operation: %s\n", op_type);
×
64
                }
65
        }
66

67
        return 0;
×
68
}
69

70
static int method_enable_unit_on(
×
71
                Client *client, char *node_name, char **units, size_t units_count, int runtime, int force) {
72
        int r = 0;
×
73
        _cleanup_sd_bus_error_ sd_bus_error error = SD_BUS_ERROR_NULL;
×
74
        _cleanup_sd_bus_message_ sd_bus_message *result = NULL;
×
75
        _cleanup_sd_bus_message_ sd_bus_message *outgoing_message = NULL;
×
76

77
        r = client_create_message_new_method_call(client, node_name, "EnableUnitFiles", &outgoing_message);
×
78
        if (r < 0) {
×
79
                fprintf(stderr, "Failed to create a new message: %s\n", strerror(-r));
×
80
                return r;
81
        }
82

83
        r = add_string_array_to_message(outgoing_message, units, units_count);
×
84
        if (r < 0) {
×
85
                fprintf(stderr, "Failed to append the string array to the message: %s\n", strerror(-r));
×
86
        }
87

88
        r = sd_bus_message_append(outgoing_message, "bb", runtime, force);
×
89
        if (r < 0) {
×
90
                fprintf(stderr, "Failed to append runtime and force to the message: %s\n", strerror(-r));
×
91
                return r;
92
        }
93

94
        r = sd_bus_call(client->api_bus, outgoing_message, BC_DEFAULT_DBUS_TIMEOUT, &error, &result);
×
95
        if (r < 0) {
×
96
                fprintf(stderr, "Failed to issue call: %s\n", error.message);
×
97
                return r;
98
        }
99

100
        int carries_install_info = 0;
×
101
        r = sd_bus_message_read(result, "b", &carries_install_info);
×
102
        if (r < 0) {
×
103
                fprintf(stderr, "Failed to read carries_install_info from the message: %s\n", strerror(-r));
×
104
                return r;
105
        }
106

107
        if (carries_install_info) {
×
108
                fprintf(stderr, "The unit files included enablement information\n");
×
109
        } else {
110
                fprintf(stderr, "The unit files did not include any enablement information\n");
×
111
        }
112

113
        r = parse_enable_disable_response_from_message(result);
×
114
        if (r < 0) {
×
115
                fprintf(stderr, "Failed to parse the response strings array: %s\n", error.message);
×
116
                return r;
117
        }
118

119
        return 0;
120
}
121

122
static int method_disable_unit_on(Client *client, char *node_name, char **units, size_t units_count, int runtime) {
×
123
        int r = 0;
×
124
        _cleanup_sd_bus_error_ sd_bus_error error = SD_BUS_ERROR_NULL;
×
125
        _cleanup_sd_bus_message_ sd_bus_message *result = NULL;
×
126
        _cleanup_sd_bus_message_ sd_bus_message *outgoing_message = NULL;
×
127

128
        r = client_create_message_new_method_call(client, node_name, "DisableUnitFiles", &outgoing_message);
×
129
        if (r < 0) {
×
130
                fprintf(stderr, "Failed to create a new message: %s\n", strerror(-r));
×
131
                return r;
132
        }
133

134
        r = add_string_array_to_message(outgoing_message, units, units_count);
×
135
        if (r < 0) {
×
136
                fprintf(stderr, "Failed to append the string array to the message: %s\n", strerror(-r));
×
137
        }
138

139
        r = sd_bus_message_append(outgoing_message, "b", runtime);
×
140
        if (r < 0) {
×
141
                fprintf(stderr, "Failed to append runtime to the message: %s\n", strerror(-r));
×
142
                return r;
143
        }
144

145
        r = sd_bus_call(client->api_bus, outgoing_message, BC_DEFAULT_DBUS_TIMEOUT, &error, &result);
×
146
        if (r < 0) {
×
147
                fprintf(stderr, "Failed to issue call: %s\n", error.message);
×
148
                return r;
149
        }
150

151
        r = parse_enable_disable_response_from_message(result);
×
152
        if (r < 0) {
×
153
                fprintf(stderr, "Failed to parse the response strings array: %s\n", error.message);
×
154
                return r;
155
        }
156

157
        return 0;
158
}
159

160
int method_enable(Command *command, void *userdata) {
×
161
        int r = 0;
×
162
        r = method_enable_unit_on(
×
163
                        userdata,
164
                        command->opargv[0],
165
                        &command->opargv[1],
×
166
                        command->opargc - 1,
×
167
                        command_flag_exists(command, ARG_RUNTIME_SHORT),
×
168
                        command_flag_exists(command, ARG_FORCE_SHORT));
×
169
        if (r < 0) {
×
170
                fprintf(stderr,
×
171
                        "Failed to enable the units on node [%s] - %s",
172
                        command->opargv[0],
×
173
                        strerror(-r));
174
                return r;
×
175
        }
176

177
        if (!command_flag_exists(command, ARG_NO_RELOAD_SHORT)) {
×
178
                r = method_daemon_reload(command, userdata);
×
179
        }
180

181
        return r;
182
}
183

184
int method_disable(Command *command, void *userdata) {
×
185
        int r = 0;
×
186
        r = method_disable_unit_on(
×
187
                        userdata,
188
                        command->opargv[0],
189
                        &command->opargv[1],
×
190
                        command->opargc - 1,
×
191
                        command_flag_exists(command, ARG_RUNTIME_SHORT));
×
192
        if (r < 0) {
×
193
                fprintf(stderr,
×
194
                        "Failed to disable the units on node [%s] - %s",
195
                        command->opargv[0],
×
196
                        strerror(-r));
197
        }
198
        if (!command_flag_exists(command, ARG_NO_RELOAD_SHORT)) {
×
199
                r = method_daemon_reload(command, userdata);
×
200
        }
201

202
        return r;
×
203
}
204

205

206
void usage_method_enable() {
×
207
        usage_print_header();
×
208
        usage_print_description("Enable a unit on a node");
×
209
        usage_print_usage("bluechictl enable [nodename] [unit1, unit2, ...] [options]");
×
210
        printf("\n");
×
211
        printf("Available options:\n");
×
212
        printf("  --%s \t shows this help message\n", ARG_HELP);
×
213
        printf("  --%s \t if set, the changes don't persist after reboot \n", ARG_RUNTIME);
×
214
        printf("  --%s \t if set, symlinks pointing to other units are replaced \n", ARG_FORCE);
×
215
        printf("  --%s \t if set, no daemon-reload is triggered \n", ARG_NO_RELOAD);
×
216
}
×
217

218
void usage_method_disable() {
×
219
        usage_print_header();
×
220
        usage_print_description("Disable a unit on a node");
×
221
        usage_print_usage("bluechictl disable [nodename] [unit1, unit2, ...] [options]");
×
222
        printf("\n");
×
223
        printf("Available options:\n");
×
224
        printf("  --%s \t shows this help message\n", ARG_HELP);
×
225
        printf("  --%s \t if set, the changes don't persist after reboot \n", ARG_RUNTIME);
×
226
        printf("  --%s \t if set, no daemon-reload is triggered \n", ARG_NO_RELOAD);
×
227
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc