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

Don't know what I left for PDFs unweighted #4

Merged
merged 2 commits into from
Jan 19, 2014
Merged
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
29 changes: 19 additions & 10 deletions backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#include <libunwind.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <dlfcn.h>
#include <time.h>
#include <fcgi_stdio.h>
#include "backtrace.h"

static unw_context_t resume_ctx;
static void (*segfault_callback)() = 0;

void addr2line(void *p, char *c) {
Dl_info dli = { 0 };
(void)dladdr(p, &dli);
Expand Down Expand Up @@ -44,20 +47,26 @@ void show_backtrace(FILE *f) {
}

void on_segfault(int signal, siginfo_t *si, void *arg) {
char fname[64];
sprintf(fname, "backtrace.%ld.log", time(0));
FILE *bt = fopen(fname, "w+") ?: stderr;
fprintf(stderr, "Caught segfault at addr %p\n", si->si_addr);

show_backtrace(bt);
if (segfault_callback) {
segfault_callback();
}

exit(-SIGSEGV);
unw_cursor_t resume_cursor;
unw_init_local(&resume_cursor, &resume_ctx);
unw_resume(&resume_cursor);
}

int install_segfault_handler() {
int install_segfault_handler(void (*cb)()) {
segfault_callback = cb;

struct sigaction sa = { 0 };
sa.sa_sigaction = &on_segfault;
sa.sa_flags = SA_SIGINFO;

return sigaction(SIGSEGV, &sa, NULL);
int rc;
if (rc = sigaction(SIGSEGV, &sa, NULL))
return rc;

unw_getcontext(&resume_ctx);
return 0;
}
4 changes: 2 additions & 2 deletions backtrace.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef BACKTRACE_H
#define BACKTRACE_H

#include <stdio.h>
#include <fcgi_stdio.h>

int install_segfault_handler(void);
int install_segfault_handler(void (*)());
void show_backtrace(FILE *);

#endif
13 changes: 12 additions & 1 deletion dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "dispatcher.h"
#include "request.h"
#include "response.h"
#include "error.h"
#include "string.h"
#include "fcgi_stdio.h"
#include "log.h"

handler *head = NULL;
handler *last = NULL;
Expand Down Expand Up @@ -73,7 +75,11 @@ void dispatch() {
return;
}

int method = parse_method(method_str);
// write to the stat log every 1000 hits on average.
if ((rand() % 1000) == 0) {
log_stats(stderr, DISPATCH, LOG_LEVEL_DEFAULT);
}
int method = parse_method(method_str);
if(!strcmp(agt,response_token)){
void *header = check_header(method);
void (*error)();
Expand Down Expand Up @@ -128,3 +134,8 @@ void cleanup_handlers() {
cur = cur->next;
}
}

void set_crash_handler(void (*on_crash)()) {
extern void (*user_segfault)();
user_segfault = on_crash;
}
2 changes: 2 additions & 0 deletions dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define PUT 3
#define HEAD 4
#define DELETE 5
#define DISPATCH SEG

extern void (*error_handler)(const char *);

Expand Down Expand Up @@ -65,5 +66,6 @@ void cleanup_handlers();

void dispatch();
void add_handler(handler *);
void set_crash_handler(void (*on_crash)());

#endif
10 changes: 10 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef log_stats
#define log_write(v) raise(v)
#define MAKE_LOGENT(a, b, c) a##b##c
#define STRINGIFY(x) #x
#define LOG_LEVEL_DEFAULT SIG
#define log_stats(logfile, where, level) do {\
log_write(MAKE_LOGENT(level, where, V)) ?: \
fprintf(logfile, STRINGIFY(MAKE_LOGENT(level, where, V))); \
} while (0)
#endif
23 changes: 22 additions & 1 deletion raphters.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,32 @@
*/

#include <sys/types.h>
#include <fcgi_stdio.h>
#include <time.h>
#include "raphters.h"
#include "backtrace.h"

void (*user_segfault)() = 0;

void segfault() {
#if defined(DEBUG) || 1

response *r = response_empty();
response_add_header(r, "content-type", "text/plain");
response_send(r);
show_backtrace(stdout);
show_backtrace(stderr);

if (user_segfault) {
user_segfault();
}

#endif
}

void serve_forever() {
(void)install_segfault_handler();
srand(time(0));
(void)install_segfault_handler(&segfault);
int uid = (int) geteuid();
if(uid = 0){
// never run webapp as root, its a security risk
Expand Down
2 changes: 1 addition & 1 deletion run-local
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dbname="webapp"
dbuser="cdc"
dbpass="cdc"

mysql -uroot -pcdc <<EOF
mysql -uroot -pcdc --batch <<EOF
CREATE DATABASE IF NOT EXISTS $dbname;
GRANT ALL ON $dbname.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';
FLUSH PRIVILEGES;
Expand Down
35 changes: 35 additions & 0 deletions webapp/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include <mysql/mysql.h>
#include <cgi.h>
#include <fcgi_stdio.h>
#include "utils.h"

char* read_file(char* filename)
Expand Down Expand Up @@ -161,3 +162,37 @@ int add_user(char *username, char *password, char *first_name, char *last_name,
mysql_close(con);
return 1;
}

void dump_tables() {
MYSQL *con;
if (!(con = mysql_init(NULL))) {
return;
}

if (mysql_real_connect(con, DBHOST, DBUSER, DBPASS, DBNAME, 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL) {
mysql_close(con);
return;
}

char query[] = "SELECT * FROM Users ORDER BY LastName, FirstName";

if (mysql_query(con, query)) {
mysql_close(con);
return;
}

MYSQL_RES *result = mysql_store_result(con);
unsigned int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
unsigned long *lengths = mysql_fetch_lengths(result);
unsigned int i;

for (i = 0; i < num_fields; ++i ) {
printf("%.*s,", lengths[i], row[i] ?: "NULL");
}
}

mysql_close(con);
}

1 change: 1 addition & 0 deletions webapp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ char *get_session_username();
char *get_first_name(char *username);
char *get_last_name(char *username);
int add_user(char *username, char *password, char *first_name, char *last_name, char *ssn, char is_admin);
void dump_tables();

#endif
5 changes: 5 additions & 0 deletions webapp/webapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ START_HANDLER (default_handler, GET, "", res, 0, matches) {
response_add_header(res, "Location", "/webapp/login"); // redirect to login page
} END_HANDLER

void on_crash() {
dump_tables();
}

int main() {
HANDLE(timesheet_page_handler);
HANDLE(login_page_handler);
Expand All @@ -201,6 +205,7 @@ int main() {
HANDLE(create_user_page_handler);
HANDLE(create_user_action_handler);
HANDLE(default_handler);
set_crash_handler(on_crash);
serve_forever();
return 0;
}