Skip to content

Commit 2e14c52

Browse files
committed
fix some error code states to make it better for shell scripts
1 parent b2138eb commit 2e14c52

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

usr.sbin/agectl/agectl.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
static int valid_age(const char *);
4747
static int valid_dob(const char *);
4848
static int calculate_age(const char *s);
49-
static void update_age_groups(const char *, int);
49+
static int update_age_groups(const char *, int);
5050
static int run_pw_command(const char *, const char *, const char *);
5151

5252

@@ -70,6 +70,7 @@ main(int argc, char *argv[])
7070
char *set_val = NULL;
7171
char *target_user = NULL;
7272
int mode = 0; /* 0 = query, 1 = set age, 2 = set dob */
73+
int update_failed = 0;
7374

7475
while ((ch = getopt(argc, argv, "a:b:")) != -1) {
7576
switch (ch) {
@@ -123,15 +124,21 @@ main(int argc, char *argv[])
123124

124125
if (mode == 1) {
125126
snprintf(buf, sizeof(buf), "SET %d age %s", pw->pw_uid, set_val);
126-
update_age_groups(target_user, atoi(set_val));
127-
} else {
127+
if (update_age_groups(target_user, atoi(set_val)) == 1) {
128+
update_failed = 1;
129+
}
130+
} else if (mode == 2) {
128131
snprintf(buf, sizeof(buf), "SET %d dob %s", pw->pw_uid, set_val);
129132

130133
int age = calculate_age(set_val);
131134
if (age < 0) {
132135
fprintf(stderr, "Failed to compute age from dob '%s'\n", set_val);
136+
close(fd);
137+
exit(1);
133138
} else {
134-
update_age_groups(target_user, age);
139+
if (update_age_groups(target_user, age) == 1) {
140+
update_failed = 1;
141+
}
135142
}
136143
}
137144
write(fd, buf, strlen(buf));
@@ -145,6 +152,10 @@ main(int argc, char *argv[])
145152
}
146153

147154
close(fd);
155+
156+
if (update_failed)
157+
return 1;
158+
148159
return 0;
149160
}
150161

@@ -293,17 +304,19 @@ run_pw_command(const char *group, const char *user, const char *action)
293304
return -1;
294305
}
295306

296-
static void
307+
static int
297308
update_age_groups(const char *username, int age)
298309
{
299310
const char *groups[] = {"age4p", "age13p", "age16p", "age18p"};
300311
int min_ages[] = {4, 13, 16, 18};
301312
int num_groups = sizeof(groups) / sizeof(groups[0]);
313+
int code = 0;
302314

303315
for (int i = 0; i < num_groups; i++) {
304316
struct group *grp = getgrnam(groups[i]);
305317
if (grp == NULL) {
306318
fprintf(stderr, "Group %s not found.\n", groups[i]);
319+
code = 1;
307320
continue;
308321
}
309322

@@ -320,6 +333,7 @@ update_age_groups(const char *username, int age)
320333
if (age >= min_ages[i]) {
321334
if (!in_group) {
322335
if (run_pw_command(groups[i], username, "-m") != 0) {
336+
code = 1;
323337
fprintf(stderr, "Failed to add user %s to group %s\n", username, groups[i]);
324338
} else {
325339
fprintf(stderr, "Added user %s to group %s\n", username, groups[i]);
@@ -328,11 +342,14 @@ update_age_groups(const char *username, int age)
328342
} else {
329343
if (in_group) {
330344
if (run_pw_command(groups[i], username, "-d") != 0) {
345+
code = 1;
331346
fprintf(stderr, "Failed to remove user %s from group %s\n", username, groups[i]);
332347
} else {
333348
fprintf(stderr, "Removed user %s from group %s\n", username, groups[i]);
334349
}
335350
}
336351
}
337352
}
353+
354+
return code;
338355
}

0 commit comments

Comments
 (0)