elks icon indicating copy to clipboard operation
elks copied to clipboard

System info

Open toncho11 opened this issue 1 month ago • 5 comments

Hi @ghaerr

I need a userland library with 3 methods. It will be used by my Nano-x app. It seems getting the total and free conventional memory is really difficult from user land - without accessing specific kernel structures. It seems I can not use the method utilized by meminfo for example? Please assist me in writing a small helper library.

#ifndef TOOLS_H
#define TOOLS_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

/* get_pid: returns PID of a process by name */
static int get_pid(const char *name)
{
    FILE *fp;
    char line[128];
    int pid;
    char pname[64];

    fp = popen("/bin/ps", "r");
    if (!fp) return -1;

    while (fgets(line, sizeof(line), fp)) {
        if (sscanf(line, "%d %63s", &pid, pname) == 2) {
            if (strcmp(pname, name) == 0) {
                pclose(fp);
                return pid;
            }
        }
    }

    pclose(fp);
    return -1;
}

/* force_close: send SIGKILL to a PID */
static int force_close(int pid)
{
    if (kill(pid, SIGKILL) == 0) return 0;
    perror("force_close");
    return -1;
}

/* get_memory: returns total and free conventional memory (userland-only stub) */
static int get_memory(unsigned long *free_bytes, unsigned long *total_bytes)
{
    
}

#endif /* TOOLS_H */

toncho11 avatar Nov 23 '25 16:11 toncho11

Hello @toncho11,

Actually this has been on my list for a while. I can write such a routine, it will likely have to break the rules a bit and use access through /dev/kmem to get kernel variables so that installed system and free memory can be calculated in user land. I'm still traveling, but will get to this as soon as I can!

Thank you.

ghaerr avatar Nov 23 '25 16:11 ghaerr

Thank you!

Actually I want to be able to exit everything and go to the console, but if I have launched other Nano-X applications (from my own) and if I exit my own the others will keep Nano-X open. So my solution is to find the pid of Nano-X and kill it. Bu then ... that might leave me in VGA mode and it might leave the other Nano-X applications resident in memory even if they do not have Nano-X server any more. So the main problem is that there is no API that can signal the server to close all Nano-X apps and shutdown.

So in general I need:

  • a way to close all (my Nano-X, the other Nano-X apps and the Nano-X server) properly and go back to console
  • get the free/total conventional memory

toncho11 avatar Nov 23 '25 19:11 toncho11

a way to close all (my Nano-X, the other Nano-X apps and the Nano-X server) properly and go back to console

I've added that to my TODO list, let me think about the best way to accomplish that. Normally, the nano-X server will exit and return to text mode when the last nano-X client disconnects from it, but in this case perhaps sending a signal to exit gracefully will work. A potential issue will be that some NX apps may still be hung in a client/server read and won't gracefully exit - this will have to be tested.

ghaerr avatar Nov 24 '25 01:11 ghaerr

a way to close all (my Nano-X, the other Nano-X apps and the Nano-X server) properly and go back to console

I've added that to my TODO list, let me think about the best way to accomplish that. Normally, the nano-X server will exit and return to text mode when the last nano-X client disconnects from it, but in this case perhaps sending a signal to exit gracefully will work. A potential issue will be that some NX apps may still be hung in a client/server read and won't gracefully exit - this will have to be tested.

That is why one solution is to use "get_process_pid" and first process kill the other nxapps, kill the Nano-X server and finally exit my Nano-X app. Thank you!

toncho11 avatar Nov 24 '25 06:11 toncho11

There can be a new GrShutdown() Nano-X API function that sends GR_EVENT_TYPE_CLOSE_REQ to all apps. Than these apps will handle GR_EVENT_TYPE_CLOSE_REQ in their main loop. It seems most do. And then the Nano-X server will exit (maybe giving 2 seconds for apps to close themselves). But forceful exit still might be needed in the case the app is blocked in doing something and does not make a pass of the main loop.

toncho11 avatar Nov 30 '25 09:11 toncho11

I managed the memory handling. What I am missing currently is the proper close of everything as discussed above, but it is not currently a blocker issue.

toncho11 avatar Dec 08 '25 07:12 toncho11

The ability to shutdown Nano-X is now possible using the method implemented in https://github.com/ghaerr/microwindows/pull/154.

ghaerr avatar Dec 13 '25 19:12 ghaerr