I am trying to get access to inode number of pid namespace which can be seen using ls -Li /proc/<pid>/ns/mnt or /proc/<pid>/ns/pid.
I am a newbie to kernel code, want to know in kernel, from current task structure how can I get this value? Some code would really help.
The current task structure can be looked into for pid_namespace struct, which has the this field proc_inum:
task_active_pid_ns(current)->proc_inum;
EDIT: From kernel version 3.19 onwards, the pid_namespace struct has received an update that includes the addition of the ns_common struct that is defined in the header file: /include/linux/ns_common.h.
The ns_common struct is defined as follows:
struct ns_common {
atomic_long_t stashed;
const struct proc_ns_operations *ops;
unsigned int inum;
};
The new pid_namespace struct is defined to include ns_common struct and does not contain the proc_inum integer. Instead, the inode number can be accessed using the ns_common struct.
So, the inode number can be obtained (from kernel v3.19 onwards) with the following line:
task_active_pid_ns(current)->ns_common->inum
Source: https://elixir.bootlin.com/linux/v3.19/source/include/linux/pid_namespace.h
Related
One of the most widely used functions for output generation in Omnet++ is recordScalar.
virtual void recordScalar (cComponent *component, const char *name, double value, opp_string_map *attributes=nullptr)=0
Is there a more comprehensive function than recordScalar that stores structured data as value instead of storing a double number? Or coding it ourselves.
Or coding a similar function to write mentioned outputs in a text file in the format of JSON by that function?
By structured data, I mean struct data type in c++. like this:
struct logtype {
int src;
int dest;
int messagescount; // the count of messages transmitted between src and dest and vice versa
};
Thanks
OMNeT++ does not contain ready to use tool for storing complex structures. However, OMNeT++ uses C++ and one can write own method that will store some data to a text file, or to a JSON file, or to any file.
I am working with go, specifically QT bindings. However, I do not understand the use of leading underscores in the struct below. I am aware of the use of underscores in general but not this specific example.
type CustomLabel struct {
core.QObject
_ func() `constructor:"init"`
_ string `property:"text"`
}
Does it relate to the struct tags?
Those are called blank-fields because the blank identifier is used as the field name.
They cannot be referred to (just like any variable that has the blank identifier as its name) but they take part in the struct's memory layout. Usually and practically they are used as padding, to align subsequent fields to byte-positions (or memory-positions) that match layout of the data coming from (or going to) another system. The gain is that so these struct values (or rather their memory space) can be dumped or read simply and efficiently in one step.
#mkopriva's answer details what the specific use case from the question is for.
A word of warning: these blank fields as "type-annotations" should be used sparingly, as they add unnecessary overhead to all (!) values of such struct. These fields cannot be referred to, but they still require memory. If you add a blank field whose size is 8 bytes (e.g. int64), if you create a million elements, those 8 bytes will count a million times. As such, this is a "flawed" use of blank fields: the intention is to add meta info to the type itself (not to its instances), yet the cost is that all elements will require increased memory.
You might say then to use a type whose size is 0, such as struct{}. It's better, as if used in the right position (e.g. being the first field, for reasoning see Struct has different size if the field order is different and also Why position of `[0]byte` in the struct matters?), they won't change the struct's size. Still, code that use reflection to iterate over the struct's fields will still have to loop over these too, so it makes such code less efficient (typically all marshaling / unmarshaling process). Also, since now we can't use an arbitrary type, we lose the advantage of carrying a type information.
This last statement (about when using struct{} we lose the carried type information) can be circumvented. struct{} is not the only type with 0 size, all arrays with 0 length also have zero size (regardless of the actual element type). So we can retain the type information by using a 0-sized array of the type we'd like to incorporate, such as:
type CustomLabel struct {
_ [0]func() `constructor:"init"`
_ [0]string `property:"text"`
}
Now this CustomLabel type looks much better performance-wise as the type in question: its size is still 0. And it is still possible to access the array's element type using Type.Elem() like in this example:
type CustomLabel struct {
_ [0]func() `constructor:"init"`
_ [0]string `property:"text"`
}
func main() {
f := reflect.ValueOf(CustomLabel{}).Type().Field(0)
fmt.Println(f.Tag)
fmt.Println(f.Type)
fmt.Println(f.Type.Elem())
}
Output (try it on the Go Playground):
constructor:"init"
[0]func()
func()
For an overview of struct tags, read related question: What are the use(s) for tags in Go?
You can think of it as meta info of the type, it's not accessible through an instance of that type but can be accessed using reflect or go/ast. This gives the interested package/program some directives as to what to do with that type. For example based on those tags it could generate code using go:generate.
Considering that one of the tags says constructor:"init" and the field's type is func() it's highly probable that this is used with go:generate to generate an constructor function or initializer method named init for the type CustomLabel.
Here's an example of using reflect to get the "meta" info (although as I've already mentioned, the specific qt example is probably meant to be handled by go:generate).
type CustomLabel struct {
_ func() `constructor:"init"`
_ string `property:"text"`
}
fmt.Println(reflect.ValueOf(CustomLabel{}).Type().Field(0).Tag)
// constructor:"init"
fmt.Println(reflect.ValueOf(CustomLabel{}).Type().Field(0).Type)
// func()
https://play.golang.org/p/47yWG4U0uit
In the cuRAND documentation, both types curandState_t and curandState are used. Are there any difference between them?
http://docs.nvidia.com/cuda/curand/device-api-overview.html#device-api-overview
They are the same. In curand_kernel.h you will find this:
typedef struct curandStateXORWOW curandState_t;
typedef struct curandStateXORWOW curandState;
i.e. both types are aliases of the state for the default XORWOW generator state type.
In boost multiindex example complex_structs, it use one key in the car_manufacturer struct for car_table.
If car_manufacturer have been modify to have 2 ID
struct car_manufacturer {
std::string name;
int cm_code;
car_manufacturer(const std::string& name_, const int& cm_code_):name(name_), cm_code(cm_code_){}
};
What will be the key_from_key struct looks like? Have try to add another KeyExtractor or use composite index inside key_from_key, but still cannot compile.
Please help on this. Thanks.
After some trying, my compile error is actually cause by using find() without boost::make_tuple.
Can use back the same key_from_key struct. Key1Extrator will be composite key of name and cm_code.
This question is out of pure curiosity. How does Cocoa define the id type? Is it just a typedef for a void *? Also, if you know which header file it is defined in, I would be interested in taking a look.
Hold down the command key and double click on any highlighted term to jump to its definition.
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
typedef struct objc_selector *SEL;
typedef id (*IMP)(id, SEL, ...);
It is delcared in /usr/include/objc/objc.h (on Leopard) as follows:
typedef struct objc_object {
Class isa;
} *id;
Which means it is not void * at all, but rather a pointer to a struct that contains a single member, pointing at the class definition. Interesting indeed.
I remember when I was just getting into C and learning that Objective-C was initially implemented as just a preprocessor layer on top of C. It isn't quite like that anymore.
The best reading on the topic I've found:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html
in objc.h
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
To find out on your own, in XCode, right click id -- or any other type -- and select Jump to definition. It's interesting to note the similarities to other C/C++ based object systems; an object pointer -- an id -- points to a struct that starts with a point to shared class information. I many C++ implementations, that would be the virtual function table, as it would be with Microsoft's COM. In Cocoa, the particulars of objc_class aren't revealed to us in the header file.
The id type is generally declared like:
typedef struct objc_object *id;
This is critical for Objective-C++ where the type is part of a mangled function name.
You can take a look in /usr/include/objc/objc.h
You can refer the doc here : http://opensource.apple.com/source/objc4/objc4-437/runtime/objc.h
Hope this will do a favor for you