I need to build a javascript engine (duktape or jerryscript) for a embedded device which should be capable of executing shell commands. how can this be achieved?
With duktape in C, you can easily create a native ECMAScript function and create a reference to it via the global object:
#include "duktape.h"
int main() {
/* create heap */
duk_context* heap = duk_create_heap(NULL,NULL,NULL,NULL,
NULL); /* preferably, set an error callback here */
/* push the native function on the stack */
duk_push_c_function(ctx, /* heap context */
&native_js_shell, /* native function pointer */
1); /* accepted arguments */
/* make this javascript function a property of the global object */
duk_put_global_string(ctx, /* heap context*/
"shell"); /* function name in js */
return 0;
}
/* Your native C function */
duk_ret_t native_js_shell(duk_context* ctx) {
/* obtain the argument from javascript */
const char* cmd = duk_safe_to_string(ctx, /* heap context */
-1); /* position on stack */
/* run the shell command, etc. */
/* ... */
}
All the explanation for the duk_* functions can be found in the duktape API, but perhaps this gives you an idea of how it's structured.
p.s. Welcome to Stack Overflow! Your question may have been downrated since it pretty much requires someone to write all the code for you. In general, in the future, try to do the research on your own, and ask specific questions as you get stuck. :)
Related
I have a few datamodules, created with C++ Builder 6. Each of them uses another datamodule that initializes the connection with the database. I'm trying to make a DLL out of those datamodules.
The error is thrown on creation of DataModule_Users and says 'Abnormal program termination'.
Where do I go wrong?
Thanks in advance
datamodule_dll.bpf
USEFORM("DataModule_Connection.cpp", DataModule_Connection); /* TDataModule: File Type */
USEFORM("DataModule_Users.cpp", DataModule_Users); /*TDataModule: File Type */
//------------------------------------------------------------------
This file is used by the project manager only and should be treated like the project file
DllEntryPoint
datamodule_DLL.cpp
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
extern "C" __declspec(dllexport) const char * GetUserName(const char * ID);
const char * GetUserName(const char * ID) {
CoInitialize(NULL); // datasets use COM
// This is used by DataModule_Users (normally this is done with Application->CreateForm();
DataModule_Connection = new DataModule_Connection( 0 /* Owner */ );
DataModule_Users = new DataModule_Users( 0 /* Owner */ );
return DataModule_Users->GetUserName(ID);
}
I will quote Remy Lebeau:
COM is initialized on a per-thread basis. Once a thread's COM model
(apartment vs multithreaded) has been set, it cannot be changed later.
If your DLL calls CoInitialize() before the caller does, your
parameters would take priority, which might not meet the caller's
needs. If you want to control the COM settings for your DMs without
affecting the caller, you have to move the DMs to their own thread.
Besides, DllEntryPoint() is not an appropriate place to initialize
them anyway. The OS restricts what DllEntryPoint() is allowed to do.
This solved the issue. I had to call coInitialize() before I load the library.
I'm developing software for an ARM Cortex M3 (NXP LPC1769) microncontroller. At the moment I'm searching for a mechansim to detect if my function is called within an ISR. I asume that I have to check a register. Based on this information I would like to call difficult functions.
I already checked the reference manual, if there is a register containing the necessary information.
For example I tried to detect if I'm called from an ISR (I used SysTick-ISR) based on the "Interrupt Active Bit Register" (IABR) register. This register should be != 0 if an ISR is active. But the value was 0x00000000. This implies that no interrupt is active. Besides this test I checked the NVIC and SC register in the reference manual searching for a register containing the necessary flag but I didn't found one.
Does anybody know a suitable register / mechanism for my problem?
You need to test the VECTACTIVE field of the Interrupt Control State Register.
I use the following:
//! Test if in interrupt mode
inline bool isInterrupt()
{
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ;
}
SCM and SCB_ICSR_VECTACTIVE_Msk are defined in the CMSIS (core_cm3.h), which I imagine would be included indirectly by your part specific header (lpc17xx.h or similar I guess). I am using C++, including stdbool.h in C will get you a bool type, or change to an int or typedef of your own.
It is then used thus for example:
void somefunction( char ch )
{
if( isInterrupt() )
{
// Do not block if ISR
send( ch, NO_WAIT ) ;
}
else
{
send( ch, TIMEOUT ) ;
}
}
If a solution is required that assumes no knowledge of the architecture consider the following:
volatile int interrupt_nest_count = 0 ;
#define ENTER_ISR() interrupt_nest_count++
#define EXIT_ISR() interrupt_nest_count--
#define IN_ISR() (interrupt_nest_count != 0)
void isrA()
{
ENTER_ISR() ;
somefunction( 'a' ) ;
EXIT_ISR() ;
}
void isrB()
{
ENTER_ISR() ;
somefunction( 'b' ) ;
EXIT_ISR() ;
}
void somefunction( char ch )
{
if( IN_ISR() )
{
// Do not block if ISR
send( ch, NO_WAIT ) ;
}
else
{
send( ch, TIMEOUT ) ;
}
}
However the question refers to safely detecting the interrupt context, and this relies on the enter/exit macros being added to all ISRs.
After some discussion and more searching I found the right register:
Interrupt Program Status Register: The IPSR contains the exception type number of
the current Interrupt Service Routine (ISR). See the register summary in Table 626 for
its attributes.
If a function isn't called from an isr the value of the register is IPSR == 0
The simplest method is to pass the context as a parameter to the function. It is also platform independent.
typedef enum _context {
normal_context = 0,
isr_context = 1
} context;
Call to the function from ISR:
func(param1, param2, isr_context);
Call to the function from normal code:
func(param1, param2, normal_context);
If the ISR code is not under your control and you are just passing a function pointer, then just use two different wrapper functions. One that passes isr_context and another that passes normal_context as a parameter to the function.
The best way is probably to make two different functions: one that is called from the ISR and another that is called from the rest of the program.
If that isn't an option, then you could determine the caller with pure standard C, no registers needed:
inline void my_func (const char* caller);
static void isr (void)
{
my_func(__func__);
}
inline void my_func (const char* caller)
{
if(strcmp(caller, "isr")==0)
{
// was called from isr
}
else
{
// called from elsewhere
}
}
If you give your ISRs smart names, the above code will be quick enough to run from an isr.
I've been coding an NGINX filter module that can read/write cookies for incoming requests. If a particular cookie isn't set correctly (i.e. authentication cookie), it will set the outgoing header status to the appropriate error code. This works fine per the directions of Evan Miller's tutorial. The next part I'm trying to get working (and haven't thus far) is having the body filter be invoked so I can insert/replace body response text when error responses are encountered. I again followed Evan Miller's tutorial on body filters, and I cannot for the life of me get this working. Here's my setup:
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
...
...
static ngx_http_module_t ngx_http_source_cookie_module_ctx = {
NULL, /* preconfiguration */
ngx_http_source_cookie_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_source_cookie_create_loc_conf, /* create location configuration */
ngx_http_source_cookie_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_source_cookie_module = {
NGX_MODULE_V1,
&ngx_http_source_cookie_module_ctx, /* module context */
ngx_http_source_cookie_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_source_cookie_header_filter(ngx_http_request_t *r)
{
// this gets invoked
...
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
// this never get invoked
...
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static ngx_int_t
ngx_http_source_cookie_init(ngx_conf_t *cf)
{
// registering of my filters
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_source_cookie_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_body_filter;
return NGX_OK;
}
This is my basic setup, and as far as I can tell, it's spot on all the examples/tutorials I've come across. I'm wondering if there's something different altogether I need to enable... like a NGINX config option, NGINX ./configure compile option, etc.
Any help is greatly appreciated.
I note that Evan doesnt fix http content length in ngx_http_<module_name>_header_filter().
If I dont add http content length(r->headers_out.content_length_n), the inserted text to the end of request will not be output from nginx-1.2.7 stable.
Also you can see footer filter module.
I want to write a little Linux Kernel Module that can show me the PID of all running processes.
I have the following code:
/*
* procInfo.c My Kernel Module for process info
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/*
* The init function, called when the module is loaded.
* Returns zero if successfully loaded, nonzero otherwise.
*/
static int mod_init(void)
{
printk(KERN_ALERT "ProcInfo sucessfully loaded.\n");
return 0;
}
/*
* The exit function, called when the module is removed.
*/
static void mod_exit(void)
{
printk(KERN_ALERT "ProcInfo sucessfully unloaded.\n");
}
void getProcInfo()
{
printk(KERN_INFO "The process is \"%s\" (pid %i)\n",
current->comm, current->pid);
}
module_init(mod_init);
module_exit(mod_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rodrigo");
As you can see, i know i have to use the *struct task_struct* structure to get the PID and the Process name, but i am using current, and i know the existance of some double linked circular list that contains all PCB's, so the main question is:
what do i need to add to iterate over this linked lisk with p-next_task and p-prev_task so getProcInfo works?
Thanks!
The following macros from include/linux/sched.h may be useful:
#define next_task(p) \
list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
#define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; )
You probably need to hold the tasklist_lock before calling these macros; several examples of how to lock, iterate, and unlock, are in mm/oom_kill.c.
Actually, for newer kernels (2.6.18 and newer) the proper way to list tasks is by holding an rcu lock, because task list is now an RCU list. Also tasklist_lock is no more exported symbol - it means that when you are compiling a loadable kernel module, this symbol will not be visible for you.
example code to use
struct task_struct *task;
rcu_read_lock();
for_each_process(task) {
task_lock(task);
/* do something with your task :) */
task_unlock(task);
}
rcu_read_unlock();
Also documentation about RCU in linux kernel source directory can be helpful and you will find it in Documentation/RCU
My application is built with apache and runs on windows. I am creating a Thread using the createThread() and then for each thread executing the below :
ap_run_sub_req( subrequest );
ap_rflush( subrequest );
ap_destroy_sub_req( subrequest );
The ap_destroy_sub_request in turn calls apr_pool_destroy() function.
The ap_run_sub_req() allocated memory for pool and ap_destroy_sub_req() frees the allocated memory.
If the apr_pool_destroy() is called inside a thread then the allocated memory is not freed as a result my application is having memory leak. I couldn't find in any apache documentation any mention of apr_pool_destroy() being non-thread safe functions.
How can this problem be resolved ?? How can I free the allocated pool inside the threads?
Thanks
Here's the source code for apr_pool_destroy():
APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
{
apr_memnode_t *active;
apr_allocator_t *allocator;
/* Run pre destroy cleanups */
run_cleanups(&pool->pre_cleanups);
pool->pre_cleanups = NULL;
/* Destroy the subpools. The subpools will detach themselve from
* this pool thus this loop is safe and easy.
*/
while (pool->child)
apr_pool_destroy(pool->child);
/* Run cleanups */
run_cleanups(&pool->cleanups);
/* Free subprocesses */
free_proc_chain(pool->subprocesses);
/* Remove the pool from the parents child list */
if (pool->parent) {
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex;
if ((mutex = apr_allocator_mutex_get(pool->parent->allocator)) != NULL)
apr_thread_mutex_lock(mutex);
#endif /* APR_HAS_THREADS */
if ((*pool->ref = pool->sibling) != NULL)
pool->sibling->ref = pool->ref;
#if APR_HAS_THREADS
if (mutex)
apr_thread_mutex_unlock(mutex);
#endif /* APR_HAS_THREADS */
}
/* Find the block attached to the pool structure. Save a copy of the
* allocator pointer, because the pool struct soon will be no more.
*/
allocator = pool->allocator;
active = pool->self;
*active->ref = NULL;
#if APR_HAS_THREADS
if (apr_allocator_owner_get(allocator) == pool) {
/* Make sure to remove the lock, since it is highly likely to
* be invalid now.
*/
apr_allocator_mutex_set(allocator, NULL);
}
#endif /* APR_HAS_THREADS */
/* Free all the nodes in the pool (including the node holding the
* pool struct), by giving them back to the allocator.
*/
allocator_free(allocator, active);
/* If this pool happens to be the owner of the allocator, free
* everything in the allocator (that includes the pool struct
* and the allocator). Don't worry about destroying the optional mutex
* in the allocator, it will have been destroyed by the cleanup function.
*/
if (apr_allocator_owner_get(allocator) == pool) {
apr_allocator_destroy(allocator);
}
}
From the looks of it, it's not thread-safe, but I'm not a C expert. You should probably post on the APR mailing list.