我正在尝试自己编写 runc exec,但是在实现 nsenter 模块时遇到了问题。
这是示例代码:
package main
import "fmt"
/*
#define JUMP_PARENT 0x00
#define JUMP_CHILD 0xA0
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <setjmp.h>
char child_stack[4096] __attribute__ ((aligned(16)));
int child_func(void *arg) {
jmp_buf* env = (jmp_buf*)arg;
longjmp(*env, JUMP_CHILD);
}
__attribute__((constructor)) void init(void) {
printf("init...\n");
jmp_buf env;
switch(setjmp(env)) {
case JUMP_PARENT:
printf("JUMP_PARENT\n");
int child_pid = clone(child_func, child_stack, CLONE_PARENT, env);
printf("CHILD_PID: %d\n", child_pid);
exit(0);
case JUMP_CHILD:
printf("JUMP_CHILD\n");
return;
}
}
*/
import "C"
func main() {
fmt.Println("main...")
}
这是 CentOS7 中的输出:
[root@localhost cgo-practive]# go build .
[root@localhost cgo-practive]# ls
cgo-practive main.go
[root@localhost cgo-practive]# ./cgo-practive
init...
JUMP_PARENT
CHILD_PID: 14348
[root@localhost cgo-practive]# JUMP_CHILD
// program blocked here
然后我用gdb找出了被屏蔽的原因:
(gdb) list
28 exit(0);
29 case JUMP_CHILD:
30 printf("JUMP_CHILD\n");
31 return;
32 }
33 }
34 */
35 import "C"
36
37 func main() {
(gdb) info stack
#0 0x00007efd6f9684ed in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x00007efd6f966170 in pthread_cond_broadcast@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#2 0x00000000004862e6 in x_cgo_notify_runtime_init_done (dummy=<optimized out>) at gcc_libinit.c:69
#3 0x0000000000451070 in runtime.asmcgocall () at /usr/local/go/src/runtime/asm_amd64.s:637
#4 0x00007ffdec4b5c30 in ?? ()
#5 0x000000000044efd1 in runtime.malg.func1 () at /usr/local/go/src/runtime/proc.go:3289
#6 0x000000000044f886 in runtime.systemstack () at /usr/local/go/src/runtime/asm_amd64.s:351
#7 0x000000000042c5b0 in ?? () at /usr/local/go/src/runtime/proc.go:1146
#8 0x000000000044f719 in runtime.rt0_go () at /usr/local/go/src/runtime/asm_amd64.s:201
#9 0x0000000000000000 in ?? ()
程序好像是在x_cgo_notify_runtime_init_done中被阻塞了,但是我不擅长cgo,想不通为什么会被阻塞。
谢谢。
最佳答案
作为man page说
Stacks grow downward on all processors that run Linux (except the HP PA processors), so child_stack usually points to the topmost address of the memory space set up for the child stack.
所以你应该改用下面的代码
int child_pid = clone(child_func, &child_stack[4096], CLONE_PARENT, &env);
关于在返回 Go Runtime 之前,Cgo 在 x_cgo_notify_runtime_init_done 中被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55194065/
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
是否可以在所有delayed_job任务之前运行一个方法?基本上,我们试图确保每个运行delayed_job的服务器都有我们代码的最新实例,所以我们想运行一个方法来在每个作业运行之前检查它。(我们已经有了“check”方法并在别处使用它。问题只是关于如何从delayed_job中调用它。) 最佳答案 现在有一种官方方法可以通过插件来做到这一点。这篇博文通过示例清楚地描述了如何执行此操作http://www.salsify.com/blog/delayed-jobs-callbacks-and-hooks-in-rails(本文中描述
我无法从for循环中获取变量。似乎i(var)是稍后计算的,而不是我完全需要的类定义。ree-1.8.7-2010.02>classPatree-1.8.7-2010.02?>foriin39..47ree-1.8.7-2010.02?>define_method("a#{i}".to_sym)doree-1.8.7-2010.02>putsiree-1.8.7-2010.02?>endree-1.8.7-2010.02?>endree-1.8.7-2010.02?>end#=>39..47ree-1.8.7-2010.02>p=Pat.new#=>#ree-1.8.7-2010.02
多年来,我在各种网站上遇到过各种问题,用户在字符串和文本字段的开头/结尾放置空格。有时这些会导致格式/布局问题,有时会导致搜索问题(即搜索顺序看起来不对,但实际上并非如此),有时它们实际上会使应用程序崩溃。我认为这会很有用,而不是像我过去所做的那样放入一堆before_save回调,向ActiveRecord添加一些功能以在保存之前自动调用任何字符串/文本字段上的.strip,除非我告诉它不是,例如do_not_strip:field_x,:field_y或类定义顶部的类似内容。在我去弄清楚如何做到这一点之前,有没有人看到更好的解决方案?明确一点,我已经知道我可以做到这一点:befor
我有一个带有利润字段的用户模型。利润字段是DECIMAL(11,0)类型。我在表单上有一个屏蔽输入,允许用户输入1,000美元之类的内容。我想格式化该值并从中删除除数字以外的所有内容,这样我将保存1000。这是我到目前为止所拥有的:classUser但它一直在数据库中保存0。看起来它在我的格式化函数之前将其转换为十进制。 最佳答案 试试这个:defprofit=(new_profit)self[:profit]=new_profit.gsub(/[^0-9]/,'')end 关于ruby
假设我有一个名为Product的模型,其中有一个名为brand的字段。假设brand的值以this_is_a_brand格式存储。我可以在模型(或其他任何地方)中定义一个方法,允许我在调用brand之前修改它的值吗?例如,如果我调用@product.brand,我想得到ThisisaBrand,而不是this_is_a_brand。 最佳答案 我建议使用方括号语法([]和[]=)而不是read_attribute和write_attribute。方括号语法更短并且designedtowraptheprotectedread/writ
我有一些Rspec测试有时会因错误而失败:自动加载常量时检测到循环依赖。这些测试是多线程的(rufus-scheduler测试),这显然是自动加载代码的已知问题(http://route.github.io/2013/11/13/rails-autoloading.html)。如果我在config/environments/test.rb中设置config.eager_load=true,我可以使测试始终如一地工作。但是,我担心当它变大时,这会真正减慢我的测试套件的其余部分。有什么方法可以仅为我的多线程测试设置这个预加载选项吗?rails4.1.4 最佳答案
我有一个这样的描述block:describe"Documents"dosubject{page}let(:course){FactoryGirl.create(:course)}describe"new"dobeforedovisitnew_course_document_path(course)fill_in"Name",with:"TestDocument"attach_file"Originaldocument","#{Rails.root}/spec/fixtures/03_GUI_concurrency.pdf"endit{shouldhave_selector('titl