summaryrefslogtreecommitdiffstats
path: root/cpukit/shttpd/compat_rtems.c
blob: 53d33d08a9584ee23ee9f04ecd84a7c6ff9416b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**********************************************************************
 *
 *  rtems shttpd management
 *
 *  FILE NAME   : rtems_shttpd.c
 *
 *  AUTHOR      : Steven Johnson
 *
 *  DESCRIPTION : Defines the interface functions to the shttp daemon
 *
 *  REVISION    : $Id$
 *
 *  COMMENTS    :
 *
 **********************************************************************/

 /**********************************************************************
 * INCLUDED MODULES
 **********************************************************************/
#include <rtems.h>
#include "defs.h"

#define MAX_WEB_BASE_PATH_LENGTH 256
#define MIN_SHTTPD_STACK         (8*1024)

typedef struct RTEMS_HTTPD_ARGS {
    rtems_shttpd_init     init_callback;
    rtems_shttpd_addpages addpages_callback;
    unsigned int          port;
    char                  webroot[MAX_WEB_BASE_PATH_LENGTH];
} RTEMS_HTTPD_ARGS;

static int rtems_webserver_running = FALSE; //not running.

static rtems_task rtems_httpd_daemon(rtems_task_argument args)
{
  RTEMS_HTTPD_ARGS *httpd_args = (RTEMS_HTTPD_ARGS*)args;

  struct shttpd_ctx       *ctx;

  if (httpd_args != NULL)
    if (httpd_args->init_callback != NULL)
      httpd_args->init_callback();

/**************************************
 *  Initialize the web server
 */
  /*
    * Initialize SHTTPD context.
    * Set WWW root to current WEB_ROOT_PATH.
    */
  ctx = shttpd_init(NULL, "document_root", httpd_args->webroot, NULL);

  if (httpd_args != NULL)
    if (httpd_args->addpages_callback != NULL)
      httpd_args->addpages_callback(ctx);

  /* Finished with args, so free them */
  if (httpd_args != NULL)
    free(httpd_args);

  /* Open listening socket */
  shttpd_listen(ctx, httpd_args->port);

  rtems_webserver_running = TRUE;

  /* Serve connections infinitely until someone kills us */
  while (rtems_webserver_running)
    shttpd_poll(ctx, 1000);

  /* Unreached, because we will be killed by a signal */
  shttpd_fini(ctx);

  rtems_task_delete( RTEMS_SELF );
}

rtems_status_code rtems_initialize_webserver(
  rtems_task_priority   initial_priority,
  size_t                stack_size,
  rtems_mode            initial_modes,
  rtems_attribute       attribute_set,
  rtems_shttpd_init     init_callback,
  rtems_shttpd_addpages addpages_callback,
  char                 *webroot,
  unsigned int          port
)
{
  rtems_status_code   sc;
  rtems_id            tid;
  RTEMS_HTTPD_ARGS    *args;

  if (stack_size < MIN_SHTTPD_STACK)
    stack_size = MIN_SHTTPD_STACK;

  args = malloc(sizeof(RTEMS_HTTPD_ARGS));

  if (args != NULL) {
    args->init_callback = init_callback;
    args->addpages_callback = addpages_callback;
    args->port = port;
    strncpy(args->webroot,webroot,MAX_WEB_BASE_PATH_LENGTH);

    sc = rtems_task_create(rtems_build_name('H', 'T', 'P', 'D'),
                           initial_priority,
                           stack_size,
                           initial_modes,
                           attribute_set,
                           &tid);

    if (sc == RTEMS_SUCCESSFUL) {
      sc = rtems_task_start(tid, rtems_httpd_daemon, (rtems_task_argument)args);
    }
  } else {
    sc = RTEMS_NO_MEMORY;
  }

  return sc;
}

void rtems_terminate_webserver(void)
{
  rtems_webserver_running = FALSE; // not running, so terminate
}

int rtems_webserver_ok(void)
{
  return rtems_webserver_running;
}

void
set_close_on_exec(int fd)
{
        // RTEMS Does not have a functional "execve"
        // so technically this call does not do anything,
        // but it doesnt hurt either.
        (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
}
#ifndef __rtems__
int
my_stat(const char *path, struct stat *stp)
{
  return (stat(path, stp));
}

int
my_open(const char *path, int flags, int mode)
{
  return (open(path, flags, mode));
}

int
my_remove(const char *path)
{
  return (remove(path));
}

int
my_rename(const char *path1, const char *path2)
{
  return (rename(path1, path2));
}

int
my_mkdir(const char *path, int mode)
{
  return (mkdir(path, mode));
}

char *
my_getcwd(char *buffer, int maxlen)
{
  return (getcwd(buffer, maxlen));
}
#endif
int
set_non_blocking_mode(int fd)
{
  int     ret = -1;
  int     flags;

  if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
    DBG(("nonblock: fcntl(F_GETFL): %d", ERRNO));
  } else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) {
    DBG(("nonblock: fcntl(F_SETFL): %d", ERRNO));
  } else {
    ret = 0;        /* Success */
  }

  return (ret);
}

#if !defined(NO_CGI)
int
spawn_process(struct conn *c, const char *prog, char *envblk, char **envp)
{
  return (-1); // RTEMS does not have subprocess support as standard.
}
#endif