Skip to content

Commit fbe6691

Browse files
committed
confd: replace systemf() shell-outs with POSIX/libite/libsrx APIs
Replace remaining systemf() calls that invoke simple file-system operations with direct C API equivalents, eliminating unnecessary fork/exec overhead: - mkdir -p → mkpath() from libite - ln -sf → erase() + symlink() from libite/POSIX - rm -rf → rmrf() from libsrx helpers - rm -f dir/* → rmrf() + mkpath() to clear and recreate the dir Files updated: dagger.c, containers.c, firewall.c, services.c, system.c Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
1 parent 8ef4376 commit fbe6691

5 files changed

Lines changed: 59 additions & 38 deletions

File tree

src/confd/src/containers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ static int del(const char *name)
345345

346346
/* Schedule a cleanup job for this container as soon as it has stopped */
347347
snprintf(prune_dir, sizeof(prune_dir), "%s/%s", _PATH_CLEAN, name);
348-
systemf("mkdir -p %s", prune_dir);
348+
mkpath(prune_dir, 0755);
349349

350350
/* Finit cleanup:script runs when container is deleted, it will remove any image by-ID */
351351
pp = popenf("r", "podman inspect %s 2>/dev/null | jq -r '.[].Id' 2>/dev/null", name);

src/confd/src/dagger.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static FILE *dagger_fopen(struct dagger *d, int gen, const char *action,
2121
return NULL;
2222
}
2323

24-
if (systemf("mkdir -p " PATH_ACTION_, d->path, gen, action, node))
24+
if (fmkpath(0755, PATH_ACTION_, d->path, gen, action, node))
2525
return NULL;
2626

2727
if (asprintf(&path, PATH_ACTION_"/%02u-%s", d->path, gen, action, node, prio, script) == -1)
@@ -117,7 +117,7 @@ int dagger_add_dep(const struct dagger *d, const char *depender, const char *dep
117117

118118
int dagger_add_node(struct dagger *d, const char *node)
119119
{
120-
return systemf("mkdir -p %s/%d/dag/%s", d->path, d->next, node);
120+
return fmkpath(0755, "%s/%d/dag/%s", d->path, d->next, node);
121121
}
122122

123123
int dagger_abandon(struct dagger *d)
@@ -192,13 +192,12 @@ int dagger_is_bootstrap(struct dagger *d)
192192

193193
int dagger_claim(struct dagger *d, const char *path)
194194
{
195-
int err;
195+
char lnk[strlen(path) + 32];
196196

197197
memset(d, 0, sizeof(*d));
198198

199-
err = systemf("mkdir -p %s", path);
200-
if (err)
201-
return err;
199+
if (mkpath(path, 0755))
200+
return -1;
202201

203202
d->next_fp = fopenf("wx", "%s/next", path);
204203
if (!d->next_fp) {
@@ -209,23 +208,22 @@ int dagger_claim(struct dagger *d, const char *path)
209208
if (readdf(&d->current, "%s/current", path)) {
210209
d->current = -1;
211210
} else {
212-
err = systemf("mkdir -p %s/%d/action/exit"
213-
" && "
214-
"ln -sf ../../top-down-order %s/%d/action/exit/order",
215-
path, d->current, path, d->current);
216-
if (err)
217-
return err;
211+
if (fmkpath(0755, "%s/%d/action/exit", path, d->current))
212+
return -1;
213+
snprintf(lnk, sizeof(lnk), "%s/%d/action/exit/order", path, d->current);
214+
erase(lnk);
215+
if (symlink("../../top-down-order", lnk))
216+
return -1;
218217
}
219218

220219
d->next = d->current + 1;
221-
err = systemf("mkdir -p %s/%d/action/init"
222-
" && "
223-
"mkdir -p %s/%d/skip"
224-
" && "
225-
"ln -s ../../bottom-up-order %s/%d/action/init/order",
226-
path, d->next, path, d->next, path, d->next);
227-
if (err)
228-
return err;
220+
if (fmkpath(0755, "%s/%d/action/init", path, d->next))
221+
return -1;
222+
if (fmkpath(0755, "%s/%d/skip", path, d->next))
223+
return -1;
224+
snprintf(lnk, sizeof(lnk), "%s/%d/action/init/order", path, d->next);
225+
if (symlink("../../bottom-up-order", lnk) && errno != EEXIST)
226+
return -1;
229227

230228
strlcpy(d->path, path, sizeof(d->path));
231229
return 0;

src/confd/src/firewall.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ int firewall_change(sr_session_ctx_t *session, struct lyd_node *config, struct l
504504
break;
505505

506506
case SR_EV_ABORT:
507-
systemf("rm -rf " FIREWALLD_DIR_NEXT);
507+
rmrf(FIREWALLD_DIR_NEXT);
508508
return SR_ERR_OK;
509509

510510
case SR_EV_DONE:
@@ -515,7 +515,7 @@ int firewall_change(sr_session_ctx_t *session, struct lyd_node *config, struct l
515515
}
516516

517517
/* Firewall is enabled, roll in new configuration */
518-
systemf("rm -rf " FIREWALLD_DIR);
518+
rmrf(FIREWALLD_DIR);
519519
if (rename(FIREWALLD_DIR_NEXT, FIREWALLD_DIR)) {
520520
ERRNO("Failed rolling in firewalld configuration");
521521
return SR_ERR_SYS;
@@ -533,7 +533,7 @@ int firewall_change(sr_session_ctx_t *session, struct lyd_node *config, struct l
533533
global = lydx_get_descendant(tree, "firewall", NULL);
534534

535535
/* Clean up any stale /etc/firewalld+ first */
536-
systemf("rm -rf " FIREWALLD_DIR_NEXT);
536+
rmrf(FIREWALLD_DIR_NEXT);
537537

538538
/* If firewall is disabled or not enabled, don't generate config */
539539
if (!global || !lydx_is_enabled(global, "enabled")) {

src/confd/src/services.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,28 @@ static void svc_enable(int ena, svc type, const char *svcname)
285285
svcname = name[type];
286286

287287
if (fexistf("/etc/nginx/available/%s.conf", svcname)) {
288-
if (ena)
289-
systemf("ln -sf ../available/%s.conf /etc/nginx/enabled/", svcname);
290-
else
291-
systemf("rm -f /etc/nginx/enabled/%s.conf", svcname);
288+
char src[256], dst[256];
289+
290+
snprintf(dst, sizeof(dst), "/etc/nginx/enabled/%s.conf", svcname);
291+
if (ena) {
292+
snprintf(src, sizeof(src), "../available/%s.conf", svcname);
293+
erase(dst);
294+
symlink(src, dst);
295+
} else {
296+
erase(dst);
297+
}
292298
}
293299
if (fexistf("/etc/nginx/%s.app", svcname)) {
294-
if (ena)
295-
systemf("ln -sf ../%s.app /etc/nginx/app/%s.conf", svcname, svcname);
296-
else
297-
systemf("rm -f /etc/nginx/app/%s.conf", svcname);
300+
char src[256], dst[256];
301+
302+
snprintf(dst, sizeof(dst), "/etc/nginx/app/%s.conf", svcname);
303+
if (ena) {
304+
snprintf(src, sizeof(src), "../%s.app", svcname);
305+
erase(dst);
306+
symlink(src, dst);
307+
} else {
308+
erase(dst);
309+
}
298310
}
299311

300312
ena ? finit_enable(svcname) : finit_disable(svcname);

src/confd/src/system.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,11 @@ static int change_clock(sr_session_ctx_t *session, struct lyd_node *config, stru
268268
}
269269
}
270270

271+
char zonelink[256];
272+
273+
snprintf(zonelink, sizeof(zonelink), "/usr/share/zoneinfo/%s", timezone);
271274
(void)remove("/etc/localtime+");
272-
if (systemf("ln -sf /usr/share/zoneinfo/%s /etc/localtime+", timezone)) {
275+
if (symlink(zonelink, "/etc/localtime+")) {
273276
ERROR("No such timezone %s", timezone);
274277
rc = SR_ERR_VALIDATION_FAILED;
275278
}
@@ -306,7 +309,8 @@ static int change_ntp_client(sr_session_ctx_t *session, struct lyd_node *config,
306309
case SR_EV_DONE:
307310
if (!srx_enabled(session, XPATH_NTP_"/enabled")) {
308311
(void)remove(NTP_CLIENT_CONF);
309-
systemf("rm -f /etc/chrony/sources.d/*");
312+
rmrf("/etc/chrony/sources.d");
313+
mkpath("/etc/chrony/sources.d", 0755);
310314
/* Note: chronyd enable/disable is managed centrally in core.c */
311315
finit_reload("chronyd");
312316
return SR_ERR_OK;
@@ -701,7 +705,9 @@ static int sys_del_user(char *user, bool silent)
701705
ERROR("Error deleting user \"%s\"", user);
702706

703707
/* Ensure $HOME is removed at least. */
704-
systemf("rm -rf /home/%s", user);
708+
char home[256];
709+
snprintf(home, sizeof(home), "/home/%s", user);
710+
rmrf(home);
705711

706712
return SR_ERR_SYS;
707713
}
@@ -825,9 +831,14 @@ static int sys_add_user(sr_session_ctx_t *sess, char *name)
825831
* /home/%s/.ssh/authorized_keys file. This creates a both the
826832
* directory and the symlink owned by root to prevent tampering.
827833
*/
834+
char src[256], dst[256];
835+
828836
DEBUG("Adding secure /home/%s/.ssh directory.", name);
829837
fmkpath(0750, "/home/%s/.ssh", name);
830-
systemf("ln -sf /var/run/sshd/%s.keys /home/%s/.ssh/authorized_keys", name, name);
838+
snprintf(src, sizeof(src), "/var/run/sshd/%s.keys", name);
839+
snprintf(dst, sizeof(dst), "/home/%s/.ssh/authorized_keys", name);
840+
erasef("/home/%s/.ssh/authorized_keys", name);
841+
symlink(src, dst);
831842

832843
return SR_ERR_OK;
833844
}
@@ -1491,7 +1502,7 @@ static int change_editor(sr_session_ctx_t *session, struct lyd_node *config, str
14911502
continue;
14921503

14931504
erase(alt);
1494-
rc = systemf("ln -s %s %s", map[i].path, alt);
1505+
rc = symlink(map[i].path, alt);
14951506
if (rc)
14961507
ERROR("Failed setting system editor '%s'", map[i].editor);
14971508
}
@@ -1611,7 +1622,7 @@ static int change_hostname(sr_session_ctx_t *session, struct lyd_node *config, s
16111622
}
16121623

16131624
/* Use hostname.d for deterministic hostname management */
1614-
systemf("mkdir -p /etc/hostname.d");
1625+
mkpath("/etc/hostname.d", 0755);
16151626

16161627
fp = fopen("/etc/hostname.d/50-configured", "w");
16171628
if (!fp)

0 commit comments

Comments
 (0)