LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2010, 09:16 AM   #1
scao
LQ Newbie
 
Registered: Jun 2010
Posts: 1

Rep: Reputation: 0
Question on posix_spawnp on Linux


When trying to emulate the behavior of system(3), I noticed a different
behavior of posix_spawnp on Linux (AMD and IBM) from SUN and AIX. Here's
the code snippet to spawn a child process and have it run:
/bin/sh -c "ls -l /etc"
(return value checking is omitted):

pid_t pid;
sigset_t sigset;
int rc = 0;
posix_spawnattr_t attr;
short flags = 0;
int status, ret;
extern char** environ;
char *const argv[] = { "/bin/sh", "-c", "ls -l /etc", NULL };

rc = posix_spawnattr_init( &attr );

// Set all signal actions to defaults in child
sigfillset( &sigset );
rc = posix_spawnattr_setsigdefault( &attr, &sigset );

// Empty signal mask in child
sigemptyset( &sigset );
rc = posix_spawnattr_setsigmask( &attr, &sigset );

flags |= (POSIX_SPAWN_SETSIGDEF|POSIX_SPAWN_SETSIGMASK);
rc = posix_spawnattr_setflags( &attr, flags );

rc = posix_spawnp( &pid, argv[0], NULL, &attr, argv, environ );
posix_spawnattr_destroy( &attr );

ret = waitpid( pid, &status, 0 );

if( ret == -1 ) {
// waitpid has failed
rc = errno;
}

if( status != 0 ) {
printf("status = %d\n", status);
if( WIFEXITED( status ) ) {
ret = WEXITSTATUS( status );
} else {
ret = -1;
}

printf("ret = %d\n", ret);
}

On Linux, the call to posix_spawnp() here spawns the child process which
immediately becomes defunct and does NOT run the command passed in.
status is 32512 and ret 127.

On SUN and AIX, the code as it is works as expected, and status is 0.

However, on Linux, if NULL instead of $attr is passed into posix_spawnp
here, it works as expected. This is also true on SUN and AIX. So NULL
appears to make the code portable across SUN, AIX and Linux.

I'd like to understand why there is the difference and if using NULL in
place of &attr is indeed the right approach to emulate system(3). Any
insights would be really appreciated.
 
Old 07-16-2010, 10:55 AM   #2
zagzagel
Member
 
Registered: Jul 2010
Location: Portugal
Distribution: Arch Linux
Posts: 31

Rep: Reputation: 1
Try making "ls -l /etc" into "ls", "-l", "/etc". If you run "ls -l /etc" on bash it says it can't find the process. I know that sh -c "ls -l /etc" works but from my experience, separation of arguments is a bit strange in sh and it's treatment is different from the kernel (I assume the spawn call is dealed by the kernel).

Just trying to help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux distro question & mysql install question natalie.aloi Linux - Newbie 5 07-19-2009 08:28 PM
Non Linux User Question: (closest to) Standard Video Format on Linux escherwisser Linux - Newbie 3 05-18-2009 09:41 AM
Question writing workshop for Linux+, experienced Linux administrators needed cbalkcom Linux - News 1 10-12-2008 03:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration