|
Procster
0.1
Procster-ViewProcessesovertheweb
|
Process server (main()) More...
#include <sys/types.h>#include <sys/select.h>#include <sys/socket.h>#include <microhttpd.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <jansson.h>#include <ctype.h>#include "proclister.h"#include <fcntl.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/resource.h>
Data Structures | |
| struct | connection_info_struct |
| MHD connection/request Helper data structure for maintaining the "state" of request data reading / parsing (as data is read in chunks) for HTTP methods that have a HTTP Body (POST, PUT). More... | |
Macros | |
| #define | MHD_PLATFORM_H |
| #define | POSTBUFFERSIZE 256 |
| #define | con_info_need_mem(con_info, cnt) (con_info->used + cnt + 1) |
| #define | parse_no_state(c) (!c) |
| Macros for detecting parsing state (from MHD multiple calls to response handler). More... | |
| #define | parse_starting(c) ((!*con_cls) && (!c->is_parsing) && (c->is_parsing = 1) && (*con_cls = c)) |
| #define | parse_running(c) ((*con_cls) && (c->is_parsing == 1)) |
| #define | parse_ending(c) ((*con_cls) && (!*upload_data_size) && (c->is_parsing) && (c->is_parsing = 2)) |
Typedefs | |
| typedef struct connection_info_struct | CONNINFO |
Enumerations | |
| enum | ConnectionType { GET = 0, POST = 1 } |
| enum | ParsingState { INITED =0, PARSING, COMPLETE } |
Functions | |
| char * | proc_list_json (int flags) |
| Create a process list directly from readproc() listing as JSON. More... | |
| static int | send_page (struct MHD_Connection *connection, const char *page, int status_code) |
| Generic string content sending. More... | |
| void | con_info_destroy (CONNINFO *con_info) |
| Destroy POST Request info freeing all allocated memory. More... | |
| CONNINFO * | con_info_create (struct MHD_Connection *connection) |
| Allocate CONNINFO for POST Parsing. More... | |
| int | post_body_parse (struct MHD_Connection *connection, const char *upload_data, size_t *upload_data_size, void **con_cls) |
| Parse POST Body incrementally. More... | |
| int | answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) |
| MHD POST Handler example. More... | |
| int | answer_to_connection1 (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) |
| Legacy: Generic POST Handler with POST Body parsing. More... | |
| struct MHD_Response * | trystatic (const char *url) |
| Try to resolve url to a static file and return response for it. More... | |
| int | basic_creds_ok (struct MHD_Connection *connection) |
| Check HTTP Basic credentials. More... | |
| int | pid_extract (const char *url) |
| Extract pid number from (relative) Web URL. More... | |
| int | answer_to_connection0 (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) |
| MHD Response handler for creating OS Process listing. More... | |
| void | req_term_cb (void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe) |
| Post request Callback launched by MHD after completing request. More... | |
| int | savepid (json_t *json) |
| void | daemon_prep () |
| void | daemon_launch (int port, json_t *json) |
| int | main (int argc, char *argv[]) |
| Main for (Micro HTTP Daemon) process app. More... | |
Variables | |
| char | docroot [256] = {0} |
Process server (main())
gcc -o procserver procserver.c -lmicrohttpd
Poll each 3000 ms Case 1
| #define con_info_need_mem | ( | con_info, | |
| cnt | |||
| ) | (con_info->used + cnt + 1) |
Referenced by con_info_create(), and post_body_parse().
| #define MHD_PLATFORM_H |
| #define parse_ending | ( | c | ) | ((*con_cls) && (!*upload_data_size) && (c->is_parsing) && (c->is_parsing = 2)) |
Referenced by answer_to_connection().
| #define parse_no_state | ( | c | ) | (!c) |
Macros for detecting parsing state (from MHD multiple calls to response handler).
Macros have to be called in the order they appear here (See also example for post_body_parse()). This is because they also change the state of the parsing. These macros eliminate the calls to parser.
Referenced by answer_to_connection().
| #define parse_running | ( | c | ) | ((*con_cls) && (c->is_parsing == 1)) |
Referenced by answer_to_connection().
| #define parse_starting | ( | c | ) | ((!*con_cls) && (!c->is_parsing) && (c->is_parsing = 1) && (*con_cls = c)) |
Referenced by answer_to_connection().
| #define POSTBUFFERSIZE 256 |
| typedef struct connection_info_struct CONNINFO |
| enum ConnectionType |
| enum ParsingState |
| int answer_to_connection | ( | void * | cls, |
| struct MHD_Connection * | connection, | ||
| const char * | url, | ||
| const char * | method, | ||
| const char * | version, | ||
| const char * | upload_data, | ||
| size_t * | upload_data_size, | ||
| void ** | con_cls | ||
| ) |
MHD POST Handler example.
Echoes back the request content (w/o parsing it in between).
References con_info_create(), con_info_destroy(), MHD_add_response_header(), MHD_create_response_from_buffer(), MHD_destroy_response(), MHD_YES, parse_ending, parse_no_state, parse_running, parse_starting, post_body_parse(), connection_info_struct::postdata, and send_page().
| int answer_to_connection0 | ( | void * | cls, |
| struct MHD_Connection * | connection, | ||
| const char * | url, | ||
| const char * | method, | ||
| const char * | version, | ||
| const char * | upload_data, | ||
| size_t * | upload_data_size, | ||
| void ** | con_cls | ||
| ) |
MHD Response handler for creating OS Process listing.
Example of simple GET handling of HTTP Request.
References MHD_add_response_header(), MHD_create_response_from_buffer(), MHD_destroy_response(), pid_extract(), proc_kill(), proc_list_json2(), proc_tree(), ptree_free(), ptree_json(), and trystatic().
Referenced by daemon_launch().
| int answer_to_connection1 | ( | void * | cls, |
| struct MHD_Connection * | connection, | ||
| const char * | url, | ||
| const char * | method, | ||
| const char * | version, | ||
| const char * | upload_data, | ||
| size_t * | upload_data_size, | ||
| void ** | con_cls | ||
| ) |
Legacy: Generic POST Handler with POST Body parsing.
See: MHD_post_process and MHD_create_post_processor MHD_ContentReaderFreeCallback MHD_RequestCompletedCallback set by MHD_OPTION_NOTIFY_COMPLETED (2 pointer params)
References con_info_destroy(), connection_info_struct::is_parsing, MHD_add_response_header(), MHD_create_response_from_buffer(), MHD_destroy_response(), MHD_YES, post_body_parse(), connection_info_struct::postdata, and send_page().
| int basic_creds_ok | ( | struct MHD_Connection * | connection | ) |
Check HTTP Basic credentials.
| CONNINFO* con_info_create | ( | struct MHD_Connection * | connection | ) |
Allocate CONNINFO for POST Parsing.
CONNINFO is used to keep track of body retrieval / buffering state across multiple calls to MHD answer_to_connection() request handler callback.
| connection | - MHD connection / request struct |
References con_info_destroy(), con_info_need_mem, connection_info_struct::contlen, connection_info_struct::conttype, connection_info_struct::debug, connection_info_struct::postdata, and connection_info_struct::size.
Referenced by answer_to_connection().
| void con_info_destroy | ( | CONNINFO * | con_info | ) |
Destroy POST Request info freeing all allocated memory.
It is recommended that this is called in the MHD main handler or req_term_cb() (which ? Both passed to MHD_start_daemon() as 5th and 8th params respectively).
References connection_info_struct::conttype, and connection_info_struct::postdata.
Referenced by answer_to_connection(), answer_to_connection1(), con_info_create(), and req_term_cb().
| void daemon_launch | ( | int | port, |
| json_t * | json | ||
| ) |
References answer_to_connection0(), and savepid().
Referenced by main().
| void daemon_prep | ( | ) |
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Main for (Micro HTTP Daemon) process app.
5th param to MHD_start_daemon() defines the main connection handler (that should do respective dispatching if app handles many different actions).
References daemon_launch(), daemon_prep(), and docroot.
| int pid_extract | ( | const char * | url | ) |
Extract pid number from (relative) Web URL.
| url | - procserver kill process URL |
Referenced by answer_to_connection0().
| int post_body_parse | ( | struct MHD_Connection * | connection, |
| const char * | upload_data, | ||
| size_t * | upload_data_size, | ||
| void ** | con_cls | ||
| ) |
Parse POST Body incrementally.
This gets called multiple times as a result of MHD answer_to_connection() being called multiple times with request types that have HTTP Body present.
The state of request processing and body parsing is reflected in con_info.state in follwing ways:
| connection | - MHD Connection |
| upload_data | - Incoming POST data fragment (to add to collected POST data) |
| upload_data_size | - Size of new fragment |
| con_cls | - MHD Request specific user-data, Here: CONNINFO * |
References con_info_need_mem, connection_info_struct::debug, connection_info_struct::is_parsing, MHD_YES, connection_info_struct::postdata, connection_info_struct::size, and connection_info_struct::used.
Referenced by answer_to_connection(), and answer_to_connection1().
| char* proc_list_json | ( | int | flags | ) |
Create a process list directly from readproc() listing as JSON.
For flags see FLAGS in man openproc.
| flags | The openproc() flags that define the extent of parsing process info from /proc/$PID (There are ~ 20 flags available). |
Make process attibutes more configurable. Possibly corralate fields to generate to flags passed as input (need lookup tables for those).
Possibly wrap response with additional JSON object to be able to convey status and error messages.
References IS_KTHREAD, and PROC_FLAGS_DEFAULT.
| void req_term_cb | ( | void * | cls, |
| struct MHD_Connection * | connection, | ||
| void ** | con_cls, | ||
| enum MHD_RequestTerminationCode | toe | ||
| ) |
Post request Callback launched by MHD after completing request.
References con_info_destroy().
| int savepid | ( | json_t * | json | ) |
Referenced by daemon_launch().
|
static |
Generic string content sending.
| connection | MHD_Connection / single request (See MHD Docs) |
| page | - Page as (null terminated) text |
| status_code | - HTTP numeirc status code (e.g. 200, 400. ...) to send with response |
References MHD_create_response_from_buffer(), and MHD_destroy_response().
Referenced by answer_to_connection(), and answer_to_connection1().
| struct MHD_Response* trystatic | ( | const char * | url | ) |
Try to resolve url to a static file and return response for it.
| url | - Request URL to test for static content |
References docroot, MHD_create_response_from_fd(), and connection_info_struct::size.
Referenced by answer_to_connection0().
| char docroot[256] = {0} |
Referenced by main(), server_new(), and trystatic().