Git Product home page Git Product logo

macos-native-processlist's Introduction

macos-native-processlist

Build

Loads list of processes on macOS natively, without any bullshit ps output parsing

Usage

import { getProcessList } from 'macos-native-processlist'

getProcessList().then(processes => {
  for (let process of processes) {
    console.log(process.pid, process.name, process.children.length)
  }
})

For the full API look at the typings file.

macos-native-processlist's People

Contributors

dependabot-preview[bot] avatar eugeny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

macos-native-processlist's Issues

Potential bugs? Memory leak and mixing malloc and delete

In Worker's constructor, it allocates memory using new:

Worker::Worker(Napi::Function &callback) : AsyncWorker(callback)
{
this->processes = new kinfo_proc[1024];
processCount = new uint32_t;
}
But then in Worker::Execute(), it assigns NULL to this->processes, which cause memory leak:
this->processes = NULL;

In Worker::Execute, it allocates memory using malloc and assigns to this->processes:

this->processes = (kinfo_proc *)malloc(length);
Finally in Worker's destructor the malloced memory is passed to delete[], which is an undefined behaviour:
Worker::~Worker()
{
delete[] this->processes;
delete processCount;
}

My solution:
worker.cc:

#include "worker.h"
#include <sys/sysctl.h>
#include <libproc.h>
#include <system_error>

Worker::Worker(Napi::Function &callback) : AsyncWorker(callback), processCount(0)
{
}

Worker::~Worker()
{
}

void Worker::Execute()
{
    static int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};

    while (1)
    {
        size_t length = 0;
        int err = sysctl(mib, std::size(mib), nullptr, &length, nullptr, 0);
        if (err == -1) // This should not fail normally, throw if failed
            throw std::system_error(errno, std::generic_category());

        this->processes = std::make_unique<kinfo_proc[]>(length / sizeof(kinfo_proc));

        err = sysctl(mib, std::size(mib), this->processes.get(), &length, nullptr, 0);
        if (err == -1)
        {
            if (errno != ENOMEM) // Try again if we got ENOMEM, otherwise throw
                throw std::system_error(errno, std::generic_category());
        }
        else
        {
            this->processCount = length / sizeof(kinfo_proc);
            break;
        }
    }
}

constexpr size_t NAME_BUFFER_SIZE = 4096;

void Worker::OnOK()
{
    auto env = this->Env();
    auto result = Napi::Array::New(env);
    for (size_t i = 0; i < this->processCount; i++)
    {
        auto object = Napi::Object::New(env);

        char buffer[NAME_BUFFER_SIZE];
        proc_name(this->processes[i].kp_proc.p_pid, buffer, NAME_BUFFER_SIZE);
        object.Set("name", Napi::String::New(env, buffer));
        proc_pidpath(this->processes[i].kp_proc.p_pid, buffer, NAME_BUFFER_SIZE);
        object.Set("path", Napi::String::New(env, buffer));
        object.Set("pid", Napi::Number::New(env, this->processes[i].kp_proc.p_pid));
        object.Set("ppid", Napi::Number::New(env, this->processes[i].kp_eproc.e_ppid));
        object.Set("pgid", Napi::Number::New(env, this->processes[i].kp_eproc.e_pgid));
        result.Set(i, object);
    }

    this->Callback().Call({result});
}

worker.h:

#pragma once

#include <napi.h>
#include <memory>

class Worker : public Napi::AsyncWorker
{
public:
    Worker(Napi::Function &callback);
    ~Worker();

    void Execute();
    void OnOK();

private:
    std::unique_ptr<kinfo_proc[]> processes;
    size_t processCount;
};

node-addon-api will catch the exception, so it's safe to throw here:
https://github.com/nodejs/node-addon-api/blob/01274966d57017269142dfae9ba9e0f70e02bb43/napi-inl.h#L4921-L4931

I don't have macOS development environment, so I don't know if there's any issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.