Skip to content

Commit

Permalink
Don't use gnu-specific strerror_r.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Nov 7, 2018
1 parent 34c752a commit ba67e1f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
6 changes: 5 additions & 1 deletion client/client_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,11 @@ int client_id_generate(struct mosq_config *cfg, const char *id_base)

int client_connect(struct mosquitto *mosq, struct mosq_config *cfg)
{
#ifndef WIN32
char *err;
#else
char err[1024];
#endif
int rc;
int port;

Expand Down Expand Up @@ -1008,7 +1012,7 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg)
if(!cfg->quiet){
if(rc == MOSQ_ERR_ERRNO){
#ifndef WIN32
strerror_r(errno, err, 1024);
err = strerror(errno);
#else
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL);
#endif
Expand Down
20 changes: 10 additions & 10 deletions src/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int drop_privileges(struct mosquitto__config *config, bool temporary)
{
#if !defined(__CYGWIN__) && !defined(WIN32)
struct passwd *pwd;
char err[256];
char *err;
int rc;

const char *snap = getenv("SNAP_NAME");
Expand All @@ -108,7 +108,7 @@ int drop_privileges(struct mosquitto__config *config, bool temporary)
return 1;
}
if(initgroups(config->user, pwd->pw_gid) == -1){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error setting groups whilst dropping privileges: %s.", err);
return 1;
}
Expand All @@ -118,7 +118,7 @@ int drop_privileges(struct mosquitto__config *config, bool temporary)
rc = setgid(pwd->pw_gid);
}
if(rc == -1){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error setting gid whilst dropping privileges: %s.", err);
return 1;
}
Expand All @@ -128,7 +128,7 @@ int drop_privileges(struct mosquitto__config *config, bool temporary)
rc = setuid(pwd->pw_uid);
}
if(rc == -1){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error setting uid whilst dropping privileges: %s.", err);
return 1;
}
Expand All @@ -144,19 +144,19 @@ int drop_privileges(struct mosquitto__config *config, bool temporary)
int restore_privileges(void)
{
#if !defined(__CYGWIN__) && !defined(WIN32)
char err[256];
char *err;
int rc;

if(getuid() == 0){
rc = setegid(0);
if(rc == -1){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error setting gid whilst restoring privileges: %s.", err);
return 1;
}
rc = seteuid(0);
if(rc == -1){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error setting uid whilst restoring privileges: %s.", err);
return 1;
}
Expand All @@ -169,20 +169,20 @@ int restore_privileges(void)
void mosquitto__daemonise(void)
{
#ifndef WIN32
char err[256];
char *err;
pid_t pid;

pid = fork();
if(pid < 0){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error in fork: %s", err);
exit(1);
}
if(pid > 0){
exit(0);
}
if(setsid() < 0){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error in setsid: %s", err);
exit(1);
}
Expand Down
6 changes: 2 additions & 4 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,16 @@ void net__broker_cleanup(void)

static void net__print_error(int log, const char *format_str)
{
#ifdef WIN32
char *buf;

#ifdef WIN32
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, WSAGetLastError(), LANG_NEUTRAL, &buf, 0, NULL);

log__printf(NULL, log, format_str, buf);
LocalFree(buf);
#else
char buf[256];

strerror_r(errno, buf, 256);
buf = strerror(errno);
log__printf(NULL, log, format_str, buf);
#endif
}
Expand Down
24 changes: 12 additions & 12 deletions src/persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ int persist__backup(struct mosquitto_db *db, bool shutdown)
uint32_t i32temp;
uint16_t i16temp;
uint8_t i8temp;
char err[256];
char *err;
char *outfile = NULL;
int len;

Expand Down Expand Up @@ -477,7 +477,7 @@ int persist__backup(struct mosquitto_db *db, bool shutdown)
return rc;
error:
mosquitto__free(outfile);
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
if(db_fptr) fclose(db_fptr);
return 1;
Expand Down Expand Up @@ -596,7 +596,7 @@ static int persist__client_msg_chunk_restore(struct mosquitto_db *db, FILE *db_f
uint8_t qos, retain, direction, state, dup;
char *client_id = NULL;
int rc;
char err[256];
char *err;

read_e(db_fptr, &i16temp, sizeof(uint16_t));
slen = ntohs(i16temp);
Expand Down Expand Up @@ -631,7 +631,7 @@ static int persist__client_msg_chunk_restore(struct mosquitto_db *db, FILE *db_f

return rc;
error:
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
fclose(db_fptr);
mosquitto__free(client_id);
Expand All @@ -650,7 +650,7 @@ static int persist__msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fp
int rc = 0;
struct mosquitto_msg_store *stored = NULL;
struct mosquitto_msg_store_load *load;
char err[256];
char *err;

payload.ptr = NULL;

Expand Down Expand Up @@ -734,7 +734,7 @@ static int persist__msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fp
return rc;
}
error:
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
fclose(db_fptr);
mosquitto__free(source_id);
Expand All @@ -747,10 +747,10 @@ static int persist__retain_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
{
dbid_t i64temp, store_id;
struct mosquitto_msg_store_load *load;
char err[256];
char *err;

if(fread(&i64temp, sizeof(dbid_t), 1, db_fptr) != 1){
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
fclose(db_fptr);
return 1;
Expand All @@ -773,7 +773,7 @@ static int persist__sub_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
char *client_id;
char *topic;
int rc = 0;
char err[256];
char *err;

read_e(db_fptr, &i16temp, sizeof(uint16_t));
slen = ntohs(i16temp);
Expand Down Expand Up @@ -807,7 +807,7 @@ static int persist__sub_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)

return rc;
error:
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
fclose(db_fptr);
return 1;
Expand All @@ -824,7 +824,7 @@ int persist__restore(struct mosquitto_db *db)
uint16_t i16temp, chunk;
uint8_t i8temp;
ssize_t rlen;
char err[256];
char *err;
struct mosquitto_msg_store_load *load, *load_tmp;

assert(db);
Expand Down Expand Up @@ -919,7 +919,7 @@ int persist__restore(struct mosquitto_db *db)
}
return rc;
error:
strerror_r(errno, err, 256);
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
if(fptr) fclose(fptr);
return 1;
Expand Down

0 comments on commit ba67e1f

Please sign in to comment.