Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:40:07+00:00 2026-05-26T23:40:07+00:00

I have the following code in a kernel module that walks up the process

  • 0

I have the following code in a kernel module that walks up the process tree and prints the process names and uids up until the init process:

// recursivly walk the task's parent until we reach init

void parent_task_walk(struct task_struct *task) {

    struct task_struct *parent;
    char filename[MAX_FILE_LEN];

    if (task && task->mm) {

        parent = get_task_parent(task);

        printk("%s (uid:%d)", exe_from_mm(task->mm, filename, MAX_FILE_LEN),
            get_task_uid(task));

        if (parent && task->pid != 1) {
            printk(", ");
            parent_task_walk(parent);
        }
    }

}

NOTE: I make use of some macros that reference the real kernel functions, as this is for a kernel module the spans multiple versions. The source code is in this file: https://github.com/cormander/tpe-lkm/blob/319e1e29ea23055cca1c0a3bce3c865def14d3d2/core.c#L61

The output ends up looking something like this:

/bin/bash (uid:500), /usr/sbin/sshd (uid:500), /usr/sbin/sshd (uid:0), /usr/sbin/sshd (uid:0), /sbin/init (uid:0)

It’s a recursive function. As you can imagine, things go bad when you launch 200 bash shells and then trigger the event. I’m not sure exactly what happens, but the machine freezes. Kernel ran out of stack space I’m assuming, went OOM, and shot itself?

I’m wondering what’s the best way to handle that case. I see a few options:

1) stop walking the process tree after N processes

2) stop walking after some character array (that will eventually be printed) is full

3) make use of a goto instead of a recursive function, and yet still obey a new rule from options #1 and #2

4) use some other non-recursion method that you’ll articulate for me

This is happening in kernel space, so not the most hospitable environment. Can anyone give any pointers as to the best approach to take for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:40:08+00:00Added an answer on May 26, 2026 at 11:40 pm

    Indeed you kernel crash probably happens because you are using a recursive function.

    You could just use a very simple loop instead of your recursive function… Do you come from a functional programming only world? A while loop is standard in C, but if you really want to use a goto you could… 😉 Here is the code using a loop.

    void parent_task_walk(struct task_struct* task) 
    {
    
        struct task_struct *parent = NULL;
        char filename[MAX_FILE_LEN];
    
    
        while (task && task->mm) {
    
            parent = get_task_parent(task);
    
            printk("%s (uid:%d)",
                   exe_from_mm(task->mm, filename, MAX_FILE_LEN),
                   get_task_uid(task));
    
            if (parent && task->pid != 1)
                printk(", ");
    
            task = parent;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a CUDA kernel, I have code similar to the following. I am trying
I have following code snippet that i use to compile class at the run
I have a module that I load it by Kernel when my application starts
I have these code lines following: load() { float* host; // init host done
I have the following code package myPackage; import org.neo4j.graphdb; import org.neo4j.kernel.EmbeddedGraphDatabase; public class dbServlet
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.