You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how to access global variable within the command function? #67
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 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.
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.
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?
Hi, @RobSanders :
Like this issue, how could i use libcli a manager thread to manage a process. I want to add a console manager interface for a process, it maybe need a api like cli_loop can return EAGAIN, and NOT run in fork mode, so that i can execute some command in same process space.
Or libcli is not design for this?
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.
Activity
RobSanders commentedon Jan 19, 2021
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.
HyunjunAhn commentedon Jan 19, 2021
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:
RobSanders commentedon Jan 19, 2021
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.
HyunjunAhn commentedon Jan 20, 2021
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?
RobSanders commentedon Jan 20, 2021
Several different methods - shared memory is one of them for example. Much depends on your application and what it is trying to do.
pengtianabc commentedon Aug 12, 2024
Hi, @RobSanders :
Like this issue, how could i use libcli a manager thread to manage a process. I want to add a console manager interface for a process, it maybe need a api like cli_loop can return EAGAIN, and NOT run in fork mode, so that i can execute some command in same process space.
Or libcli is not design for this?
RobSanders commentedon Aug 12, 2024
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.