1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 20:17:45 +02:00

fix potential race conditions

This commit is contained in:
Jfreegman 2014-07-09 00:05:13 -04:00
parent f98c77432b
commit 1ce731471d
9 changed files with 31 additions and 27 deletions

View File

@ -253,7 +253,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
/* holds the lone filename */ /* holds the lone filename */
char filename_nopath[MAX_STR_SIZE]; char filename_nopath[MAX_STR_SIZE];
get_file_name(filename_nopath, pathname); get_file_name(filename_nopath, sizeof(filename_nopath), pathname);
int len = strlen(filename_nopath); int len = strlen(filename_nopath);
snprintf(msg, sizeof(msg), "File transfer request for '%s' (%llu bytes).", filename_nopath, snprintf(msg, sizeof(msg), "File transfer request for '%s' (%llu bytes).", filename_nopath,

View File

@ -193,7 +193,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
fseek(file_to_send, 0, SEEK_SET); fseek(file_to_send, 0, SEEK_SET);
char filename[MAX_STR_SIZE]; char filename[MAX_STR_SIZE];
get_file_name(filename, path); get_file_name(filename, sizeof(filename), path);
int filenum = tox_new_file_sender(m, self->num, filesize, (const uint8_t *) filename, strlen(filename)); int filenum = tox_new_file_sender(m, self->num, filesize, (const uint8_t *) filename, strlen(filename));
if (filenum == -1) { if (filenum == -1) {

View File

@ -45,6 +45,8 @@
#define TOX_DNS3_TXT_PREFIX "v=tox3;id=" #define TOX_DNS3_TXT_PREFIX "v=tox3;id="
#define DNS3_KEY_SZ 32 #define DNS3_KEY_SZ 32
extern struct _Winthread Winthread;
/* TODO: process keys from key file instead of hard-coding like a noob */ /* TODO: process keys from key file instead of hard-coding like a noob */
static struct dns3_server { static struct dns3_server {
char *name; char *name;
@ -77,7 +79,6 @@ static struct _thread_data {
static struct _dns_thread { static struct _dns_thread {
pthread_t tid; pthread_t tid;
pthread_mutex_t lock;
} dns_thread; } dns_thread;
@ -86,9 +87,9 @@ static int dns_error(ToxWindow *self, char *errmsg)
char msg[MAX_STR_SIZE]; char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "User lookup failed: %s", errmsg); snprintf(msg, sizeof(msg), "User lookup failed: %s", errmsg);
pthread_mutex_lock(&dns_thread.lock); pthread_mutex_lock(&Winthread.lock);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
pthread_mutex_unlock(&dns_thread.lock); pthread_mutex_unlock(&Winthread.lock);
return -1; return -1;
} }
@ -272,15 +273,15 @@ void *dns3_lookup_thread(void *data)
memcpy(encrypted_id, ans_id + prfx_len, ans_len - prfx_len); memcpy(encrypted_id, ans_id + prfx_len, ans_len - prfx_len);
if (tox_decrypt_dns3_TXT(dns_obj, (uint8_t *) t_data.id_bin, (uint8_t *) encrypted_id, strlen(encrypted_id), if (tox_decrypt_dns3_TXT(dns_obj, (uint8_t *) t_data.id_bin, (uint8_t *) encrypted_id,
request_id) == -1) { strlen(encrypted_id), request_id) == -1) {
dns_error(self, "Core failed to decrypt DNS response."); dns_error(self, "Core failed to decrypt DNS response.");
kill_dns_thread(dns_obj); kill_dns_thread(dns_obj);
} }
pthread_mutex_lock(&dns_thread.lock); pthread_mutex_lock(&Winthread.lock);
cmd_add_helper(self, t_data.m, t_data.id_bin, t_data.msg); cmd_add_helper(self, t_data.m, t_data.id_bin, t_data.msg);
pthread_mutex_unlock(&dns_thread.lock); pthread_mutex_unlock(&Winthread.lock);
kill_dns_thread(dns_obj); kill_dns_thread(dns_obj);
return 0; return 0;
@ -302,9 +303,10 @@ void dns3_lookup(ToxWindow *self, Tox *m, char *id_bin, char *addr, char *msg)
t_data.m = m; t_data.m = m;
t_data.busy = 1; t_data.busy = 1;
pthread_mutex_unlock(&Winthread.lock);
if (pthread_create(&dns_thread.tid, NULL, dns3_lookup_thread, NULL) != 0) if (pthread_create(&dns_thread.tid, NULL, dns3_lookup_thread, NULL) != 0)
exit_toxic_err("failed in dns3_lookup", FATALERR_THREAD_CREATE); exit_toxic_err("failed in dns3_lookup", FATALERR_THREAD_CREATE);
if (pthread_mutex_init(&dns_thread.lock, NULL) != 0) pthread_mutex_lock(&Winthread.lock);
exit_toxic_err("failed in dns3_lookup", FATALERR_MUTEX_INIT);
} }

View File

@ -194,7 +194,7 @@ int valid_nick(char *nick)
} }
/* gets base file name from path or original file name if no path is supplied */ /* gets base file name from path or original file name if no path is supplied */
void get_file_name(char *namebuf, const char *pathname) void get_file_name(char *namebuf, int bufsize, const char *pathname)
{ {
int idx = strlen(pathname) - 1; int idx = strlen(pathname) - 1;
@ -213,7 +213,7 @@ void get_file_name(char *namebuf, const char *pathname)
filename = tmpname; filename = tmpname;
} }
snprintf(namebuf, sizeof(namebuf), "%s", filename); snprintf(namebuf, bufsize, "%s", filename);
} }
/* converts str to all lowercase */ /* converts str to all lowercase */

View File

@ -80,7 +80,7 @@ int qsort_strcasecmp_hlpr(const void *nick1, const void *nick2);
int valid_nick(char *nick); int valid_nick(char *nick);
/* gets base file name from path or original file name if no path is supplied */ /* gets base file name from path or original file name if no path is supplied */
void get_file_name(char *namebuf, const char *pathname); void get_file_name(char *namebuf, int bufsize, const char *pathname);
/* converts str to all lowercase */ /* converts str to all lowercase */
void str_to_lower(char *str); void str_to_lower(char *str);

View File

@ -259,7 +259,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
wprintw(statusbar->topline, " %s", statusbar->nick); wprintw(statusbar->topline, " %s", statusbar->nick);
wattroff(statusbar->topline, A_BOLD); wattroff(statusbar->topline, A_BOLD);
} else { } else {
wprintw(statusbar->topline, "[Offline]"); wprintw(statusbar->topline, " [Offline]");
wattron(statusbar->topline, A_BOLD); wattron(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, " %s ", statusbar->nick); wprintw(statusbar->topline, " %s ", statusbar->nick);
wattroff(statusbar->topline, A_BOLD); wattroff(statusbar->topline, A_BOLD);
@ -368,7 +368,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m)
const char *toxic_ver = strtok(ver, "_"); const char *toxic_ver = strtok(ver, "_");
if ( (!strcmp("Online", statusmsg) || !strncmp("Toxing on Toxic", statusmsg, 15)) && toxic_ver != NULL) { if ( (!strcmp("Online", statusmsg) || !strncmp("Toxing on Toxic", statusmsg, 15)) && toxic_ver != NULL) {
snprintf(statusmsg, sizeof(statusmsg), "Toxing on Toxic v.%s", toxic_ver); snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver);
s_len = strlen(statusmsg); s_len = strlen(statusmsg);
statusmsg[s_len] = '\0'; statusmsg[s_len] = '\0';
} }

View File

@ -80,10 +80,9 @@ struct arg_opts {
struct _Winthread Winthread; struct _Winthread Winthread;
struct user_settings *user_settings = NULL; struct user_settings *user_settings = NULL;
static bool sig_exit_toxic = false;
static void catch_SIGINT(int sig) static void catch_SIGINT(int sig)
{ {
sig_exit_toxic = true; Winthread.sig_exit_toxic = true;
} }
static void flag_window_resize(int sig) static void flag_window_resize(int sig)
@ -459,9 +458,8 @@ static void do_toxic(Tox *m, ToxWindow *prompt)
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
do_connection(m, prompt); do_connection(m, prompt);
do_file_senders(m); do_file_senders(m);
pthread_mutex_unlock(&Winthread.lock);
tox_do(m); /* main tox-core loop */ tox_do(m); /* main tox-core loop */
pthread_mutex_unlock(&Winthread.lock);
} }
#define INACTIVE_WIN_REFRESH_RATE 10 #define INACTIVE_WIN_REFRESH_RATE 10
@ -482,6 +480,11 @@ void *thread_winref(void *data)
refresh_inactive_windows(); refresh_inactive_windows();
draw_count = 0; draw_count = 0;
} }
if (Winthread.sig_exit_toxic) {
pthread_mutex_lock(&Winthread.lock);
exit_toxic_success(m);
}
} }
} }
@ -648,13 +651,13 @@ int main(int argc, char *argv[])
uint64_t cur_time = get_unix_time(); uint64_t cur_time = get_unix_time();
if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) { if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) {
pthread_mutex_lock(&Winthread.lock);
store_data(m, DATA_FILE); store_data(m, DATA_FILE);
pthread_mutex_unlock(&Winthread.lock);
last_save = cur_time; last_save = cur_time;
} }
if (sig_exit_toxic)
exit_toxic_success(m);
usleep(40000); usleep(40000);
} }

View File

@ -105,8 +105,7 @@ void on_nickchange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t
windows[i].onNickChange(&windows[i], m, friendnumber, (const char *) string, length); windows[i].onNickChange(&windows[i], m, friendnumber, (const char *) string, length);
} }
if (store_data(m, DATA_FILE)) store_data(m, DATA_FILE);
wprintw(prompt->window, "\nCould not store Tox data\n");
} }
void on_statusmessagechange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t length, void *userdata) void on_statusmessagechange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t length, void *userdata)
@ -138,8 +137,7 @@ void on_friendadded(Tox *m, int32_t friendnumber, bool sort)
windows[i].onFriendAdded(&windows[i], m, friendnumber, sort); windows[i].onFriendAdded(&windows[i], m, friendnumber, sort);
} }
if (store_data(m, DATA_FILE)) store_data(m, DATA_FILE);
wprintw(prompt->window, "\nCould not store Tox data\n");
} }
void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length, void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length,

View File

@ -66,6 +66,7 @@ enum {
struct _Winthread { struct _Winthread {
pthread_t tid; pthread_t tid;
pthread_mutex_t lock; pthread_mutex_t lock;
bool sig_exit_toxic;
bool flag_resize; bool flag_resize;
}; };