linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn@gclements.plus.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: script in c
Date: Mon, 8 Nov 2004 07:27:55 +0000	[thread overview]
Message-ID: <16783.8187.683414.241864@cerise.gclements.plus.com> (raw)
In-Reply-To: <20041108065024.35087.qmail@web14001.mail.yahoo.com>

[-- Attachment #1: message body and .signature --]
[-- Type: text/plain, Size: 990 bytes --]


> >            Is there a way by which I can exec a
> > shell script without
> > using the "system()" call?
> > Thanks in advance.
> 
> You can use exec*() family for this purpose.
> 
> man exec
> 
> Here is a little sample:
> 
> [mbaris@zion:/tmp/code_temp]$ cat exec.c
> #include <unistd.h>
> #include <string.h>
> #include <errno.h>
> 
> int main(void)
> {
>   if (execl("/bin/sh", "-c", "./script.sh", NULL)==-1)
>   {
>     printf("ERROR: %s\n", strerror(errno));
>   }
> 
>   return 0;
> }

A system() replacement is somewhat more complex than that.

If the process wants to continue executing after the script has
finished, you need to fork() a child process, have the child perform
the exec(), then wait() for the child to finish. You may also need to
change the signal handling while the child is executing (system()
ignores SIGINT and SIGQUIT and blocks SIGCHLD while the child process
runs).

A more complete example is attached.

-- 
Glynn Clements <glynn@gclements.plus.com>


[-- Attachment #2: process spawning example --]
[-- Type: text/plain, Size: 1363 bytes --]

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int spawn(char *command, char **args)
{
	/* sample usage:
	 *	
	 *	char *args[3];
	 *	
	 *	args[0] = "sh";
	 *	args[1] = "/path/to/script";
	 *	args[2] = NULL;
	 *	spawn("/bin/sh", args);
	 */

	struct sigaction act, intr, quit;
	sigset_t block, oldmask;
	int status = -1;
	pid_t pid;

	sigemptyset(&act.sa_mask);
	act.sa_flags = SA_RESTART;

	act.sa_handler = SIG_IGN;
	if (sigaction(SIGINT, &act, &intr) < 0)
		goto error_1;
	if (sigaction(SIGQUIT, &act, &quit) < 0)
		goto error_2;

	sigemptyset(&block);
	sigaddset(&block, SIGCHLD);
	if (sigprocmask(SIG_BLOCK, &block, &oldmask) < 0)
		goto error_3;

	pid = fork();

	if (pid < 0)
	{
		fprintf(stderr, "unable to create a new process");
		goto error_4;
	}

	if (pid == 0)
	{
		sigaction(SIGINT, &intr, NULL);
		sigaction(SIGQUIT, &quit, NULL);

		execvp(command, args);
		/* if we reach this point, execvp() failed */
		fprintf(stderr, "unable to execute command");
		_exit(127);
	}
	else
	{
		pid_t n;

		do n = waitpid(pid, &status, 0);
		while (n == (pid_t) -1 && errno == EINTR);

		if (n != pid)
			status = -1;
	}

error_4:
	sigprocmask(SIG_SETMASK, &oldmask, NULL);
error_3:
	sigaction(SIGQUIT, &quit, NULL);
error_2:
	sigaction(SIGINT, &intr, NULL);
error_1:
	return status;
}

  reply	other threads:[~2004-11-08  7:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-08  6:50 script in c linux-c-programming-owner
2004-11-08  7:27 ` Glynn Clements [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-11-08  6:15 Honnavalli_Sreevathsa
2004-11-08  6:32 ` Ron Michael Khu
2004-11-08  6:48   ` Jan-Benedict Glaw
2004-11-08  5:39 kaushal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=16783.8187.683414.241864@cerise.gclements.plus.com \
    --to=glynn@gclements.plus.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).