[LTP] [PATCH v3 2/5] syscalls/clone03: Convert to new API

Cyril Hrubis chrubis@suse.cz
Mon Oct 11 17:02:42 CEST 2021


Hi!
> > +static void verify_clone(void)
> > +{
> > +	int child_pid;
> >
> > -		/* Close read end from parent */
> > -		if ((close(pfd[0])) == -1)
> > -			tst_resm(TWARN | TERRNO, "close(pfd[0]) failed");
> > +	TST_EXP_PID_SILENT(ltp_clone(SIGCHLD, child_fn, NULL, CHILD_STACK_SIZE,
> > +				child_stack));
> 
> tst_clone is the new API.

Actually we can't use it as it is becuase:

- these tests are tests fof clone() not for clone3() so we really have
  to use the old syscall

- even if we added flag to tst_clone to force the older syscall we would
  have to add size parameter to the structure since we need to test the
  case where we pass the stack explicitly as well

I guess that it would be better to do this eventually but I do not want
to block the cleanup because of this. We can fix this later on.

> >
> > -		/* Get child's pid from pid string */
> > -		child_pid = atoi(buff);
> > +	if (!TST_PASS)
> > +		return;
> >
> > -		if (TEST_RETURN == child_pid)
> > -			tst_resm(TPASS, "Test passed");
> > -		else
> > -			tst_resm(TFAIL, "Test failed");
> > +	tst_reap_children();
> >
> > -		if ((wait(&status)) == -1)
> > -			tst_brkm(TBROK | TERRNO, cleanup,
> > -				 "wait failed, status: %d", status);
> > -	}
> > +	child_pid = atoi(channel);
> 
> atoi is deprecated (see the man page).
> 
> >
> > -	free(child_stack);
> > -
> > -	cleanup();
> > -	tst_exit();
> > +	TST_EXP_PASS(!(TST_RET == child_pid), "pid(%d)", child_pid);
> >  }
> >
> >  static void setup(void)
> >  {
> > -	tst_sig(FORK, DEF_HANDLER, cleanup);
> > -	TEST_PAUSE;
> > +	child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
> > +	channel = SAFE_MMAP(NULL, 10, PROT_READ | PROT_WRITE,
> > +				MAP_SHARED | MAP_ANONYMOUS, -1, 0);
> 
> You could mmap a region needed for pid_t and just read and write it like
> a normal variable.

That was my point when I suggested mmap()

it should really be:

static int *child_pid;


...
	child_pid = SAFE_MMAP(NULL, sizeof(*child_pid), ....);
...

Then we can just do *child_pid = foo and if (*child_pid == bar) in the
code.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list