/filesystems/procfs/procfs.cc
http://macfuse.googlecode.com/ · C++ · 4975 lines · 4101 code · 796 blank · 78 comment · 606 complexity · 8905523075cf982ed57d446bcdf473d7 MD5 · raw file
Large files are truncated click here to view the full file
- /*
- * procfs as a MacFUSE file system for Mac OS X
- *
- * Copyright Amit Singh. All Rights Reserved.
- * http://osxbook.com
- *
- * http://code.google.com/p/macfuse/
- *
- * Source License: GNU GENERAL PUBLIC LICENSE (GPL)
- */
- #define MACFUSE_PROCFS_VERSION "2.0"
- #define FUSE_USE_VERSION 26
- #include <dirent.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <getopt.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/sysctl.h>
- #include <grp.h>
- #include <pwd.h>
- #include <mach/mach.h>
- #include <mach/mach_vm.h>
- #include <mach/vm_region.h>
- #include <mach/vm_statistics.h>
- #include <Carbon/Carbon.h>
- #include <CoreFoundation/CoreFoundation.h>
- #include <IOKit/IOKitLib.h>
- #include <cassert>
- #include <vector>
- #include <pcrecpp.h>
- #include <fuse.h>
- #include "procfs_displays.h"
- #include "procfs_proc_info.h"
- #include "procfs_windows.h"
- #include "sequencegrab/procfs_sequencegrab.h"
- #if MACFUSE_PROCFS_ENABLE_TPM
- #include "procfs_tpm.h"
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- static int procfs_ui = 0;
- #define PROCFS_DEFAULT_FILE_SIZE 65536
- static int total_file_patterns = 0;
- static int total_directory_patterns = 0;
- static int total_link_patterns = 0;
- static processor_port_array_t processor_list;
- static mach_port_t p_default_set = 0;
- static mach_port_t p_default_set_control = 0;
- static host_priv_t host_priv;
- static natural_t processor_count = 0;
- static io_connect_t lightsensor_port = 0;
- static io_connect_t motionsensor_port = 0;
- static unsigned int sms_gIndex = 0;
- static IOItemCount sms_gStructureInputSize = 0;
- static IOByteCount sms_gStructureOutputSize = 0;
- /* camera */
- static pthread_mutex_t camera_lock;
- static int camera_busy = 0;
- static CFMutableDataRef camera_tiff = (CFMutableDataRef)0;
- /* display */
- static pthread_mutex_t display_lock;
- static int display_busy = 0;
- static CFMutableDataRef display_png = (CFMutableDataRef)0;
- static pcrecpp::RE *valid_process_pattern = new pcrecpp::RE("/(\\d+)");
- typedef struct {
- char x;
- char y;
- char z;
- short v;
- #define FILLER_SIZE 60
- char scratch[FILLER_SIZE];
- } MotionSensorData_t;
- static kern_return_t
- sms_getOrientation_hardware_apple(MotionSensorData_t *odata)
- {
- kern_return_t kr;
- IOItemCount isize = sms_gStructureInputSize;
- IOByteCount osize = sms_gStructureOutputSize;
- MotionSensorData_t idata;
- kr = IOConnectMethodStructureIStructureO(motionsensor_port,
- sms_gIndex,
- isize,
- &osize,
- &idata,
- odata);
- return kr;
- }
- static int init_task_list(task_array_t *task_list,
- mach_msg_type_number_t *task_count)
- {
- return processor_set_tasks(p_default_set_control, task_list, task_count);
- }
- static void fini_task_list(task_array_t task_list,
- mach_msg_type_number_t task_count)
- {
- unsigned int i;
- for (i = 0; i < task_count; i++) {
- mach_port_deallocate(mach_task_self(), task_list[i]);
- }
- vm_deallocate(mach_task_self(), (vm_address_t)task_list,
- task_count * sizeof(task_t));
- }
- static int init_thread_list(task_t the_task,
- thread_array_t *thread_list,
- mach_msg_type_number_t *thread_count)
- {
- return task_threads(the_task, thread_list, thread_count);
- }
- static void fini_thread_list(thread_array_t thread_list,
- mach_msg_type_number_t thread_count)
- {
- unsigned int i;
- for (i = 0; i < thread_count; i++) {
- mach_port_deallocate(mach_task_self(), thread_list[i]);
- }
- vm_deallocate(mach_task_self(), (vm_address_t)thread_list,
- thread_count * sizeof(thread_act_t));
- }
- static int init_port_list(task_t the_task,
- mach_port_name_array_t *name_list,
- mach_msg_type_number_t *name_count,
- mach_port_type_array_t *type_list,
- mach_msg_type_number_t *type_count)
- {
- return mach_port_names(the_task,
- name_list, name_count, type_list, type_count);
- }
- static void fini_port_list(mach_port_name_array_t name_list,
- mach_msg_type_number_t name_count,
- mach_port_type_array_t type_list,
- mach_msg_type_number_t type_count)
- {
- vm_deallocate(mach_task_self(), (vm_address_t)name_list,
- name_count * sizeof(mach_port_name_t));
- vm_deallocate(mach_task_self(), (vm_address_t)type_list,
- type_count * sizeof(mach_port_type_t));
- }
-
- #define DECL_PORT_LIST() \
- mach_port_name_array_t name_list; \
- mach_msg_type_number_t name_count; \
- mach_port_type_array_t type_list; \
- mach_msg_type_number_t type_count;
- #define INIT_PORT_LIST(the_task) \
- if (init_port_list(the_task, &name_list, &name_count, &type_list, &type_count) != 0) { \
- return -EIO; \
- }
- #define FINI_PORT_LIST() \
- fini_port_list(name_list, name_count, type_list, type_count)
- #define DECL_TASK_LIST() \
- task_array_t task_list; \
- mach_msg_type_number_t task_count;
- #define INIT_TASK_LIST() \
- if (init_task_list(&task_list, &task_count) != 0) { return -EIO; }
- #define FINI_TASK_LIST() \
- fini_task_list(task_list, task_count)
- #define DECL_THREAD_LIST() \
- thread_array_t thread_list; \
- mach_msg_type_number_t thread_count;
- #define INIT_THREAD_LIST(the_task) \
- if (init_thread_list(the_task, &thread_list, &thread_count) != 0) { \
- return -EIO; \
- }
- #define FINI_THREAD_LIST() \
- fini_thread_list(thread_list, thread_count)
- struct procfs_dispatcher_entry;
- typedef struct procfs_dispatcher_entry * procfs_dispatcher_entry_t;
- typedef int (*procfs_open_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- const char *path,
- struct fuse_file_info *fi);
- typedef int (*procfs_release_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- const char *path,
- struct fuse_file_info *fi);
- typedef int (*procfs_opendir_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- const char *path,
- struct fuse_file_info *fi);
- typedef int (*procfs_releasedir_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- const char *path,
- struct fuse_file_info *fi);
- typedef int (*procfs_getattr_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- struct stat *stbuf);
- typedef int (*procfs_read_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- char *buf,
- size_t size,
- off_t offset,
- struct fuse_file_info *fi);
- typedef int (*procfs_readdir_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- void *buf,
- fuse_fill_dir_t filler,
- off_t offset,
- struct fuse_file_info *fi);
- typedef int (*procfs_readlink_handler_t)(procfs_dispatcher_entry_t e,
- const char *argv[],
- char *buf,
- size_t size);
- typedef struct procfs_dispatcher_entry {
- int flag;
- char *pattern;
- pcrecpp::RE *compiled_pattern;
- int argc;
- procfs_open_handler_t open;
- procfs_release_handler_t release;
- procfs_opendir_handler_t opendir;
- procfs_releasedir_handler_t releasedir;
- procfs_getattr_handler_t getattr;
- procfs_read_handler_t read;
- procfs_readdir_handler_t readdir;
- procfs_readlink_handler_t readlink;
- const char *content_files[32];
- const char *content_directories[32];
- };
- /* flags */
- #define PROCFS_FLAG_ISDOTFILE 0x00000001
- #define PROCFS_MAX_ARGS 3
- #define OPEN_HANDLER(handler) \
- int \
- procfs_open_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- const char *path, \
- struct fuse_file_info *fi) \
- #define RELEASE_HANDLER(handler) \
- int \
- procfs_release_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- const char *path, \
- struct fuse_file_info *fi) \
- #define OPENDIR_HANDLER(handler) \
- int \
- procfs_opendir_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- const char *path, \
- struct fuse_file_info *fi) \
- #define RELEASEDIR_HANDLER(handler) \
- int \
- procfs_releasedir_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- const char *path, \
- struct fuse_file_info *fi) \
- #define GETATTR_HANDLER(handler) \
- int \
- procfs_getattr_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- struct stat *stbuf) \
- #define READ_HANDLER(handler) \
- int \
- procfs_read_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- char *buf, \
- size_t size, \
- off_t offset, \
- struct fuse_file_info *fi) \
- #define READDIR_HANDLER(handler) \
- int \
- procfs_readdir_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- void *buf, \
- fuse_fill_dir_t filler, \
- off_t offset, \
- struct fuse_file_info *fi) \
- #define READLINK_HANDLER(handler) \
- int \
- procfs_readlink_##handler(procfs_dispatcher_entry_t e, \
- const char *argv[], \
- char *buf, \
- size_t size) \
- #define PROTO_OPEN_HANDLER(handler) OPEN_HANDLER(handler)
- #define PROTO_RELEASE_HANDLER(handler) RELEASE_HANDLER(handler)
- #define PROTO_OPENDIR_HANDLER(handler) OPENDIR_HANDLER(handler)
- #define PROTO_RELEASEDIR_HANDLER(handler) RELEASEDIR_HANDLER(handler)
- #define PROTO_READ_HANDLER(handler) READ_HANDLER(handler)
- #define PROTO_READDIR_HANDLER(handler) READDIR_HANDLER(handler)
- #define PROTO_READLINK_HANDLER(handler) READLINK_HANDLER(handler)
- #define PROTO_GETATTR_HANDLER(handler) GETATTR_HANDLER(handler)
- #define DECL_FILE(pattern, argc, openp, releasep, getattrp, readp) \
- { \
- 0, \
- pattern, \
- new pcrecpp::RE(pattern), \
- argc, \
- procfs_open_##openp, \
- procfs_release_##releasep, \
- procfs_opendir_enotdir, \
- procfs_releasedir_enotdir, \
- procfs_getattr_##getattrp, \
- procfs_read_##readp, \
- procfs_readdir_enotdir, \
- procfs_readlink_einval, \
- { NULL }, \
- { NULL } \
- },
- #define DECL_FILE_WITHFLAGS(flag, pattern, argc, openp, releasep, getattrp, readp) \
- { \
- flag, \
- pattern, \
- new pcrecpp::RE(pattern), \
- argc, \
- procfs_open_##openp, \
- procfs_release_##releasep, \
- procfs_opendir_enotdir, \
- procfs_releasedir_enotdir, \
- procfs_getattr_##getattrp, \
- procfs_read_##readp, \
- procfs_readdir_enotdir, \
- procfs_readlink_einval, \
- { NULL }, \
- { NULL } \
- },
- #define DECL_DIRECTORY(pattern, argc, opendirp, releasedirp, getattrp, readdirp, contents, ...) \
- { \
- 0, \
- pattern, \
- new pcrecpp::RE(pattern), \
- argc, \
- procfs_open_eisdir, \
- procfs_release_eisdir, \
- procfs_opendir_##opendirp, \
- procfs_releasedir_##releasedirp, \
- procfs_getattr_##getattrp, \
- procfs_read_eisdir, \
- procfs_readdir_##readdirp, \
- procfs_readlink_einval, \
- contents, \
- __VA_ARGS__ \
- },
- #define DECL_DIRECTORY_COMPACT(pattern, contents, ...) \
- { \
- 0, \
- pattern, \
- new pcrecpp::RE(pattern), \
- 0, \
- procfs_open_eisdir, \
- procfs_release_eisdir, \
- procfs_opendir_default_directory, \
- procfs_releasedir_default_directory, \
- procfs_getattr_default_directory, \
- procfs_read_eisdir, \
- procfs_readdir_default, \
- procfs_readlink_einval, \
- contents, \
- ##__VA_ARGS__ \
- },
- #define DECL_LINK(pattern, argc, openp, releasep, getattrp, readlinkp) \
- { \
- 0, \
- pattern, \
- new pcrecpp::RE(pattern), \
- argc, \
- procfs_open_##openp, \
- procfs_release_##releasep, \
- procfs_opendir_enotdir, \
- procfs_releasedir_enotdir, \
- procfs_getattr_##getattrp, \
- procfs_read_einval, \
- procfs_readdir_enotdir, \
- procfs_readlink_##readlinkp, \
- { NULL }, \
- { NULL } \
- },
- #define DECL_LINK_COMPACT(pattern, argc, readlinkp) \
- { \
- 0, \
- pattern, \
- new pcrecpp::RE(pattern), \
- argc, \
- procfs_open_default_file, \
- procfs_release_default_file, \
- procfs_opendir_enotdir, \
- procfs_releasedir_enotdir, \
- procfs_getattr_default_link, \
- procfs_read_einval, \
- procfs_readdir_enotdir, \
- procfs_readlink_##readlinkp, \
- { NULL }, \
- { NULL } \
- },
- PROTO_OPEN_HANDLER(default_file);
- PROTO_OPEN_HANDLER(eisdir);
- PROTO_OPEN_HANDLER(proc__windows__identify);
- PROTO_OPEN_HANDLER(proc__windows__screenshots__window);
- PROTO_OPEN_HANDLER(system__hardware__camera__screenshot);
- PROTO_OPEN_HANDLER(system__hardware__displays__display__screenshot);
- PROTO_RELEASE_HANDLER(default_file);
- PROTO_RELEASE_HANDLER(eisdir);
- PROTO_RELEASE_HANDLER(proc__windows__identify);
- PROTO_RELEASE_HANDLER(proc__windows__screenshots__window);
- PROTO_RELEASE_HANDLER(system__hardware__camera__screenshot);
- PROTO_RELEASE_HANDLER(system__hardware__displays__display__screenshot);
- PROTO_OPENDIR_HANDLER(default_directory);
- PROTO_OPENDIR_HANDLER(enotdir);
- PROTO_RELEASEDIR_HANDLER(default_directory);
- PROTO_RELEASEDIR_HANDLER(enotdir);
- PROTO_GETATTR_HANDLER(default_file);
- PROTO_GETATTR_HANDLER(default_file_finder_info);
- PROTO_GETATTR_HANDLER(default_directory);
- PROTO_GETATTR_HANDLER(default_link);
- PROTO_GETATTR_HANDLER(byname__name);
- PROTO_GETATTR_HANDLER(system__hardware__camera__screenshot);
- PROTO_GETATTR_HANDLER(system__hardware__displays__display);
- PROTO_GETATTR_HANDLER(system__hardware__displays__display__screenshot);
- #if MACFUSE_PROCFS_ENABLE_TPM
- PROTO_GETATTR_HANDLER(system__hardware__tpm__keyslots__slot);
- PROTO_GETATTR_HANDLER(system__hardware__tpm__pcrs__pcr);
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- PROTO_GETATTR_HANDLER(proc__task__ports__port);
- PROTO_GETATTR_HANDLER(proc__task__threads__thread);
- PROTO_GETATTR_HANDLER(proc__windows__screenshots__window);
- PROTO_READ_HANDLER(einval);
- PROTO_READ_HANDLER(eisdir);
- PROTO_READ_HANDLER(zero);
- PROTO_READ_HANDLER(default_file_finder_info);
- PROTO_READ_HANDLER(proc__carbon);
- #if __i386__
- PROTO_READ_HANDLER(proc__fds);
- #endif /* __i386__ */
- PROTO_READ_HANDLER(proc__generic);
- PROTO_READ_HANDLER(proc__task__absolutetime_info);
- PROTO_READ_HANDLER(proc__task__basic_info);
- PROTO_READ_HANDLER(proc__task__events_info);
- PROTO_READ_HANDLER(proc__task__thread_times_info);
- PROTO_READ_HANDLER(proc__task__mach_name);
- PROTO_READ_HANDLER(proc__task__ports__port);
- PROTO_READ_HANDLER(proc__task__role);
- PROTO_READ_HANDLER(proc__task__threads__thread__basic_info);
- PROTO_READ_HANDLER(proc__task__threads__thread__states__debug);
- PROTO_READ_HANDLER(proc__task__threads__thread__states__exception);
- PROTO_READ_HANDLER(proc__task__threads__thread__states__float);
- PROTO_READ_HANDLER(proc__task__threads__thread__states__thread);
- PROTO_READ_HANDLER(proc__task__tokens);
- PROTO_READ_HANDLER(proc__task__vmmap);
- PROTO_READ_HANDLER(proc__task__vmmap_r);
- PROTO_READ_HANDLER(proc__windows__generic);
- PROTO_READ_HANDLER(proc__windows__screenshots__window);
- PROTO_READ_HANDLER(proc__xcred);
- PROTO_READ_HANDLER(system__firmware__variables);
- PROTO_READ_HANDLER(system__hardware__camera__screenshot);
- PROTO_READ_HANDLER(system__hardware__cpus__cpu__data);
- PROTO_READ_HANDLER(system__hardware__displays__display__info);
- PROTO_READ_HANDLER(system__hardware__displays__display__screenshot);
- #if MACFUSE_PROCFS_ENABLE_TPM
- PROTO_READ_HANDLER(system__hardware__tpm__hwmodel);
- PROTO_READ_HANDLER(system__hardware__tpm__hwvendor);
- PROTO_READ_HANDLER(system__hardware__tpm__hwversion);
- PROTO_READ_HANDLER(system__hardware__tpm__keyslots__slot);
- PROTO_READ_HANDLER(system__hardware__tpm__pcrs__pcr);
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- PROTO_READ_HANDLER(system__hardware__xsensor);
- PROTO_READDIR_HANDLER(default);
- PROTO_READDIR_HANDLER(enotdir);
- PROTO_READDIR_HANDLER(byname);
- PROTO_READDIR_HANDLER(proc__task__ports);
- PROTO_READDIR_HANDLER(proc__task__threads);
- PROTO_READDIR_HANDLER(proc__windows__screenshots);
- PROTO_READDIR_HANDLER(root);
- PROTO_READDIR_HANDLER(system__hardware__cpus);
- PROTO_READDIR_HANDLER(system__hardware__cpus__cpu);
- PROTO_READDIR_HANDLER(system__hardware__displays);
- PROTO_READDIR_HANDLER(system__hardware__displays__display);
- #if MACFUSE_PROCFS_ENABLE_TPM
- PROTO_READDIR_HANDLER(system__hardware__tpm__keyslots);
- PROTO_READDIR_HANDLER(system__hardware__tpm__pcrs);
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- PROTO_READLINK_HANDLER(einval);
- PROTO_READLINK_HANDLER(byname__name);
- static struct procfs_dispatcher_entry
- procfs_link_table[] = {
- DECL_LINK(
- "/byname/(.+)",
- 1,
- default_file,
- default_file,
- byname__name,
- byname__name
- )
- };
- static struct procfs_dispatcher_entry
- procfs_file_table[] = {
- DECL_FILE_WITHFLAGS(
- PROCFS_FLAG_ISDOTFILE,
- "/system/.*\\._.*|/\\d+/.*\\._.*",
- 0,
- default_file,
- default_file,
- default_file_finder_info,
- default_file_finder_info
- )
- DECL_FILE(
- "/system/firmware/variables",
- 0,
- default_file,
- default_file,
- default_file,
- system__firmware__variables
- )
- DECL_FILE(
- "/system/hardware/(lightsensor|motionsensor|mouse)/data",
- 1,
- default_file,
- default_file,
- default_file,
- system__hardware__xsensor
- )
- DECL_FILE(
- "/system/hardware/camera/screenshot.tiff",
- 0,
- system__hardware__camera__screenshot,
- system__hardware__camera__screenshot,
- system__hardware__camera__screenshot,
- system__hardware__camera__screenshot
- )
- DECL_FILE(
- "/system/hardware/cpus/(\\d+)/data",
- 1,
- default_file,
- default_file,
- default_file,
- system__hardware__cpus__cpu__data
- )
- DECL_FILE(
- "/system/hardware/displays/(\\d+)/info",
- 1,
- default_file,
- default_file,
- default_file,
- system__hardware__displays__display__info
- )
- DECL_FILE(
- "/system/hardware/displays/(\\d+)/screenshot.png",
- 1,
- system__hardware__displays__display__screenshot,
- system__hardware__displays__display__screenshot,
- system__hardware__displays__display__screenshot,
- system__hardware__displays__display__screenshot
- )
- #if MACFUSE_PROCFS_ENABLE_TPM
- DECL_FILE(
- "/system/hardware/tpm/hwmodel",
- 0,
- default_file,
- default_file,
- default_file,
- system__hardware__tpm__hwmodel
- )
- DECL_FILE(
- "/system/hardware/tpm/hwvendor",
- 0,
- default_file,
- default_file,
- default_file,
- system__hardware__tpm__hwvendor
- )
- DECL_FILE(
- "/system/hardware/tpm/hwversion",
- 0,
- default_file,
- default_file,
- default_file,
- system__hardware__tpm__hwversion
- )
- DECL_FILE(
- "/system/hardware/tpm/keyslots/key(\\d+)",
- 1,
- default_file,
- default_file,
- system__hardware__tpm__keyslots__slot,
- system__hardware__tpm__keyslots__slot
- )
- DECL_FILE(
- "/system/hardware/tpm/pcrs/pcr(\\d+)",
- 1,
- default_file,
- default_file,
- system__hardware__tpm__pcrs__pcr,
- system__hardware__tpm__pcrs__pcr
- )
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- DECL_FILE(
- "/(\\d+)/carbon/(name|psn)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__carbon
- )
- #if __i386__
- DECL_FILE(
- "/(\\d+)/fds",
- 1,
- default_file,
- default_file,
- default_file,
- proc__fds
- )
- #endif /* __i386__ */
- DECL_FILE(
- "/(\\d+)/(cmdline|jobc|paddr|pgid|ppid|tdev|tpgid|wchan)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__generic
- )
- DECL_FILE(
- "/(\\d+)/task/absolutetime_info/(threads_system|threads_user|total_system|total_user)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__task__absolutetime_info
- )
- DECL_FILE(
- "/(\\d+)/task/basic_info/(policy|resident_size|suspend_count|system_time|user_time|virtual_size)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__task__basic_info
- )
- DECL_FILE(
- "/(\\d+)/task/events_info/(cow_faults|csw|faults|messages_received|messages_sent|pageins|syscalls_mach|syscalls_unix)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__task__events_info
- )
- DECL_FILE(
- "/(\\d+)/task/thread_times_info/(system_time|user_time)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__task__thread_times_info
- )
- DECL_FILE(
- "/(\\d+)/task/mach_name",
- 1,
- default_file,
- default_file,
- default_file,
- proc__task__mach_name
- )
- DECL_FILE(
- "/(\\d+)/task/ports/([a-f\\d]+)/(msgcount|qlimit|seqno|sorights|task_rights)",
- 3,
- default_file,
- default_file,
- default_file,
- proc__task__ports__port
- )
- DECL_FILE(
- "/(\\d+)/task/role",
- 1,
- default_file,
- default_file,
- default_file,
- proc__task__role
- )
- DECL_FILE(
- "/(\\d+)/task/threads/([a-f\\d]+)/basic_info/(cpu_usage|flags|policy|run_state|sleep_time|suspend_count|system_time|user_time)",
- 3,
- default_file,
- default_file,
- default_file,
- proc__task__threads__thread__basic_info
- )
- DECL_FILE(
- "/(\\d+)/task/threads/([a-f\\d]+)/states/debug/(dr[0-7])",
- 3,
- default_file,
- default_file,
- default_file,
- proc__task__threads__thread__states__debug
- )
- DECL_FILE(
- "/(\\d+)/task/threads/([a-f\\d]+)/states/exception/(err|faultvaddr|trapno)",
- 3,
- default_file,
- default_file,
- default_file,
- proc__task__threads__thread__states__exception
- )
- DECL_FILE(
- "/(\\d+)/task/threads/([a-f\\d]+)/states/float/(fpu_fcw|fpu_fsw|fpu_ftw|fpu_fop|fpu_ip|fpu_cs|fpu_dp|fpu_ds|fpu_mxcsr|fpu_mxcsrmask)",
- 3,
- default_file,
- default_file,
- default_file,
- proc__task__threads__thread__states__float
- )
- DECL_FILE(
- "/(\\d+)/task/threads/([a-f\\d]+)/states/thread/(e[a-d]x|edi|esi|ebp|esp|ss|eflags|eip|[cdefg]s)",
- 3,
- default_file,
- default_file,
- default_file,
- proc__task__threads__thread__states__thread
- )
- DECL_FILE(
- "/(\\d+)/task/tokens/(audit|security)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__task__tokens
- )
- DECL_FILE(
- "/(\\d+)/task/vmmap",
- 1,
- default_file,
- default_file,
- default_file,
- proc__task__vmmap
- )
- DECL_FILE(
- "/(\\d+)/task/vmmap_r",
- 1,
- default_file,
- default_file,
- default_file,
- proc__task__vmmap_r
- )
- DECL_FILE(
- "/(\\d+)/windows/(all|onscreen)",
- 2,
- default_file,
- default_file,
- default_file,
- proc__windows__generic
- )
- DECL_FILE(
- "/(\\d+)/windows/identify",
- 1,
- proc__windows__identify,
- proc__windows__identify,
- default_file,
- zero
- )
- DECL_FILE(
- "/(\\d+)/windows/screenshots/([a-f\\d]+).png",
- 2,
- proc__windows__screenshots__window,
- proc__windows__screenshots__window,
- proc__windows__screenshots__window,
- proc__windows__screenshots__window
- )
- DECL_FILE(
- "/(\\d+)/(ucred|pcred)/(groups|rgid|ruid|svgid|svuid|uid)",
- 3,
- default_file,
- default_file,
- default_file,
- proc__xcred
- )
- };
- static struct procfs_dispatcher_entry
- procfs_directory_table[] = {
- DECL_DIRECTORY(
- "/",
- 0,
- default_directory,
- default_directory,
- default_directory,
- root,
- { NULL },
- { "byname", "system", NULL }
- )
- DECL_DIRECTORY(
- "/byname",
- 0,
- default_directory,
- default_directory,
- default_directory,
- byname,
- { NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/system",
- { NULL },
- { "firmware", "hardware", NULL },
- )
- DECL_DIRECTORY_COMPACT(
- "/system/firmware",
- { "variables", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/system/hardware",
- { NULL },
- #if MACFUSE_PROCFS_ENABLE_TPM
- {
- "camera", "cpus", "displays", "lightsensor", "motionsensor",
- "mouse", "tpm", NULL
- }
- #else
- {
- "camera", "cpus", "displays", "lightsensor", "motionsensor",
- "mouse", NULL
- }
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- )
- DECL_DIRECTORY_COMPACT(
- "/system/hardware/camera",
- { "screenshot.tiff", NULL },
- { NULL },
- )
- DECL_DIRECTORY(
- "/system/hardware/cpus",
- 0,
- default_directory,
- default_directory,
- default_directory,
- system__hardware__cpus,
- { NULL },
- { NULL },
- )
- DECL_DIRECTORY(
- "/system/hardware/cpus/(\\d+)",
- 1,
- default_directory,
- default_directory,
- default_directory,
- system__hardware__cpus__cpu,
- { "data", NULL },
- { NULL },
- )
- DECL_DIRECTORY(
- "/system/hardware/displays",
- 0,
- default_directory,
- default_directory,
- default_directory,
- system__hardware__displays,
- { NULL },
- { NULL },
- )
- DECL_DIRECTORY(
- "/system/hardware/displays/(\\d+)",
- 1,
- default_directory,
- default_directory,
- system__hardware__displays__display,
- system__hardware__displays__display,
- { "info", "screenshot.png", NULL },
- { NULL },
- )
- DECL_DIRECTORY_COMPACT(
- "/system/hardware/lightsensor",
- { "data", NULL },
- { NULL },
- )
- DECL_DIRECTORY_COMPACT(
- "/system/hardware/motionsensor",
- { "data", NULL },
- { NULL },
- )
- DECL_DIRECTORY_COMPACT(
- "/system/hardware/mouse",
- { "data", NULL },
- { NULL },
- )
- #if MACFUSE_PROCFS_ENABLE_TPM
- DECL_DIRECTORY_COMPACT(
- "/system/hardware/tpm",
- { "hwmodel", "hwvendor", "hwversion", NULL },
- { "keyslots", "pcrs" }
- )
- DECL_DIRECTORY(
- "/system/hardware/tpm/keyslots",
- 0,
- default_directory,
- default_directory,
- default_directory,
- system__hardware__tpm__keyslots,
- { NULL },
- { NULL },
- )
- DECL_DIRECTORY(
- "/system/hardware/tpm/pcrs",
- 0,
- default_directory,
- default_directory,
- default_directory,
- system__hardware__tpm__pcrs,
- { NULL },
- { NULL },
- )
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- DECL_DIRECTORY_COMPACT(
- "/\\d+",
- {
- "cmdline",
- #if __i386__
- "fds",
- #endif /* __i386__ */
- "jobc", "paddr", "pgid", "ppid", "tdev", "tpgid",
- "wchan", "windows", NULL
- },
- { "carbon", "pcred", "task", "ucred", NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/carbon",
- { "name", "psn", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/pcred",
- { "rgid", "ruid", "svgid", "svgid", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task",
- { "mach_name", "role", "vmmap", "vmmap_r", NULL },
- {
- "absolutetime_info", "basic_info", "events_info", "ports",
- "thread_times_info", "threads", "tokens", NULL
- }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/absolutetime_info",
- {
- "threads_system", "threads_user", "total_system",
- "total_user", NULL
- },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/basic_info",
- {
- "policy", "resident_size", "suspend_count", "system_time",
- "user_time", "virtual_size", NULL
- },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/events_info",
- {
- "cow_faults", "csw", "faults", "messages_received",
- "messages_sent", "pageins", "syscalls_mach", "syscalls_unix", NULL
- },
- { NULL }
- )
- DECL_DIRECTORY(
- "/(\\d+)/task/ports",
- 1,
- default_directory,
- default_directory,
- default_directory,
- proc__task__ports,
- { NULL },
- { NULL }
- )
- DECL_DIRECTORY(
- "/(\\d+)/task/ports/([a-f\\d]+)",
- 2,
- default_directory,
- default_directory,
- proc__task__ports__port,
- default,
- { "msgcount", "qlimit", "seqno", "sorights", "task_rights", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/thread_times_info",
- { "system_time", "user_time", NULL },
- { NULL }
- )
- DECL_DIRECTORY(
- "/(\\d+)/task/threads",
- 1,
- default_directory,
- default_directory,
- default_directory,
- proc__task__threads,
- { NULL },
- { NULL }
- )
- DECL_DIRECTORY(
- "/(\\d+)/task/threads/([a-f\\d])+",
- 2,
- default_directory,
- default_directory,
- proc__task__threads__thread,
- default,
- { NULL },
- { "basic_info", "states", NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/threads/[a-f\\d]+/basic_info",
- {
- "cpu_usage", "flags", "policy", "run_state", "sleep_time",
- "suspend_count", "system_time", "user_time", NULL
- },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/threads/[a-f\\d]+/states",
- { "debug", "exception", "float", "thread", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/threads/[a-f\\d]+/states/debug",
- { "dr0", "dr1", "dr2", "dr3", "dr4", "dr5", "dr6", "dr7", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/threads/[a-f\\d]+/states/exception",
- { "err", "faultvaddr", "trapno", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/threads/[a-f\\d]+/states/float",
- {
- "fpu_cs", "fpu_dp", "fpu_ds", "fpu_fcw", "fpu_fop", "fpu_fsw",
- "fpu_ftw", "fpu_ip", "fpu_mxcsr", "fpu_mxcsrmask", NULL
- },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/threads/[a-f\\d]+/states/thread",
- {
- "eax", "ebx", "ecx", "edx", "edi", "esi", "ebp", "esp", "ss",
- "eflags", "eip", "cs", "ds", "es", "fs", "gs", NULL
- },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/task/tokens",
- { "audit", "security", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/ucred",
- { "groups", "uid", NULL },
- { NULL }
- )
- DECL_DIRECTORY_COMPACT(
- "/\\d+/windows",
- { "all", "onscreen", "identify", NULL },
- { "screenshots", NULL }
- )
- DECL_DIRECTORY(
- "/(\\d+)/windows/screenshots",
- 1,
- default_directory,
- default_directory,
- default_directory,
- proc__windows__screenshots,
- { NULL },
- { NULL },
- )
- };
- // BEGIN: OPEN/OPENDIR
- //
- // int
- // procfs_open/opendir_<handler>(procfs_dispatcher_entry_t e,
- // const char *argv[],
- // const char *path,
- // struct fuse_file_info *fi)
- OPEN_HANDLER(default_file)
- {
- return 0;
- }
- OPEN_HANDLER(eisdir)
- {
- return -EISDIR;
- }
- OPEN_HANDLER(proc__windows__identify)
- {
- if (fi->fh != 0) { /* XXX: need locking */
- return 0;
- } else {
- fi->fh = 1;
- }
- char *whandler = NULL;
- if ((whandler = getenv("MACFUSE_PROCFS_WHANDLER")) == NULL) {
- goto bail;
- }
- int npid = vfork();
- if (npid == 0) {
- execl(whandler, whandler, argv[0], NULL);
- return 0;
- }
- bail:
- return 0;
- }
- OPEN_HANDLER(proc__windows__screenshots__window)
- {
- if (fi->fh != 0) { /* XXX: need locking */
- return 0;
- }
- pid_t pid = strtol(argv[0], NULL, 10);
- CGWindowID target = strtol(argv[1], NULL, 16);
- ProcessSerialNumber psn;
- OSStatus status = GetProcessForPID(pid, &psn);
- if (status != noErr) {
- return -ENOENT;
- }
- CGSConnectionID conn;
- CGError err = CGSGetConnectionIDForPSN(0, &psn, &conn);
- if (err != kCGErrorSuccess) {
- return -ENOENT;
- }
- #define MAX_WINDOWS 256
- CGSWindowID windowIDs[MAX_WINDOWS];
- int windowCount = 0;
- int i = 0;
- err = CGSGetWindowList(_CGSDefaultConnection(), conn, MAX_WINDOWS,
- windowIDs, &windowCount);
- for (i = 0; i < windowCount; i++) {
- if (windowIDs[i] == target) {
- goto doread;
- }
- }
- return -ENOENT;
- doread:
- CFMutableDataRef window_png = (CFMutableDataRef)0;
- int ret = PROCFS_GetPNGForWindowAtIndex(target, &window_png);
- if (ret == -1) {
- return -EIO;
- }
- struct ProcfsWindowData *pwd =
- (struct ProcfsWindowData *)malloc(sizeof(struct ProcfsWindowData));
- if (!pwd) {
- CFRelease(window_png);
- return -ENOMEM;
- }
- pwd->window_png = window_png;
- pwd->max_len = PROCFS_GetPNGSizeForWindowAtIndex(target);
- pwd->len = (size_t)CFDataGetLength(window_png);
- fi->fh = (uint64_t)pwd;
- return 0;
- }
- OPEN_HANDLER(system__hardware__camera__screenshot)
- {
- pthread_mutex_lock(&camera_lock);
- if (camera_busy) {
- pthread_mutex_unlock(&camera_lock);
- return -EBUSY;
- } else {
- camera_busy = 1;
- pthread_mutex_unlock(&camera_lock);
- }
- int ret = PROCFS_GetTIFFFromCamera(&camera_tiff);
- return ret;
- }
- OPEN_HANDLER(system__hardware__displays__display__screenshot)
- {
- pthread_mutex_lock(&display_lock);
- if (display_busy) {
- pthread_mutex_unlock(&display_lock);
- return -EBUSY;
- } else {
- display_busy = 1;
- pthread_mutex_unlock(&display_lock);
- }
- unsigned long index = strtol(argv[0], NULL, 10);
- CGDisplayCount display_count = PROCFS_GetDisplayCount();
- if (index >= display_count) {
- return -ENOENT;
- }
- if (display_png) {
- CFRelease(display_png);
- display_png = (CFMutableDataRef)0;
- }
- int ret = PROCFS_GetPNGForDisplayAtIndex(index, &display_png);
- if (ret) {
- if (display_png) {
- CFRelease(display_png);
- display_png = (CFMutableDataRef)0;
- }
- return -EIO;
- }
- return 0;
- }
- OPENDIR_HANDLER(default_directory)
- {
- return 0;
- }
- OPENDIR_HANDLER(enotdir)
- {
- return -ENOTDIR;
- }
- // END: OPEN/OPENDIR
- // BEGIN: RELEASE/RELEASEDIR
- //
- // int
- // procfs_release/releasedir_<handler>(procfs_dispatcher_entry_t e,
- // const char *argv[],
- // const char *path,
- // struct fuse_file_info *fi)
- RELEASE_HANDLER(default_file)
- {
- return 0;
- }
- RELEASE_HANDLER(eisdir)
- {
- return -EISDIR;
- }
- RELEASE_HANDLER(proc__windows__identify)
- {
- fi->fh = 0;
- return 0;
- }
- RELEASE_HANDLER(proc__windows__screenshots__window)
- {
- if (fi->fh) {
- struct ProcfsWindowData *pwd = (struct ProcfsWindowData *)(fi->fh);
- CFRelease((CFMutableDataRef)(pwd->window_png));
- free((void *)pwd);
- fi->fh = 0;
- }
- return 0;
- }
- RELEASE_HANDLER(system__hardware__camera__screenshot)
- {
- pthread_mutex_lock(&camera_lock);
- camera_busy = 0;
- pthread_mutex_unlock(&camera_lock);
- return 0;
- }
- RELEASE_HANDLER(system__hardware__displays__display__screenshot)
- {
- pthread_mutex_lock(&display_lock);
- display_busy = 0;
- if (display_png) {
- CFRelease(display_png);
- display_png = (CFMutableDataRef)0;
- }
- pthread_mutex_unlock(&display_lock);
- return 0;
- }
- RELEASEDIR_HANDLER(default_directory)
- {
- return 0;
- }
- RELEASEDIR_HANDLER(enotdir)
- {
- return -ENOTDIR;
- }
- // END: RELEASE/RELEASEDIR
- // BEGIN: GETATTR
- //
- // int
- // procfs_getattr_<handler>(procfs_dispatcher_entry_t e,
- // const char *argv[],
- // struct stat *stbuf)
-
- GETATTR_HANDLER(default_file)
- {
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_size = 0;
- if (procfs_ui) {
- stbuf->st_size = PROCFS_DEFAULT_FILE_SIZE;
- }
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- return 0;
- }
- GETATTR_HANDLER(default_file_finder_info)
- {
- if (!procfs_ui) {
- return -ENOENT;
- }
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_size = 82;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- return 0;
- }
-
- GETATTR_HANDLER(default_directory)
- {
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFDIR | 0555;
- stbuf->st_nlink = 1;
- stbuf->st_size = 0;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
-
- return 0;
- }
- GETATTR_HANDLER(default_link)
- {
- stbuf->st_mode = S_IFLNK | 0755;
- stbuf->st_nlink = 1;
- stbuf->st_size = 0;
-
- return 0;
- }
- GETATTR_HANDLER(byname__name)
- {
- const char *target_Pname = argv[0];
- struct stat the_stat;
- char the_name[MAXNAMLEN + 1];
- Boolean strstatus = false;
- ProcessSerialNumber psn;
- OSErr osErr = noErr;
- OSStatus status;
- CFStringRef Pname;
- pid_t Pid;
- psn.highLongOfPSN = kNoProcess;
- psn.lowLongOfPSN = kNoProcess;
- while ((osErr = GetNextProcess(&psn)) != procNotFound) {
- status = GetProcessPID(&psn, &Pid);
- if (status != noErr) {
- continue;
- }
- Pname = (CFStringRef)0;
- status = CopyProcessName(&psn, &Pname);
- if (status != noErr) {
- if (Pname) {
- CFRelease(Pname);
- Pname = (CFStringRef)0;
- }
- continue;
- }
- strstatus = CFStringGetCString(Pname, the_name, MAXNAMLEN,
- kCFStringEncodingASCII);
- if (strstatus != true) {
- Pid = 0;
- } else if (strcmp(target_Pname, the_name) != 0) {
- Pid = 0;
- }
- CFRelease(Pname);
- Pname = (CFStringRef)0;
- if (Pid) {
- break;
- }
- }
- if (!Pid) {
- return -ENOENT;
- }
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFLNK | 0755;
- stbuf->st_nlink = 1;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- int len = snprintf(the_name, MAXNAMLEN, "../%u", Pid);
- the_stat.st_size = len;
- return 0;
- }
- GETATTR_HANDLER(system__hardware__displays__display)
- {
- unsigned long index = strtol(argv[0], NULL, 10);
- CGDisplayCount display_count = PROCFS_GetDisplayCount();
- if (index >= display_count) {
- return -ENOENT;
- }
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFDIR | 0555;
- stbuf->st_nlink = 1;
- stbuf->st_size = 0;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
-
- return 0;
- }
- GETATTR_HANDLER(system__hardware__camera__screenshot)
- {
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- stbuf->st_size = PROCFS_GetTIFFSizeFromCamera();
- return 0;
- }
- GETATTR_HANDLER(system__hardware__displays__display__screenshot)
- {
- unsigned long index = strtol(argv[0], NULL, 10);
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- stbuf->st_size = PROCFS_GetPNGSizeForDisplayAtIndex(index);
- return 0;
- }
- #if MACFUSE_PROCFS_ENABLE_TPM
- GETATTR_HANDLER(system__hardware__tpm__keyslots__slot)
- {
- uint32_t keys[256];
- unsigned long slotno = strtol(argv[0], NULL, 10);
- uint16_t slots_used = 0;
- uint32_t slots_free = 0;
- uint32_t slots_total = 0;
- if (TPM_GetCapability_Slots(&slots_free)) {
- return -ENOENT;
- }
- if (TPM_GetCapability_Key_Handle(&slots_used, keys)) {
- return -ENOENT;
- }
- slots_total = slots_used + slots_free;
- if (slotno >= slots_total) {
- return -ENOENT;
- }
- time_t current_time = time(NULL);
- stbuf->st_nlink = 1;
- stbuf->st_size = 9;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- if (slotno >= slots_used) {
- stbuf->st_mode = S_IFREG | 0000;
- } else {
- stbuf->st_mode = S_IFREG | 0444;
- }
- return 0;
- }
- GETATTR_HANDLER(system__hardware__tpm__pcrs__pcr)
- {
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_size = 60;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- return 0;
- }
- #endif /* MACFUSE_PROCFS_ENABLE_TPM */
- GETATTR_HANDLER(proc__task__ports__port)
- {
- kern_return_t kr;
- task_t the_task = MACH_PORT_NULL;
- pid_t pid = strtol(argv[0], NULL, 10);
- kr = task_for_pid(mach_task_self(), pid, &the_task);
- if (kr != KERN_SUCCESS) {
- return -ENOENT;
- }
- DECL_PORT_LIST();
- INIT_PORT_LIST(the_task);
- int found = 0;
- unsigned int i;
- unsigned int the_port_name = strtoul(argv[1], NULL, 16);
- for (i = 0; i < name_count; i++) {
- if (the_port_name == name_list[i]) {
- found = 1;
- break;
- }
- }
- FINI_PORT_LIST();
- if (the_task != MACH_PORT_NULL) {
- mach_port_deallocate(mach_task_self(), the_task);
- }
- if (!found) {
- return -ENOENT;
- }
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFDIR | 0555;
- stbuf->st_nlink = 1;
- stbuf->st_size = 0;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
-
- return 0;
- }
- GETATTR_HANDLER(proc__task__threads__thread)
- {
- kern_return_t kr;
- task_t the_task = MACH_PORT_NULL;
- pid_t pid = strtol(argv[0], NULL, 10);
- kr = task_for_pid(mach_task_self(), pid, &the_task);
- if (kr != KERN_SUCCESS) {
- return -ENOENT;
- }
- DECL_THREAD_LIST();
- INIT_THREAD_LIST(the_task);
- FINI_THREAD_LIST();
- if (the_task != MACH_PORT_NULL) {
- mach_port_deallocate(mach_task_self(), the_task);
- }
- unsigned int the_thread_name = strtoul(argv[1], NULL, 16);
- if (the_thread_name >= thread_count) {
- return -ENOENT;
- }
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFDIR | 0555;
- stbuf->st_nlink = 1;
- stbuf->st_size = 0;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
-
- return 0;
- }
- GETATTR_HANDLER(proc__windows__screenshots__window)
- {
- pid_t pid = strtol(argv[0], NULL, 10);
- CGWindowID target = strtol(argv[1], NULL, 16);
- ProcessSerialNumber psn;
- OSStatus status = GetProcessForPID(pid, &psn);
- if (status != noErr) {
- return 0; /* technically not an error in this case */
- }
- CGSConnectionID conn;
- CGError err = CGSGetConnectionIDForPSN(0, &psn, &conn);
- if (err != kCGErrorSuccess) {
- return 0; /* just be nice */
- }
- #define MAX_WINDOWS 256
- CGSWindowID windowIDs[MAX_WINDOWS];
- int windowCount = 0;
- int i = 0;
- err = CGSGetWindowList(_CGSDefaultConnection(), conn, MAX_WINDOWS,
- windowIDs, &windowCount);
- for (i = 0; i < windowCount; i++) {
- if (windowIDs[i] == target) {
- time_t current_time = time(NULL);
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_atime = stbuf->st_ctime = stbuf->st_mtime = current_time;
- stbuf->st_size = PROCFS_GetPNGSizeForWindowAtIndex(windowIDs[i]);
- return 0;
- }
- }
- return -ENOENT;
- }
- // END: GETATTR
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <libgen.h>
- #include <sys/sysctl.h>
- int
- procinfo(pid_t pid, struct kinfo_proc *kp)
- {
- int mib[4];
- size_t bufsize = 0, orig_bufsize = 0;
- struct kinfo_proc *kprocbuf;
- int retry_count = 0;
- int local_error;
- mib[0] = CTL_KERN;
- mib[1] = KERN_PROC;
- mib[2] = KERN_PROC_PID;
- mib[3] = pid;
- kprocbuf = kp;
- orig_bufsize = bufsize = sizeof(struct kinfo_proc);
- for (retry_count = 0; ; retry_count++) {
- local_error = 0;
- bufsize = orig_bufsize;
- if ((local_error = sysctl(mib, 4, kp, &bufsize, NULL, 0)) < 0) {
- if (retry_count < 1000) {
- sleep(1);
- continue;
- }
- return local_error;
- } else if (local_error == 0) {
- br…