【发布时间】:2011-12-31 14:01:02
【问题描述】:
我的应用程序的目标是通过以太网链路控制我的嵌入式目标上的一些 LED。我的嵌入式主板支持 lighttpd 网络服务器。从这个网络服务器,我可以运行 python 脚本,读取到我板上的设备没有问题。当我尝试写入这些设备时,问题就来了。 lighttpd 服务器作为“www”组运行。我的董事会的根用户没有密码。我试图强制 lighttpd 服务器以 root 身份运行会导致 lighttpd 根本无法启动。所以我做了一个 C 程序,作为一个子进程调用,通过 sudo 从 python 脚本提升到 root。
我的控制 LED 的 C 程序:
int main(int argc, char* args[]){
string python_message = "";
bool quit = false;
while (!quit)
{
cin >> python_message;
if (python_message == "quit"){
quit = true;
}else if (python_message == "1"){
ledn(1,"1");
}else if (python_message == "2"){
ledn(1,"0");
}else {
cout << "Huh?" << endl;
}
}
return 0;
}
cgi-bin中的python脚本
import sys
import time
print "Blinking User LED Program"
import subprocess
proc = subprocess.Popen(["sudo","/usr/bin/slave"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
print "1"
proc.stdin.write("1\n")
time.sleep(.5)
print "0"
proc.stdin.write("0\n")
time.sleep(.5)
如果我注释 proc.stdin 和 proc.stdout 行,我的程序将运行并给我所有的打印语句输出。 当这些行出现时,我会收到 500 服务器错误。
【问题讨论】:
-
您是否添加了 cgi 的标头?
print("content-type: text/html; charset=utf-8\n\n");在 python CGI 中?此外,您可能希望在 python - cgi 脚本库中import cgi。允许调试等。您的 C 程序是否也作为 CGI 运行(您还必须将标头放在那里。 -
更改设备的权限/所有权以便 www 可以进行写入不是更容易吗?只是说:)
-
为什么不做C程序setuid?
-
无论如何,看看 lighttpd 错误日志。如果您收到 500 错误,则可能是 Python 脚本因某些错误而死,并且回溯应该在日志中。
-
var/log 中没有 lighttpd 日志(此文件夹为空)。在 lighttp.conf 中,设置为:server.errorlog= |/var/log/lighttpd/error.log"。@RicardoCárdenes 以及我如何/在哪里可以更改权限以便 www 可以进行写作?