1 /**********************************************************************
3 * rtems shttpd management
5 * FILE NAME : rtems_shttpd.c
7 * AUTHOR : Steven Johnson
9 * DESCRIPTION : Defines the interface functions to the shttp daemon
11 * REVISION : $Id: compat_rtems.c,v 1.2 2006/11/12 03:29:17 infidel Exp $
15 **********************************************************************/
17 /**********************************************************************
19 **********************************************************************/
23 #define MAX_WEB_BASE_PATH_LENGTH 256
24 #define MIN_SHTTPD_STACK (8*1024)
26 typedef struct RTEMS_HTTPD_ARGS {
27 rtems_shttpd_init init_callback;
28 rtems_shttpd_addpages addpages_callback;
29 char webroot[MAX_WEB_BASE_PATH_LENGTH];
32 static int rtems_webserver_running = FALSE; //not running.
34 static rtems_task rtems_httpd_daemon(rtems_task_argument args )
36 RTEMS_HTTPD_ARGS *httpd_args = (RTEMS_HTTPD_ARGS*)args;
38 struct shttpd_ctx *ctx;
40 if (httpd_args != NULL)
41 if (httpd_args->init_callback != NULL)
42 httpd_args->init_callback();
44 /**************************************
45 * Initialize the web server
48 * Initialize SHTTPD context.
49 * Set WWW root to current WEB_ROOT_PATH.
51 ctx = shttpd_init(NULL, "document_root", httpd_args->webroot, NULL);
53 if (httpd_args != NULL)
54 if (httpd_args->addpages_callback != NULL)
55 httpd_args->addpages_callback(ctx);
57 /* Finished with args, so free them */
58 if (httpd_args != NULL)
61 /* Open listening socket */
62 shttpd_listen(ctx, 9000);
64 rtems_webserver_running = TRUE;
66 /* Serve connections infinitely until someone kills us */
67 while (rtems_webserver_running)
68 shttpd_poll(ctx, 1000);
70 /* Unreached, because we will be killed by a signal */
73 rtems_task_delete( RTEMS_SELF );
76 rtems_status_code rtems_initialize_webserver(rtems_task_priority initial_priority,
77 rtems_unsigned32 stack_size,
78 rtems_mode initial_modes,
79 rtems_attribute attribute_set,
80 rtems_shttpd_init init_callback,
81 rtems_shttpd_addpages addpages_callback,
87 RTEMS_HTTPD_ARGS *args;
89 if (stack_size < MIN_SHTTPD_STACK)
90 stack_size = MIN_SHTTPD_STACK;
92 args = malloc(sizeof(RTEMS_HTTPD_ARGS));
96 args->init_callback = init_callback;
97 args->addpages_callback = addpages_callback;
98 strncpy(args->webroot,webroot,MAX_WEB_BASE_PATH_LENGTH);
100 sc = rtems_task_create(rtems_build_name('H', 'T', 'P', 'D'),
107 if (sc == RTEMS_SUCCESSFUL)
109 sc = rtems_task_start(tid, rtems_httpd_daemon, (rtems_task_argument)args);
114 sc = RTEMS_NO_MEMORY;
120 void rtems_terminate_webserver(void)
122 rtems_webserver_running = FALSE; //not running, so terminate
125 int rtems_webserver_ok(void)
127 return rtems_webserver_running;
131 set_close_on_exec(int fd)
133 // RTEMS Does not have a functional "execve"
134 // so technically this call does not do anything,
135 // but it doesnt hurt either.
136 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
140 my_stat(const char *path, struct stat *stp)
142 return (stat(path, stp));
146 my_open(const char *path, int flags, int mode)
148 return (open(path, flags, mode));
152 my_remove(const char *path)
154 return (remove(path));
158 my_rename(const char *path1, const char *path2)
160 return (rename(path1, path2));
164 my_mkdir(const char *path, int mode)
166 return (mkdir(path, mode));
170 my_getcwd(char *buffer, int maxlen)
172 return (getcwd(buffer, maxlen));
176 set_non_blocking_mode(int fd)
181 if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
182 DBG(("nonblock: fcntl(F_GETFL): %d", ERRNO));
183 } else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) {
184 DBG(("nonblock: fcntl(F_SETFL): %d", ERRNO));
186 ret = 0; /* Success */
194 spawn_process(struct conn *c, const char *prog, char *envblk, char **envp)
196 return (-1); // RTEMS does not have subprocess support as standard.