PIPE: issue

This forum is for general developer support questions.
Post Reply
User avatar
alfkil
Posts: 67
Joined: Mon Oct 24, 2011 8:46 pm
Location: Denmark

PIPE: issue

Post by alfkil »

When working with Spotless (the debugger), I am running into a problem with the PIPE: device.

The problem arises, when I load Spotless into Spotless (self-debugging). Upon Start (IExec->RestartTask()) the child will crash on this line :

Code: Select all

fd[1] = IDOS->Open ("PIPE:/UNIQUE/NOBLOCK", MODE_NEWFILE);
The child is created using the following line of code :

Code: Select all

    process = IDOS->CreateNewProcTags(
		NP_Seglist,					seglist,
//		NP_Entry,					foo,
		NP_FreeSeglist,				false, //important
		NP_Name,					strdup(command.c_str()),
		// NP_CurrentDir,				lock,
		// NP_ProgramDir,				homelock,
		// NP_StackSize,				2000000,
		NP_Cli,						true,
		NP_Child,					false, //important
		NP_Arguments,				arguments.c_str(),
		NP_Input,					IDOS->Input(),
		NP_CloseInput,				false,
		NP_Output,					IDOS->Output(), //pipe.getWrite(),
		NP_CloseOutput,				false,
		NP_Error,					IDOS->ErrorOutput(),
		NP_CloseError,				false,
		NP_NotifyOnDeathSigTask,	IExec->FindTask(0),
		TAG_DONE
	);
Does anyone have any ideas, why this fails? The only solution, I have come up with so far, is to remove the references to PIPE: entirely. Then the subprocess will start and finish problem free.
User avatar
billyfish
Posts: 7
Joined: Thu Feb 28, 2013 11:39 am

Re: PIPE: issue

Post by billyfish »

Hi

As a quick question, what's the scope of arguments? As if that goes out of scope, then maybe the value passed in for NP_Arguments would be garbage?

billy
User avatar
alfkil
Posts: 67
Joined: Mon Oct 24, 2011 8:46 pm
Location: Denmark

Re: PIPE: issue

Post by alfkil »

@billyfish

Thanks! It is definitely a good thing to safeguard these variables. I have inserted a strdup. Sadly it didn't solve the problem. :(
User avatar
colinw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 207
Joined: Mon Aug 15, 2011 9:20 am
Location: Brisbane, QLD. Australia.

Re: PIPE: issue

Post by colinw »

Every tag gets a free autodoc.

* NP_Name (STRPTR) -- Name of the child process. Defaults to
* "New Process" if no tag is specified, or a NULL tag is supplied.
* There is no maximum length limit imposed on this string.
* The child process' task structure; task->tc_Node.ln_Name
* will point to the buffer to where this string is copied.
* The exec function FindTask() searches on this string.
*
* From V50, the internal copy of your NP_Name string is added to
* the task memory list and will therefore be freed automatically
* when the process eventually exits.
* DO NOT use this tag with anything but a nul-terminated c-string.
*
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: PIPE: issue

Post by salass00 »

@alfkil

In your code you are passing the parent Input() file handle directly to the child using NP_Input.

If you read the WARNING section in the dos.library/CreateNewProcTags autodoc entry (second paragraph) you can find there an explanation why doing this and providing NP_Arguments is bad:
NEVER specify NP_Input,ZERO if you also specify NP_Arguments tag,
When 'NP_Arguments' is specified, it needs to modify the Input()
stream filehandle buffer to make ReadArgs() and others work properly.
If NP_Arguments is needed, the best thing is to not specify NP_Input
at all, so a valid NIL: stream will be allocated automatically.

NEVER share the NP_Input filehandle with other processes, as the
input buffer is modified to make ReadArgs() and other work,
this will cause a race condition in the buffer restoration on exit.
If you must supply an NP_Input, use a DupFileHandle() copy of it.
User avatar
alfkil
Posts: 67
Joined: Mon Oct 24, 2011 8:46 pm
Location: Denmark

Re: PIPE: issue

Post by alfkil »

@colinw

Ok, so the strdup() is redundant. Good to know!

@salass00

If I understand this correctly, then DupFileHandle() at the NP_Input tag is the way to go? Or I am not able to specficy NP_Arguments and NP_Input simultaneously? This is a little confusing. I can remove either one of them, but it is going to degrade the amount of functionality in the application.
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: PIPE: issue

Post by salass00 »

You should pass a copy of the Input() file handle (as gained from DupFileHandle()), rather than the file handle itself. Also there is no need to make a copy of the string for NP_Arguments (dos.library should handle that according to the autodoc).

Edit: That is if you want the program to have the same stdin input stream as the parent. If a NIL: input stream is what you want instead then you should remove NP_Input or pass Open("NIL:", MODE_OLDFILE) instead.
User avatar
alfkil
Posts: 67
Joined: Mon Oct 24, 2011 8:46 pm
Location: Denmark

Re: PIPE: issue

Post by alfkil »

@salass00

That is good to know. Thanks! I will modify the code accordingly.

For now, I have solved the original problem of the thread. It happened, because there was a hidden call to newlib from inside the debug hook. Or at least removing the newlib call changed the behavior to not crashing.
Post Reply