Skip to content

Commit

Permalink
fix various crashes, e.g., when waking up from rest-mode, and lots of…
Browse files Browse the repository at this point in the history
… cosmetics
  • Loading branch information
john-tornblom committed May 20, 2024
1 parent bf2f6dc commit e5a6369
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 392 deletions.
2 changes: 1 addition & 1 deletion Makefile
4 changes: 3 additions & 1 deletion Makefile.pc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
PYTHON ?= python3

BIN := websrv.pc
SRCS := src/sys_pc.c src/websrv.c src/asset.c src/fs.c src/main.c
SRCS := src/main.c src/websrv.c src/asset.c src/fs.c
SRCS += src/pc/sys.c

LDADD := -lmicrohttpd

ASSETS := $(wildcard assets/*)
Expand Down
8 changes: 4 additions & 4 deletions Makefile.prospero → Makefile.ps5
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ endif
PYTHON ?= python3

BIN := websrv.elf
SRCS := src/sys_prospero.c src/websrv.c src/asset.c src/fs.c src/main.c \
src/pt.c src/elfldr.c src/hbldr.c
CFLAGS := -Wall -Werror
SRCS := src/main.c src/websrv.c src/asset.c src/fs.c
SRCS += src/ps5/sys.c src/ps5/pt.c src/ps5/elfldr.c src/ps5/hbldr.c
CFLAGS := -Wall -Werror -Isrc
LDADD := -lkernel_sys -lmicrohttpd -lSceSystemService -lSceUserService

ASSETS := $(wildcard assets/*)
Expand All @@ -46,7 +46,7 @@ gen/%.c: assets/% gen
$(PYTHON) gen-asset-module.py --path $* $< > $@

$(BIN): $(SRCS) $(GEN_SRCS)
$(CC) -o $@ $^ $(LDADD)
$(CC) $(CFLAGS) -o $@ $^ $(LDADD)


test: $(BIN)
Expand Down
2 changes: 1 addition & 1 deletion src/asset.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef struct asset {

static asset_t* g_asset_head = 0;

#include <stdio.h>

void
asset_register(const char* path, void* data, size_t size) {
asset_t* a = calloc(1, sizeof(asset_t));
Expand Down
35 changes: 14 additions & 21 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,15 @@ dir_read_json(void *cls, uint64_t pos, char *buf, size_t max) {
char path[PATH_MAX];
struct dirent *e;
struct stat st;
mode_t mode;
int res;
char c;

if(max < 512) {
return 0;
}

if(sm->state == 0) {
res = snprintf(buf, max, "[{ \"name\": \".\", \"mode\": \"d\"}");
sm->state++;
return res;
return snprintf(buf, max, "[{ \"name\": \".\", \"mode\": \"d\"}");
}

if(sm->state == 1) {
Expand Down Expand Up @@ -140,9 +137,8 @@ dir_read_json(void *cls, uint64_t pos, char *buf, size_t max) {
}

if(sm->state == 2) {
res = snprintf(buf, max, "]");
sm->state++;
return res;
return snprintf(buf, max, "]");
}

return MHD_CONTENT_READER_END_OF_STREAM;
Expand All @@ -156,26 +152,24 @@ static ssize_t
dir_read_html(void *cls, uint64_t pos, char *buf, size_t max) {
dir_read_sm_t* sm = cls;
struct dirent *e;
int res;

if(max < 512) {
return 0;
}

if(sm->state == 0) {
res = snprintf(buf, max,
"<!DOCTYPE html>" \
"<html>" \
" <head>" \
" <title>Index of %s</title>" \
" </head>" \
" <body>" \
" <h1>Index of %s</h1>" \
" <ul>" \
,
sm->path, sm->path);
sm->state++;
return res;
return snprintf(buf, max,
"<!DOCTYPE html>" \
"<html>" \
" <head>" \
" <title>Index of %s</title>" \
" </head>" \
" <body>" \
" <h1>Index of %s</h1>" \
" <ul>" \
,
sm->path, sm->path);
}

if(sm->state == 1) {
Expand All @@ -191,9 +185,8 @@ dir_read_html(void *cls, uint64_t pos, char *buf, size_t max) {
}

if(sm->state == 2) {
res = snprintf(buf, max, "</ul></body></html>");
sm->state++;
return res;
return snprintf(buf, max, "</ul></body></html>");
}

return MHD_CONTENT_READER_END_OF_STREAM;
Expand Down
131 changes: 2 additions & 129 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,149 +14,22 @@ You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <microhttpd.h>

#include "asset.h"
#include "fs.h"
#include "sys.h"
#include "websrv.h"


/**
* Respond to a launch request.
**/
static enum MHD_Result
launch_request(struct MHD_Connection *conn, const char* url) {
struct MHD_Response *resp;
const char* title_id;
const char *args;
int ret = MHD_NO;
int status;

title_id = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "titleId");
args = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "args");

if(!title_id) {
status = MHD_HTTP_BAD_REQUEST;
} else if(sys_launch_title(title_id, args)) {
status = MHD_HTTP_SERVICE_UNAVAILABLE;
} else {
status = MHD_HTTP_OK;
}

if((resp=MHD_create_response_from_buffer(0, "",
MHD_RESPMEM_PERSISTENT))) {
ret = websrv_queue_response(conn, status, resp);
MHD_destroy_response(resp);
}

return ret;
}


/**
* Respond to a launch request.
**/
static enum MHD_Result
hbldr_request(struct MHD_Connection *conn, const char* url) {
struct MHD_Response *resp;
const char* path;
const char *args;
int ret = MHD_NO;
int status;

path = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "path");
args = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "args");

if(!path) {
status = MHD_HTTP_BAD_REQUEST;
} else if(sys_launch_homebrew(path, args)) {
status = MHD_HTTP_SERVICE_UNAVAILABLE;
} else {
status = MHD_HTTP_OK;
}

if((resp=MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT))) {
ret = websrv_queue_response(conn, status, resp);
MHD_destroy_response(resp);
}

return ret;
}


/**
*
**/
static enum MHD_Result
ahc_echo(void *cls, struct MHD_Connection *conn,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **ptr) {
static int aptr;

if(strcmp(method, MHD_HTTP_METHOD_GET)) {
return MHD_NO;
}

// never respond on first call
if(&aptr != *ptr) {
*ptr = &aptr;
return MHD_YES;
}

// reset when done
*ptr = NULL;

if(!strcmp("/fs", url)) {
return fs_request(conn, url);
}
if(!strncmp("/fs/", url, 4)) {
return fs_request(conn, url);
}

if(!strcmp("/launch", url)) {
return launch_request(conn, url);
}

if(!strcmp("/hbldr", url)) {
return hbldr_request(conn, url);
}

if(!strcmp("/", url) || !url[0]) {
return asset_request(conn, "/index.html");
}

return asset_request(conn, url);
}


int
main() {
const uint16_t port = 8080;
struct MHD_Daemon *d;

printf("Web server was compiled at %s %s\n", __DATE__, __TIME__);

if(!(d=MHD_start_daemon((MHD_USE_THREAD_PER_CONNECTION |
MHD_USE_INTERNAL_POLLING_THREAD |
MHD_USE_ERROR_LOG), port, 0, 0,
&ahc_echo, 0, MHD_OPTION_END))) {
perror("MHD_start_daemon");
return 1;
}

while(1) {
sleep(1);
websrv_listen(port);
sleep(3);
}

MHD_stop_daemon(d);

return 0;
}

6 changes: 2 additions & 4 deletions src/sys_pc.c → src/pc/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int
sys_launch_title(const char* title_id, char* args) {
printf("launch title: %s %s", title_id, args);
printf("launch title: %s %s\n", title_id, args);
return 0;
}


int
sys_launch_homebrew(const char* path, char* args) {
printf("launch homebrew: %s %s", path, args);
printf("launch homebrew: %s %s\n", path, args);
return 0;
}
Loading

0 comments on commit e5a6369

Please sign in to comment.