【问题标题】:Interrupt (SIGINT) perl script from perl script running under apache来自在 apache 下运行的 perl 脚本的中断(SIGINT)perl 脚本
【发布时间】:2014-06-28 11:37:29
【问题描述】:

我有一个以 root 身份运行的 perl 脚本,它监视串行设备并向其发送命令。在 apache 下,我有另一个 perl 脚本,它显示控制“根”脚本的 gui。

我试图通过 sigint 和 sigusr1 中断来自 gui perl 脚本的 root perl 脚本,但不允许进行操作,可能因为一个是 root,另一个不是。

我基本上希望 gui 能够告诉控制根脚本将一些命令传递给串行设备。

如果我以 root 身份从 cmd 行运行 gui 脚本,它可以成功地向 root 脚本发出信号

我不知道从哪里开始,有什么建议可以在不以 root 身份运行时中断 root 脚本的方法吗?如图所示调用单独的“信号”脚本:

     @cmds = ("perl /var/www/signal.cgi", "$perlSerialPID", "test");
     if (0 == system(@cmds)) {
         # code if the command runs ok
         print "system call ok: $?";
     } else {
        # code if the command fails
        print "not ok $?";
     }
     # try backticks
     $output = `perl /var/www/signal.cgi $perlSerialPID test`;
     print "<br>:::$output";

信号.cgi:

#!/usr/bin/perl

$pid = $ARGV[0];
$type = $ARGV[1];

if($type eq "test"){
 if(kill 0, $pid) {
   print "perl-serial $pid running!";
 }else{
   print "perl-serial $pid gone $!";
 }
}elsif($type eq "USR1"){
  if(kill USR1, $pid) {
   print "perl-serial interrupted";
  }else{
   print "perl-serial interrupt failed $!";
  }
}else{
  print "FAILED";
} 

【问题讨论】:

  • 制作一个发送信号的脚本。使用 sudo 以 root 身份调用该脚本。
  • 如上所述gui脚本是在apache下运行的,那么你如何使用sudo按照你的建议调用它?

标签: apache perl signals interrupt


【解决方案1】:
  1. named pipes 用于 IPC。

    缺点是您必须改造您的主脚本以从命名管道读取作为事件驱动的过程。

  2. 与上述概念相似(缺点相同),但使用sockets 进行通信。

  3. 创建一个单独的“发送信号”脚本,通过 UI 中的系统调用调用该脚本,并将该新脚本设为 SUID 并归 root 所有 - 在这种情况下,它将以 root 的权限执行。

    缺点:这是成熟的安全滥用,所以要非常小心地加强这一点。

【讨论】:

  • 我将尝试使用不安全的包装脚本,因为我真的想中断而不是定期轮询或检查文件/管道
  • 当您说单独的脚本时,您是指 perl 脚本吗,因为我已经这样做了,但仍然不允许操作。上面显示的代码
  • 我最终将 www-data 添加到 sudo 并通过 'sudo perl /path/to/signal.cgi' 调用信号脚本,并通过 UNIX 套接字传递任何命令。中断告诉脚本读取并操作通过套接字发送的任何 cmd,因为在读取管道时我不能阻塞脚本。
猜你喜欢
  • 2012-02-18
  • 2010-09-26
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
相关资源
最近更新 更多