为什么sysctl在Mac OS X上生成E_INVAL? [英] Why is sysctl producing E_INVAL on Mac OS X?

查看:155
本文介绍了为什么sysctl在Mac OS X上生成E_INVAL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是使用sysctl获取PID为50的特定进程的argv的C/Obj-C代码的简短摘要(省略了错误/空检查).

Below is a pared-down (error/null checks omitted) snippet of C/Obj-C code that uses sysctl to get the argv of a particular process with PID 50.

...
int getProcessArgs[3] = { CTL_KERN, KERN_PROCARGS, 50 };
sysctl(getProcessArgs, 3, NULL, &length, NULL, 0);
char* processArgs = malloc(length * sizeof(char));
sysctl(getProcessArgs, 3, processArgs, &length, NULL, 0);
...

对sysctl的第一次调用(确定argv字符串数组的大小)成功.返回的长度是〜1600,比我预期的要大,但是我想这并不合理. Malloc成功.对sysctl的第二次调用返回-1,将errno设置为22,E_INVAL.

The first call to sysctl (to determine the size of the argv string array) succeeds. The returned length is ~1600, larger than I would expect, but I suppose not unreasonable. Malloc succeeds. The second call to sysctl returns -1, setting errno to 22, E_INVAL.

我查看了其他代码,包括来自的代码这个问题,但看不到我的问题.我想念什么?

I've looked at other code, including that from this question, but can't see the problem with mine. What am I missing?

推荐答案

我尝试将您的代码包装到程序中,并且在查询我自己的进程之一时,它可以正常工作并打印出另一个进程的argv等. ,即与调用sysctl()的进程具有相同uid的人.

I tried wrapping your code up into a program, and it works fine and prints out the other process's argv etc when inquiring about one of my own processes, i.e., one with the same uid as the process invoking sysctl().

比我预期的要大"的方面是因为返回了进程的环境变量以及命令行参数. (尚不清楚所有这些信息的格式是什么.)

The "larger than I would expect" aspect is because the process's environment variables are returned as well as the command line arguments. (It's not obvious what the format of all this information is.)

查询不同用户的进程时,我从第二次看到的sysctl中得到了相同的EINVAL.我想这被认为是对其他人的流程的不合理的好奇心,但是您会认为第一个sysctl也会失败.

When inquiring about a different user's process, I get the same EINVAL from the second sysctl that you've been seeing. I guess this is considered unreasonable curiosity about other people's processes, but you'd think the first sysctl would fail too.

(查询不存在的pid时,第一个sysctl失败,并显示EINVAL.)

(When inquiring about a non-existent pid, the first sysctl fails with EINVAL.)

这一切似乎文献记载不足:在Leopard上,KERN_PROCARGS甚至没有出现在sysctl手册页中.

This all seems to be massively underdocumented: on Leopard, KERN_PROCARGS doesn't even appear in the sysctl man page.

这篇关于为什么sysctl在Mac OS X上生成E_INVAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆