Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#316] Use err(3) #317

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <err.h>

#define DEFAULT_PASSWORD_LENGTH 64
#define MIN_PASSWORD_LENGTH 8
Expand Down Expand Up @@ -169,8 +170,7 @@ main(int argc, char** argv)

if (getuid() == 0)
{
printf("pgagroal: Using the root account is not allowed\n");
exit(1);
errx(1, "Using the root account is not allowed");
}

if (argc > 0)
Expand Down Expand Up @@ -200,8 +200,7 @@ main(int argc, char** argv)
{
if (master_key(password, generate_pwd, pwd_length))
{
printf("Error for master key\n");
exit_code = 1;
errx(1, "Error for master key");
}
}
else if (action == ACTION_ADD_USER)
Expand All @@ -210,14 +209,12 @@ main(int argc, char** argv)
{
if (add_user(file_path, username, password, generate_pwd, pwd_length))
{
printf("Error for add-user\n");
exit_code = 1;
errx(1, "Error for add-user");
}
}
else
{
printf("Missing file argument\n");
exit_code = 1;
errx(1, "Missing file argument");
}
}
else if (action == ACTION_UPDATE_USER)
Expand All @@ -226,14 +223,12 @@ main(int argc, char** argv)
{
if (update_user(file_path, username, password, generate_pwd, pwd_length))
{
printf("Error for update-user\n");
exit_code = 1;
errx(1, "Error for update-user");
}
}
else
{
printf("Missing file argument\n");
exit_code = 1;
errx(1, "Missing file argument");
}
}
else if (action == ACTION_REMOVE_USER)
Expand All @@ -242,14 +237,12 @@ main(int argc, char** argv)
{
if (remove_user(file_path, username))
{
printf("Error for remove-user\n");
exit_code = 1;
errx(1, "Error for remove-user");
}
}
else
{
printf("Missing file argument\n");
exit_code = 1;
errx(1, "Missing file argument");
}
}
else if (action == ACTION_LIST_USERS)
Expand All @@ -258,14 +251,12 @@ main(int argc, char** argv)
{
if (list_users(file_path))
{
printf("Error for list-users\n");
exit_code = 1;
errx(1, "Error for list-users");
}
}
else
{
printf("Missing file argument\n");
exit_code = 1;
errx(1, "Missing file argument");
}
}
}
Expand Down
30 changes: 12 additions & 18 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <err.h>

#include <openssl/ssl.h>

Expand Down Expand Up @@ -213,8 +214,7 @@ main(int argc, char** argv)

if (getuid() == 0)
{
printf("pgagroal-cli: Using the root account is not allowed\n");
exit(1);
errx(1, "Using the root account is not allowed");
}

// if the user has specified the host and port
Expand All @@ -234,15 +234,13 @@ main(int argc, char** argv)
// there must be all the other pieces for a remote connection
if ((username != NULL || password != NULL) && !remote_connection)
{
printf("pgagroal-cli: you need also -h and -p options to perform a remote connection\n");
exit(1);
errx(1, "you need also -h and -p options to perform a remote connection");
}

// and she cannot use "local" and "remote" connections at the same time
if (configuration_path != NULL && remote_connection)
{
printf("pgagroal-cli: Use either -c or -h/-p to define endpoint\n");
exit(1);
errx(1, "Use either -c or -h/-p to define endpoint");
}

if (argc <= 1)
Expand All @@ -254,8 +252,7 @@ main(int argc, char** argv)
size = sizeof(struct configuration);
if (pgagroal_create_shared_memory(size, HUGEPAGE_OFF, &shmem))
{
printf("pgagroal-cli: Error creating shared memory\n");
exit(1);
errx(1, "Error creating shared memory");
}
pgagroal_init_configuration(shmem);

Expand All @@ -264,13 +261,11 @@ main(int argc, char** argv)
ret = pgagroal_read_configuration(shmem, configuration_path, false);
if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_NOT_FOUND)
{
printf("pgagroal-cli: Configuration not found: <%s>\n", configuration_path);
exit(1);
errx(1, "Configuration not found: <%s>", configuration_path);
}
else if (ret == PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG)
{
printf("pgagroal-cli: Too many sections in the configuration file <%s>\n", configuration_path);
exit(1);
errx(1, "Too many sections in the configuration file <%s>", configuration_path);
}

if (logfile)
Expand All @@ -284,7 +279,7 @@ main(int argc, char** argv)

if (pgagroal_start_logging())
{
exit(1);
errx(1, "Cannot start the logging subsystem");
}

config = (struct configuration*)shmem;
Expand All @@ -296,8 +291,7 @@ main(int argc, char** argv)
{
if (!remote_connection)
{
printf("pgagroal-cli: Host (-h) and port (-p) must be specified to connect to the remote host\n");
exit(1);
errx(1, "Host (-h) and port (-p) must be specified to connect to the remote host");
}
}
else
Expand All @@ -315,7 +309,7 @@ main(int argc, char** argv)

if (pgagroal_start_logging())
{
exit(1);
errx(1, "Cannot start the logging subsystem");
}

config = (struct configuration*)shmem;
Expand Down Expand Up @@ -456,7 +450,7 @@ main(int argc, char** argv)
/* Remote connection */
if (pgagroal_connect(host, atoi(port), &socket))
{
printf("pgagroal-cli: No route to host: %s:%s\n", host, port);
warnx("No route to host: %s:%s\n", host, port);
goto done;
}

Expand Down Expand Up @@ -511,7 +505,7 @@ main(int argc, char** argv)
/* Authenticate */
if (pgagroal_remote_management_scram_sha256(username, password, socket, &s_ssl) != AUTH_SUCCESS)
{
printf("pgagroal-cli: Bad credentials for %s\n", username);
warnx("Bad credentials for %s\n", username);
goto done;
}
}
Expand Down
47 changes: 24 additions & 23 deletions src/libpgagroal/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#ifdef HAVE_LINUX
#include <systemd/sd-daemon.h>
#endif
Expand Down Expand Up @@ -208,9 +209,9 @@ pgagroal_read_configuration(void* shm, char* filename, bool emitWarnings)
// check we don't overflow the number of available sections
if (idx_sections >= NUMBER_OF_SERVERS + 1)
{
fprintf(stderr, "Max number of sections (%d) in configuration file <%s> reached!\n",
NUMBER_OF_SERVERS + 1,
filename);
warnx("Max number of sections (%d) in configuration file <%s> reached!",
NUMBER_OF_SERVERS + 1,
filename);
return PGAGROAL_CONFIGURATION_STATUS_FILE_TOO_BIG;
}

Expand Down Expand Up @@ -639,20 +640,20 @@ pgagroal_read_configuration(void* shm, char* filename, bool emitWarnings)
// otherwise it is outside of a section at all
if (strlen(section) > 0)
{
fprintf(stderr, "Unknown key <%s> with value <%s> in section [%s] (line %d of file <%s>)\n",
key,
value,
section,
lineno,
filename);
warnx("Unknown key <%s> with value <%s> in section [%s] (line %d of file <%s>)",
key,
value,
section,
lineno,
filename);
}
else
{
fprintf(stderr, "Key <%s> with value <%s> out of any section (line %d of file <%s>)\n",
key,
value,
lineno,
filename);
warnx("Key <%s> with value <%s> out of any section (line %d of file <%s>)",
key,
value,
lineno,
filename);
}
}

Expand All @@ -677,9 +678,9 @@ pgagroal_read_configuration(void* shm, char* filename, bool emitWarnings)
// check there is at least one main section
if (!has_main_section)
{
fprintf(stderr, "No main configuration section [%s] found in file <%s>\n",
PGAGROAL_MAIN_INI_SECTION,
filename);
warnx("No main configuration section [%s] found in file <%s>",
PGAGROAL_MAIN_INI_SECTION,
filename);
return PGAGROAL_CONFIGURATION_STATUS_KO;
}

Expand All @@ -699,12 +700,12 @@ pgagroal_read_configuration(void* shm, char* filename, bool emitWarnings)
if (!strncmp(sections[i].name, sections[j].name, LINE_LENGTH))
{
// cannot log here ...
fprintf(stderr, "%s section [%s] duplicated at lines %d and %d of file <%s>\n",
sections[i].main ? "Main" : "Server",
sections[i].name,
sections[i].lineno,
sections[j].lineno,
filename);
warnx("%s section [%s] duplicated at lines %d and %d of file <%s>",
sections[i].main ? "Main" : "Server",
sections[i].name,
sections[i].lineno,
sections[j].lineno,
filename);
return_value++; // this is an error condition!
}
}
Expand Down
Loading