如果以 32 位编译,Ubuntu 上的 getpwnam 的行为会有所不同
我正在几个 Ubuntu 64 位服务器上设置 Zabbix 代理(用 C 编写)。我通常以 32 位编译所有内容,除非我特别需要 64 位(例如数据库服务器)。相关服务器将托管虚拟服务器并具有 8GB RAM,因此我将它们保留为 64 位。
如果 Zabbix 代理以 root 身份启动,它会尝试下拉到 zabbix 用户的权限级别,并使用 C getpwnam() 函数进行查找。
在纯 32 位系统上,此函数有效。但是,在 64 位系统上以 32 位编译时,当用户确实存在时,getpwnam() 将返回 NULL。如果在 64 位系统上以 64 位编译,它可以正常工作。
我模拟了一个小应用程序来证明这一点,但我希望这更多的是我的设置,而不是 GCC 或 STL 中的错误 - zabbix 用户位于 LDAP 目录中。
首先,这是该程序的输出:
root@sydney:/tmp# getent passwd|grep zabbix
zabbix:x:1500:1500:Zabbix Service:/home/zabbix:/bin/bash
root@sydney:/tmp# gcc main.c
root@sydney:/tmp# ./a.out
User zabbix exists with UID 1500.
root@sydney:/tmp# gcc -m32 main.c
root@sydney:/tmp# ./a.out
User zabbix does not exist.
这是我的小程序的代码,我使用 Zabbix 代理源代码对其进行了修改。
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
int main(int num_args, char** args)
{
struct passwd *pwd;
char user[7] = "zabbix";
pwd = getpwnam(user);
if (NULL == pwd)
{
fprintf(stdout, "User %s does not exist.\r\n", user);
return 1;
}
else
{
fprintf(stdout, "User %s exists with UID %d.\r\n", user, pwd->pw_uid);
}
return 0;
}
如果这最终证明是我的设置的问题,我很乐意在 serverfault.com 上询问,但我认为由于它以 getpwnam() 函数为中心,所以它更多地与编程相关。我也用谷歌搜索过,但没有发现任何有用的东西,这就是为什么我倾向于认为这是我的设置。
I'm setting up a ZABBIX agent (written in C) on a couple of Ubuntu 64-bit servers. I usually compile everything in 32-bit unless I specifically need 64-bit (such as database servers.) The servers in question will be hosting virtual servers and have 8GB RAM, hence why I've kept them 64-bit.
If the ZABBIX agent starts as root, it tries to drop-down to the permission level of the zabbix user, which it looks up using the C getpwnam() function.
On a pure 32-bit system, this function behaves. However, when compiled in 32-bit on a 64-bit system, getpwnam() returns NULL when a user does exist. If compiled in 64-bit on a 64-bit system it works fine.
I've mocked up a little application that proves this, but I'm hopeful it's more my setup than a bug in GCC or the STL - the zabbix user is in an LDAP directory.
Firstly here's the output of this program:
root@sydney:/tmp# getent passwd|grep zabbix
zabbix:x:1500:1500:Zabbix Service:/home/zabbix:/bin/bash
root@sydney:/tmp# gcc main.c
root@sydney:/tmp# ./a.out
User zabbix exists with UID 1500.
root@sydney:/tmp# gcc -m32 main.c
root@sydney:/tmp# ./a.out
User zabbix does not exist.
Here's the code for my little program which I've adapted using the ZABBIX agent source code.
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
int main(int num_args, char** args)
{
struct passwd *pwd;
char user[7] = "zabbix";
pwd = getpwnam(user);
if (NULL == pwd)
{
fprintf(stdout, "User %s does not exist.\r\n", user);
return 1;
}
else
{
fprintf(stdout, "User %s exists with UID %d.\r\n", user, pwd->pw_uid);
}
return 0;
}
If this turns out to be an issue with my setup I'll gladly ask on serverfault.com, but I thought as it centres around the getpwnam() function it was more programming-related. I've also Google'd but not found anything useful which is why I'm inclined to think it's my setup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了支持非传统名称查找(即 LDAP 而不仅仅是
/etc/{passwd,shadow}
),libc 使用 名称服务开关。有些方法未内置到 libc 中,必须单独加载:例如/lib/libnss_ldap.so.2
。这实际上在静态链接时相当有问题 – 你没有这样做,是吗? ?显然同一个模块不能同时用于 64 位和 32 位。 Ubuntu Karmic 在包
ia32-libs
中提供/lib32/libnss_ldap.so.2
– 是否已安装?In order to support non-traditional name lookups (i.e. LDAP instead of just
/etc/{passwd,shadow}
), libc uses the Name Service Switch. Some methods are not built into libc and must be loaded separately:/lib/libnss_ldap.so.2
, for example. This is actually quite problematic when statically linking – you aren't doing that, are you?Obviously the same module cannot be used for both 64-bit and 32-bit. Ubuntu Karmic ships
/lib32/libnss_ldap.so.2
in packageia32-libs
– is this installed?必须安装 multilib 和 ia32 库。
类似的答案在这里:gcc无法在i686上找到bits/predefs.h
Both multilib, and ia32 libs must be installed.
Similar answer here: gcc cannot find bits/predefs.h on i686