[17] Sort include_dir files before loading them.

Bug: https://github.com/eclipse/mosquitto/issues/17
This commit is contained in:
Roger A. Light 2017-02-19 20:29:04 +00:00
parent 93464179f1
commit f72d9b3a34
5 changed files with 179 additions and 4 deletions

View File

@ -36,6 +36,8 @@ Broker:
- queue_qos0_messages was not observing max_queued_** limits
- Add support bridges to be configured to only send notifications to the local
broker.
- When using the include_dir configuration option sort the files
alphabetically before loading them. Closes #17.
Client library:
- Outgoing messages with QoS>1 are no longer retried after a timeout period.

View File

@ -4,6 +4,7 @@ include_directories(${mosquitto_SOURCE_DIR} ${mosquitto_SOURCE_DIR}/src
set (MOSQ_SRCS
conf.c
conf_includedir.c
context.c
database.c
handle_connack.c

View File

@ -11,6 +11,7 @@ endif
OBJS= mosquitto.o \
bridge.o \
conf.o \
conf_includedir.o \
context.o \
database.o \
handle_connack.o \
@ -66,6 +67,9 @@ bridge.o : bridge.c mosquitto_broker_internal.h
conf.o : conf.c mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@
conf_includedir.o : conf_includedir.c mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@
context.o : context.c mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CFLAGS) -c $< -o $@

167
src/conf_includedir.c Normal file
View File

@ -0,0 +1,167 @@
/*
Copyright (c) 2009-2016 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include <config.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef WIN32
#else
# include <dirent.h>
#endif
#ifndef WIN32
# include <netdb.h>
# include <sys/socket.h>
#else
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
#if !defined(WIN32) && !defined(__CYGWIN__)
# include <sys/syslog.h>
#endif
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "tls_mosq.h"
#include "util_mosq.h"
#include "mqtt3_protocol.h"
int strcasecmp_p(const void *p1, const void *p2)
{
return strcasecmp(*(const char **)p1, *(const char **)p2);
}
#ifdef WIN32
int config__get_dir_files(const char *include_dir, char ***files, int *file_count)
{
char **l_files = NULL;
int l_file_count = 0;
char **files_tmp;
HANDLE fh;
char dirpath[MAX_PATH];
WIN32_FIND_DATA find_data;
snprintf(dirpath, MAX_PATH, "%s\\*.conf", include_dir);
fh = FindFirstFile(dirpath, &find_data);
if(fh == INVALID_HANDLE_VALUE){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open include_dir '%s'.", include_dir);
return 1;
}
do{
len = strlen(include_dir)+1+strlen(find_data.cFileName)+1;
l_file_count++;
files_tmp = mosquitto__realloc(l_files, l_file_count*sizeof(char *));
if(!files_tmp){
for(i=0; i<l_file_count-1; i++){
mosquitto__free(l_files[i]);
}
mosquitto__free(l_files);
closedir(dh);
return MOSQ_ERR_NOMEM;
}
l_files = files_tmp;
l_files[l_file_count-1] = mosquitto__malloc(len+1);
if(!l_files[l_file_count-1]){
for(i=0; i<l_file_count-1; i++){
mosquitto__free(l_files[i]);
}
mosquitto__free(l_files);
closedir(dh);
return MOSQ_ERR_NOMEM;
}
snprintf(l_files[l_file_count-1], len, "%s/%s", include_dir, find_data.cFileName);
l_files[l_file_count-1][len] = '\0';
}while(FindNextFile(fh, &find_data));
FindClose(fh);
*files = l_files;
*file_count = l_file_count;
return 0;
}
#endif
#ifndef WIN32
int config__get_dir_files(const char *include_dir, char ***files, int *file_count)
{
char **l_files = NULL;
int l_file_count = 0;
char **files_tmp;
int len;
int i;
DIR *dh;
struct dirent *de;
dh = opendir(include_dir);
if(!dh){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open include_dir '%s'.", include_dir);
return 1;
}
while((de = readdir(dh)) != NULL){
if(strlen(de->d_name) > 5){
if(!strcmp(&de->d_name[strlen(de->d_name)-5], ".conf")){
len = strlen(include_dir)+1+strlen(de->d_name)+1;
l_file_count++;
files_tmp = mosquitto__realloc(l_files, l_file_count*sizeof(char *));
if(!files_tmp){
for(i=0; i<l_file_count-1; i++){
mosquitto__free(l_files[i]);
}
mosquitto__free(l_files);
closedir(dh);
return MOSQ_ERR_NOMEM;
}
l_files = files_tmp;
l_files[l_file_count-1] = mosquitto__malloc(len+1);
if(!l_files[l_file_count-1]){
for(i=0; i<l_file_count-1; i++){
mosquitto__free(l_files[i]);
}
mosquitto__free(l_files);
closedir(dh);
return MOSQ_ERR_NOMEM;
}
snprintf(l_files[l_file_count-1], len, "%s/%s", include_dir, de->d_name);
l_files[l_file_count-1][len] = '\0';
}
}
}
closedir(dh);
*files = l_files;
*file_count = l_file_count;
return 0;
}
#endif

View File

@ -477,6 +477,7 @@ int config__parse_args(struct mosquitto__config *config, int argc, char *argv[])
int config__read(struct mosquitto__config *config, bool reload);
/* Free all config data. */
void config__cleanup(struct mosquitto__config *config);
int config__get_dir_files(const char *include_dir, char ***files, int *file_count);
int drop_privileges(struct mosquitto__config *config, bool temporary);
int restore_privileges(void);