[19. June 2013] Soundcloud® available now | [7. June 2013] Youporn available now | [11 Jan. 2013] Customize your A-400 Home Screen

Firmware: A-400 [13 May 2013] | C-300 [30 Nov. 2012] | A-300 [30 Nov. 2012] | C-200 RC1 [13 June 2013] | A-200/A-210 RC1 [13 June 2013]

Just got your NMT | WIKI has the answers | Search the forum | Forum Rules/Policy | Firmware & Official NMT News | Popcornhour manuals



User(s) browsing this thread: 1 Guest(s)
Thread Closed 
unix/c gurus - Starting a process from gaya without it hanging
05-01-2010, 06:27 PM (This post was last modified: 05-01-2010 07:00 PM by lordy.)
Post: #1
unix/c gurus - Starting a process from gaya without it hanging
Hi,

I have a few cgi applications where I want to start a long running process.
However on nmt100 this causes Gaya to hang.

So I thought I'd write a daemon launcher, but still gaya waits for it to finish.
Can anyone see what the problem is...
(note that gaya is invoking the daemon process directly and not going via wget/sybhttpd)

References : here and here

The following program should allow gaya to launch any command as
Code:
daemon command args

but gaya still waits for termination. Maybe it is a signalling issue ?

(ignore the php tag Smile )
PHP Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

/*
 * from http://www.steve.org.uk/Reference/Unix/faq_2.html#SEC16
 * Should allow safe forking of processes from gaya.
 * The directory change step has been omitted.
 */

#define SLEEP_USECS 1000000

static void set_logs();
static 
void close_all();

int main(int argcchar **argv) {

    
//int status;
    
int pid fork();

    switch(
pid) {

        case -
1:
            
fprintf(stderr,"Unable to fork - error %d",errno);
            exit(-
1);
            break;
        case 
0:
            
// fall through
            
break;
        default:
            
_exit(0);
    }

    
// child 1 ---------------------------------------------
    
    // Give parent time to die. - grasping at straws

    
usleep(SLEEP_USECS);

    
// NMT gaya still detects the child process. - trying both calls
    
setpgrp();
    
setsid();

    
pid fork();
    switch(
pid) {

        case -
1:
            
fprintf(stderr,"Unable to fork child - error %d",errno);
            exit(-
1);
            break;
        case 
0:
            
// fall through
            
break;
        default:
            
_exit(0);
    }

    
// child 2 ---------------------------------------------

    // Give parent time to die. - grasping at straws
    
usleep(SLEEP_USECS);

    
setpgrp();
    
setsid();

    
// chdir("/");
    
umask(0);

    
set_logs();

    
close_all();

    
char *env;

    if ((
env getenv("DAEMON_DIR")) != NULL) {
        
chdir(env);
    }

    
char *=  get_current_dir_name();


    
int i;
    for(
argc i++ ) {
        
printf("'%s' ",argv[i]);
    }
    
printf(" [%s] \n",d);
    
fflush(stdout);
    
free(d);

    if (
execvp(argv[1],argv+1) == -1) {
        
fprintf(stderr,"Unable to exec [%s] - error %d",argv[1],errno);
    }
    
_exit(-1);
}

static 
void close_all()
{
    
long i;
    
long fc_max sysconf(_SC_OPEN_MAX);
    for( 
fc_max i++ ) {
        
close(i);
    }
}

static 
void set_logs()
{
    
char *log_dir "/share/tmp";
    
char *out_log_name "daemon.out";
    
char *err_log_name "daemon.out";

    
char *env;

    if ((
env getenv("DAEMON_LOG_DIR")) != NULL) {
        
log_dir env;
    }
    
char *out_path malloc(strlen(log_dir) + strlen(out_log_name) + 2);
    
char *err_path malloc(strlen(log_dir) + strlen(err_log_name) + 2);

    
mkdir(log_dir,0777); // ignore error for now

    
if (out_path != NULL ) {
        
sprintf(out_path,"%s/%s",log_dir,out_log_name);
        
freopen(out_path,"w",stdout);
    }

    if (
err_path != NULL ) {
        
sprintf(err_path,"%s/%s",log_dir,err_log_name);
        
freopen(err_path,"w",stderr);
    }
    
freopen("/dev/null","r",stdin);


Oversight: Jukebox | FeedTime: Automatic nzbs
Find all posts by this user
05-01-2010, 07:35 PM
Post: #2
RE: unix/c gurus - Starting a process from gaya without it hanging
Hi,

I sussed it. The first of the three processes should call exit() to signal back to gaya to move on Smile The others should use _exit().

If anyone is interested the daemon launcher will be a small executable bundled with the next version of oversight.
They are welcome to use it, to stop gaya waiting for long running processes.

It also means I can remove the cron job hack.

Oversight: Jukebox | FeedTime: Automatic nzbs
Find all posts by this user
05-01-2010, 07:38 PM
Post: #3
RE: unix/c gurus - Starting a process from gaya without it hanging
It might be better for your application to just run in the background on startup and then signal it to do something instead of launch it to run.
Find all posts by this user
05-01-2010, 07:55 PM (This post was last modified: 05-01-2010 07:56 PM by lordy.)
Post: #4
RE: unix/c gurus - Starting a process from gaya without it hanging
Hi, accident, thanks for the suggestion, I try to minimise the number of long running processes on the NMT.

I do have regular scans that are started by a cron job. ,
but I just need this for when the user initiates a manual scan. (which hopefully is not often).

Now I have this working, it means oversight does not add any overhead to NMT resources. Overall its easier now to just say..

Code:
daemon command args

rather than develop an additional server, and manage its installation/startup/shutdown to avoid this issue.

I did have a horrible hack in place before this to launch user scans, back when Oversight was just bash/awk scripts. I had a cron job that check for new commands every minute. I didn't have the toolchain in place at the time to write proper code! It actually worked better than expected but now I can get rid of it!

However, somewhere down the line, for top performance , I may need to introduce a proper server process for performance reasons.
At the moment oversight scans the entire video inventory during every page load. (400 items in about 10ms on c200. 250 items in 30ms on hdx1000 ) but performance will degrade with bigger collections, so a full client/server architecture *may* help here.

Oversight: Jukebox | FeedTime: Automatic nzbs
Find all posts by this user
05-02-2010, 03:43 AM
Post: #5
RE: unix/c gurus - Starting a process from gaya without it hanging
Look about right, you fork(), child->setsid() close(0,1,2) and execve a new process.

Your double fork should not be required (any more, legacy)
Visit this user's website Find all posts by this user
05-04-2010, 06:43 PM
Post: #6
RE: unix/c gurus - Starting a process from gaya without it hanging
Tx, gaya was waiting for one of the sub-processes to call exit() ( rather than _exit() ).
Interesting about the double fork - I'll go do some reading...

Oversight: Jukebox | FeedTime: Automatic nzbs
Find all posts by this user
Thread Closed 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Help required to fix Extended gaya Extended 0.4 AquaFire 2 2,151 03-17-2011 07:46 AM
Last Post: accident
  TankTV (AppleTV Clone & Gaya replacement) vinayp 69 24,829 01-08-2011 05:48 PM
Last Post: AlramEast
  gaya / directfb / video memory thica 5 3,450 04-20-2010 07:28 AM
Last Post: thica
  Gaya, mono and interprocess stuff - how does it work? frankvw 11 3,621 03-20-2010 07:28 PM
Last Post: frankvw
  RAR support in gaya / mono / YAMJ x0r 1 2,041 03-09-2010 01:19 AM
Last Post: lundman
  NZBGET: Post-Process: /bin/sh: can't open ' ' Leceur 1 2,715 02-23-2010 04:38 PM
Last Post: Leceur
  Running/Stopped status from NMT Applications page - FTP stuck on starting DaIceMan 0 2,048 01-06-2010 01:28 AM
Last Post: DaIceMan
  starting on internal disk harrykausl 0 1,074 12-27-2009 12:17 PM
Last Post: harrykausl
  Gaya Extended gshock 107 62,354 11-30-2009 11:56 PM
Last Post: Mesterhak
  HTML Gaya Form Controls vs CSS lordy 0 1,609 11-22-2009 09:25 PM
Last Post: lordy

Forum Jump: