[484cd8d] | 1 | /********************************************************************** |
---|
| 2 | * |
---|
| 3 | * rtems shttpd management |
---|
| 4 | * |
---|
| 5 | * FILE NAME : rtems_shttpd.c |
---|
| 6 | * |
---|
| 7 | * AUTHOR : Steven Johnson |
---|
| 8 | * |
---|
| 9 | * DESCRIPTION : Defines the interface functions to the shttp daemon |
---|
| 10 | * |
---|
| 11 | * REVISION : $Id$ |
---|
| 12 | * |
---|
| 13 | * COMMENTS : |
---|
| 14 | * |
---|
| 15 | **********************************************************************/ |
---|
| 16 | |
---|
| 17 | /********************************************************************** |
---|
| 18 | * INCLUDED MODULES |
---|
| 19 | **********************************************************************/ |
---|
| 20 | #include <rtems.h> |
---|
| 21 | #include "defs.h" |
---|
| 22 | |
---|
| 23 | #define MAX_WEB_BASE_PATH_LENGTH 256 |
---|
| 24 | #define MIN_SHTTPD_STACK (8*1024) |
---|
| 25 | |
---|
| 26 | typedef struct RTEMS_HTTPD_ARGS { |
---|
| 27 | rtems_shttpd_init init_callback; |
---|
| 28 | rtems_shttpd_addpages addpages_callback; |
---|
[83ca6077] | 29 | unsigned int port; |
---|
[484cd8d] | 30 | char webroot[MAX_WEB_BASE_PATH_LENGTH]; |
---|
| 31 | } RTEMS_HTTPD_ARGS; |
---|
| 32 | |
---|
| 33 | static int rtems_webserver_running = FALSE; //not running. |
---|
| 34 | |
---|
[83ca6077] | 35 | static rtems_task rtems_httpd_daemon(rtems_task_argument args) |
---|
[484cd8d] | 36 | { |
---|
| 37 | RTEMS_HTTPD_ARGS *httpd_args = (RTEMS_HTTPD_ARGS*)args; |
---|
| 38 | |
---|
| 39 | struct shttpd_ctx *ctx; |
---|
| 40 | |
---|
| 41 | if (httpd_args != NULL) |
---|
| 42 | if (httpd_args->init_callback != NULL) |
---|
| 43 | httpd_args->init_callback(); |
---|
| 44 | |
---|
| 45 | /************************************** |
---|
| 46 | * Initialize the web server |
---|
| 47 | */ |
---|
| 48 | /* |
---|
| 49 | * Initialize SHTTPD context. |
---|
| 50 | * Set WWW root to current WEB_ROOT_PATH. |
---|
| 51 | */ |
---|
| 52 | ctx = shttpd_init(NULL, "document_root", httpd_args->webroot, NULL); |
---|
| 53 | |
---|
| 54 | if (httpd_args != NULL) |
---|
| 55 | if (httpd_args->addpages_callback != NULL) |
---|
| 56 | httpd_args->addpages_callback(ctx); |
---|
| 57 | |
---|
| 58 | /* Finished with args, so free them */ |
---|
| 59 | if (httpd_args != NULL) |
---|
| 60 | free(httpd_args); |
---|
| 61 | |
---|
| 62 | /* Open listening socket */ |
---|
[83ca6077] | 63 | shttpd_listen(ctx, httpd_args->port); |
---|
[484cd8d] | 64 | |
---|
| 65 | rtems_webserver_running = TRUE; |
---|
| 66 | |
---|
| 67 | /* Serve connections infinitely until someone kills us */ |
---|
| 68 | while (rtems_webserver_running) |
---|
| 69 | shttpd_poll(ctx, 1000); |
---|
| 70 | |
---|
| 71 | /* Unreached, because we will be killed by a signal */ |
---|
| 72 | shttpd_fini(ctx); |
---|
| 73 | |
---|
| 74 | rtems_task_delete( RTEMS_SELF ); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | rtems_status_code rtems_initialize_webserver(rtems_task_priority initial_priority, |
---|
[4847f6b1] | 78 | size_t stack_size, |
---|
[484cd8d] | 79 | rtems_mode initial_modes, |
---|
| 80 | rtems_attribute attribute_set, |
---|
| 81 | rtems_shttpd_init init_callback, |
---|
| 82 | rtems_shttpd_addpages addpages_callback, |
---|
[83ca6077] | 83 | char *webroot, |
---|
| 84 | unsigned int port |
---|
[484cd8d] | 85 | ) |
---|
| 86 | { |
---|
| 87 | rtems_status_code sc; |
---|
| 88 | rtems_id tid; |
---|
| 89 | RTEMS_HTTPD_ARGS *args; |
---|
| 90 | |
---|
| 91 | if (stack_size < MIN_SHTTPD_STACK) |
---|
| 92 | stack_size = MIN_SHTTPD_STACK; |
---|
| 93 | |
---|
| 94 | args = malloc(sizeof(RTEMS_HTTPD_ARGS)); |
---|
| 95 | |
---|
| 96 | if (args != NULL) |
---|
| 97 | { |
---|
| 98 | args->init_callback = init_callback; |
---|
| 99 | args->addpages_callback = addpages_callback; |
---|
[83ca6077] | 100 | args->port = port; |
---|
[484cd8d] | 101 | strncpy(args->webroot,webroot,MAX_WEB_BASE_PATH_LENGTH); |
---|
| 102 | |
---|
| 103 | sc = rtems_task_create(rtems_build_name('H', 'T', 'P', 'D'), |
---|
| 104 | initial_priority, |
---|
| 105 | stack_size, |
---|
| 106 | initial_modes, |
---|
| 107 | attribute_set, |
---|
| 108 | &tid); |
---|
| 109 | |
---|
| 110 | if (sc == RTEMS_SUCCESSFUL) |
---|
| 111 | { |
---|
| 112 | sc = rtems_task_start(tid, rtems_httpd_daemon, (rtems_task_argument)args); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | sc = RTEMS_NO_MEMORY; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | return sc; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void rtems_terminate_webserver(void) |
---|
| 124 | { |
---|
[83ca6077] | 125 | rtems_webserver_running = FALSE; // not running, so terminate |
---|
[484cd8d] | 126 | } |
---|
| 127 | |
---|
| 128 | int rtems_webserver_ok(void) |
---|
| 129 | { |
---|
| 130 | return rtems_webserver_running; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void |
---|
| 134 | set_close_on_exec(int fd) |
---|
| 135 | { |
---|
| 136 | // RTEMS Does not have a functional "execve" |
---|
| 137 | // so technically this call does not do anything, |
---|
| 138 | // but it doesnt hurt either. |
---|
| 139 | (void) fcntl(fd, F_SETFD, FD_CLOEXEC); |
---|
| 140 | } |
---|
[bccb841] | 141 | #ifndef __rtems__ |
---|
[484cd8d] | 142 | int |
---|
| 143 | my_stat(const char *path, struct stat *stp) |
---|
| 144 | { |
---|
| 145 | return (stat(path, stp)); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | int |
---|
| 149 | my_open(const char *path, int flags, int mode) |
---|
| 150 | { |
---|
| 151 | return (open(path, flags, mode)); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | int |
---|
| 155 | my_remove(const char *path) |
---|
| 156 | { |
---|
| 157 | return (remove(path)); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | int |
---|
| 161 | my_rename(const char *path1, const char *path2) |
---|
| 162 | { |
---|
| 163 | return (rename(path1, path2)); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | int |
---|
| 167 | my_mkdir(const char *path, int mode) |
---|
| 168 | { |
---|
| 169 | return (mkdir(path, mode)); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | char * |
---|
| 173 | my_getcwd(char *buffer, int maxlen) |
---|
| 174 | { |
---|
| 175 | return (getcwd(buffer, maxlen)); |
---|
| 176 | } |
---|
[bccb841] | 177 | #endif |
---|
[484cd8d] | 178 | int |
---|
| 179 | set_non_blocking_mode(int fd) |
---|
| 180 | { |
---|
| 181 | int ret = -1; |
---|
| 182 | int flags; |
---|
| 183 | |
---|
| 184 | if ((flags = fcntl(fd, F_GETFL, 0)) == -1) { |
---|
| 185 | DBG(("nonblock: fcntl(F_GETFL): %d", ERRNO)); |
---|
| 186 | } else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
---|
| 187 | DBG(("nonblock: fcntl(F_SETFL): %d", ERRNO)); |
---|
| 188 | } else { |
---|
| 189 | ret = 0; /* Success */ |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | return (ret); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | #if !defined(NO_CGI) |
---|
| 196 | int |
---|
| 197 | spawn_process(struct conn *c, const char *prog, char *envblk, char **envp) |
---|
| 198 | { |
---|
| 199 | return (-1); // RTEMS does not have subprocess support as standard. |
---|
| 200 | } |
---|
| 201 | #endif |
---|