Skip to content

Commit 2edb7ae

Browse files
committed
Return success for empty check-trustdb
fapolicyd-cli --check-trustdb could not distinguish an empty trust database from a database walk failure because walk_database_start() returned the same non-zero value for both cases. That made an empty trust database look like CLI_EXIT_DB_ERROR. The database walk API now has explicit success, error, and empty statuses. The walk start path closes the database before returning empty or error, and --check-trustdb maps the empty status to CLI_EXIT_SUCCESS while preserving database errors for real failures.
1 parent a6971d1 commit 2edb7ae

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/cli/fapolicyd-cli.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,8 @@ static int check_trustdb(void)
929929
}
930930
int rc = walk_database_start(&config);
931931
reset_config();
932+
if (rc == WALK_DATABASE_EMPTY)
933+
return CLI_EXIT_SUCCESS;
932934
if (rc)
933935
return CLI_EXIT_DB_ERROR;
934936

src/library/database.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6082,37 +6082,49 @@ static void *update_thread_main(void *arg)
60826082
***********************************************************************/
60836083
static walkdb_entry_t wdb_entry;
60846084

6085-
// Returns 0 on success and 1 on failure
6085+
// Returns WALK_DATABASE_SUCCESS, WALK_DATABASE_EMPTY, or WALK_DATABASE_ERROR
60866086
int walk_database_start(conf_t *config)
60876087
{
60886088
int rc;
60896089

60906090
// Initialize the database
60916091
if (init_db(config)) {
60926092
printf("Cannot open the trust database\n");
6093-
return 1;
6093+
return WALK_DATABASE_ERROR;
60946094
}
6095-
if (database_empty()) {
6095+
rc = database_empty();
6096+
if (rc > 0) {
60966097
printf("The trust database is empty - nothing to do\n");
6097-
return 1;
6098+
close_db(0);
6099+
return WALK_DATABASE_EMPTY;
6100+
} else if (rc < 0) {
6101+
close_db(0);
6102+
return WALK_DATABASE_ERROR;
60986103
}
60996104

61006105
// Position to the first entry
61016106
if (trust_db_read_open(&walk_read)) {
61026107
puts("Cannot open the trust database for reading");
6103-
return 1;
6108+
close_db(0);
6109+
return WALK_DATABASE_ERROR;
61046110
}
61056111

61066112
if ((rc = mdb_cursor_get(walk_read.cursor, &wdb_entry.path,
61076113
&wdb_entry.data,
61086114
MDB_FIRST)) == 0)
6109-
return 0;
6115+
return WALK_DATABASE_SUCCESS;
61106116

6111-
if (rc != MDB_NOTFOUND)
6117+
if (rc != MDB_NOTFOUND) {
61126118
puts(mdb_strerror(rc));
6119+
rc = WALK_DATABASE_ERROR;
6120+
} else {
6121+
printf("The trust database is empty - nothing to do\n");
6122+
rc = WALK_DATABASE_EMPTY;
6123+
}
61136124

61146125
trust_db_read_close(&walk_read);
6115-
return 1;
6126+
close_db(0);
6127+
return rc;
61166128
}
61176129

61186130
walkdb_entry_t *walk_database_get_entry(void)

src/library/database.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ void lock_rule(void);
8181
void unlock_rule(void);
8282

8383
// Database verification functions
84+
enum walk_database_status {
85+
WALK_DATABASE_SUCCESS = 0,
86+
WALK_DATABASE_ERROR = 1,
87+
WALK_DATABASE_EMPTY = 2,
88+
};
89+
8490
int walk_database_start(conf_t *config) __nonnull ((1));
8591
walkdb_entry_t *walk_database_get_entry(void);
8692
int walk_database_next(void);

0 commit comments

Comments
 (0)