Skip to content

Instantly share code, notes, and snippets.

@su8
Created September 3, 2018 09:41
Show Gist options
  • Save su8/37d31d3c6e4b0300d743e14df4e641ad to your computer and use it in GitHub Desktop.
Save su8/37d31d3c6e4b0300d743e14df4e641ad to your computer and use it in GitHub Desktop.
/*
09/03/2018
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <kvm.h>
int main (void) {
struct kinfo_proc *kp = NULL;
int x = 1;
int count = 0;
FILE *fp = NULL;
kvm_t *kd = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, "error: ");
if (NULL == kd) {
return EXIT_FAILURE;
}
if (NULL == (fp = fopen("/tmp/log", "a"))) {
goto err;
}
kp = kvm_getprocs(kd, KERN_PROC_PROC, 0, &count);
for (; x < count; x++) {
fprintf(fp, "%d %s",kp[x].ki_pid, kp[x].ki_comm);
}
err:
if (NULL != fp) {
fclose(fp);
}
kvm_close(kd);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment