-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to access global variable within the command function? #67
Comments
Do you have a short example? In our code we do not access libcli globals in our callbacks, except for those things that are held in the cli_struct* that is passed to us. |
I would like to apply the CLI with minimal changes to our heavy application that has already been developed. Therefore, handling of external global variables is inevitable. I need to be able to change external global variables in the CLI, but the test code I wrote doesn't do that. Here is the test code:
|
Think about what you are doing. In your main function you create one thread to monitor connections (CLI_Thread), and a second thread to periodically show the value of gflag. Both of these threads run in the same process. Now in CLI_Thread you wait for new connections, and fork each time you get one. So the child process is now handling the connection to the other end of the telnet sessions while the parent goes back to waiting for new connections. The fork() call creates a complete duplicate of the current process, but with a new PID. But unless you've taken steps to share variables the parent and child processes have their own separate copy of them. This means that when the child get a 'on' command to turn the flag on it goes and changes the copy of 'gflag' in its own process, but does not affect the copy of 'gflag' in the parent process. |
Thanks for the answer. I will look for ways to share variables between child and parent processes. Maybe before that, can you tell me what are some of the actions to share the variable? |
Several different methods - shared memory is one of them for example. Much depends on your application and what it is trying to do. |
Hi, @RobSanders : |
Libcli was initially designed to imitate a Cisco style IOS interface, waiting for the user to enter one command, executing it, returning any results, and then waiting for the next one. This is how our apps treat it. Basically a single thread of execution. |
Hi. I have a question while testing the library. I would like to know how to access global variables within the command function registered through the cli_register_command().
If the value of the global variable changed inside main() is printed using cli_print() in the command function, it is not reflected, and on the contrary, the value of the global variable changed inside the command function is not properly displayed in main().
Also, is there a way to telnet output using cli_print() inside another function not registered through cli_register_command()?
Do you ever have a similar problem?
The text was updated successfully, but these errors were encountered: