I was reviewing the code for a personal CLI command that uses the notification hook in clipboard.device. I noticed that my utility still exits if the hook removal fails. I checked all the examples on old OS3 developer CD's, the Changehook_Test.c example in the AmigaOS Documentation Wiki and in other example sources. In all cases the example programs still exit if the hook removal fails. Although a hook removal failure is unlikely, it doesn't seem like a good idea to exit a program and leave the hook in freed memory.
Is there a safe way to exit a program/command if a hook removal fails?
Program response to hook removal failure?
Program response to hook removal failure?
AmigaOne X1000 with 2GB memory - OS4.1 FE
- tonyw
- AmigaOS Core Developer
- Posts: 1483
- Joined: Wed Mar 09, 2011 1:36 pm
- Location: Sydney, Australia
Re: Program response to hook removal failure?
The problem is that you don't know WHY the removal failed. It could be that you passed the server the wrong Hook address and it could not find the address in its list of Hooks (the most likely problem). You could have already removed it earlier or never installed it at all. I can think of a number of possible failure scenarios, but in all cases, you have to ask "What else can the client do?" If you are cleaning up and you are going to exit anyway, what can you do but exit? If the server can't find your Hook or can't remove it, end of story. A server these days can ask the Exec if the Hook address is a valid, allocated memory address and ignore it if not.
You could of course post a warning message to the operator, but the average bunny won't know what to do with a message saying a callback Hook could not be removed.
You could of course post a warning message to the operator, but the average bunny won't know what to do with a message saying a callback Hook could not be removed.
cheers
tony
tony
Re: Program response to hook removal failure?
@tonyw
If those problems (or some others) were the cause of the "hook removal failure", the program would never be notified by the hook and never detect clipboard "pastes". Any programmer who tests a program would realize that something is wrong and fix it.The problem is that you don't know WHY the removal failed. It could be that you passed the server the wrong Hook address and it could not find the address in its list of Hooks (the most likely problem). You could have already removed it earlier or never installed it at all.
Notify the user and suspend the program by waiting for a signal or event that will never occur. I hoping there was a better solution; which is why I asked the question.If you are cleaning up and you are going to exit anyway, what can you do but exit?
You say "can ask Exec if the Hook address is valid" so the question is: Does clipboard.device ask Exec every time the hook is to be executed? If so, there is no problem with hook removal failure and a note to that effect should be added to the Changehook_Test.c example in the AmigaOS Documentation Wiki.A server these days can ask the Exec if the Hook address is a valid, allocated memory address and ignore it if not.
AmigaOne X1000 with 2GB memory - OS4.1 FE
- tonyw
- AmigaOS Core Developer
- Posts: 1483
- Joined: Wed Mar 09, 2011 1:36 pm
- Location: Sydney, Australia
Re: Program response to hook removal failure?
I'va had a look at the code for clipboard.device.
Firstly, the command CBD_CHANGEHOOK:
It returns error codes in io_Error:
io_Error = IOERR_BADADDRESS if the hook address (io_Data) is null.
io_Error = IOERR_BADLENGTH if the "Set"/"Remove" flag io_Length is neither 1 or 0.
io_Error = 0 otherwise.
On removal, it checks that the Hook is actually in its list before it tries to remove it, but returns no error indication if it is not.
Being old code, it does not use the "TypeOfMem()" facility in Exec to check the validity of each Hook before calling it. I shouldn't think most other system Hook-callers do, either.
Firstly, the command CBD_CHANGEHOOK:
It returns error codes in io_Error:
io_Error = IOERR_BADADDRESS if the hook address (io_Data) is null.
io_Error = IOERR_BADLENGTH if the "Set"/"Remove" flag io_Length is neither 1 or 0.
io_Error = 0 otherwise.
On removal, it checks that the Hook is actually in its list before it tries to remove it, but returns no error indication if it is not.
Being old code, it does not use the "TypeOfMem()" facility in Exec to check the validity of each Hook before calling it. I shouldn't think most other system Hook-callers do, either.
cheers
tony
tony