How to export symbol from Linux kernel module in this case? - linux

I've got two kernel modules built, one of which is a net_device. My net_device module A depends on module B which provide some extra control mechanism to export device info.
I want module B to be able to call the "xmit" function which is in module A. As a result, module B will become dependent on module A if I simple export symbol from A. This, obviously, creates a "deadlock" dependency situation.
Does anyone have experience resolving this? How can I properly export the "xmit" function in A and let B use it?

You may provide the callback function from module A. In that case you don't need to export each function you need to the kernel namespace. I presume you just could supply some structure to the B. Something like:
internal header:
struct possible_ops {
int (*xmit)(...);
};
A:
struct private {
struct possible_ops *ops;
};
...
ops = kzalloc(sizeof(*ops));
ops->xmit = xmit;
B:
whatever(struct possible_ops *ops) {
if (ops && ops->xmit) {
ret = ops->xmit();
...
}
}

Related

code within statically linked library src files returning null

This is for windows using MSVC compiler version 14.28.29910:
Libraries built using colcon. This was meant for a ROS2 application but I dont believe ROS has anything to do with it.
I have been stuck on this issue for two days now and I still am at a loss as to what is going on. Any help will be greatly appreciated.
I have a library that I am statically linking against. Library A. it is built with colcon. Could this be a linking issue or an issue with the fact that I build library A with a certain set of preprocessors and I build library B with a different set of preprocessors that change the Gameobject class to a different version shown below but same function implementations.
ament_auto_add_library(A STATIC
${SOURCES}
${HEADERS}
)
ament_target_dependencies(A ${ALL_DEPENDS})
install(TARGETS
A
EXPORT A_export
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
ament_export_libraries(A_export HAS_LIBRARY_TARGET)
I then link to it from a ros2 package using the standard
target_link_libraries(B A)
I have a object called Gameobject that is defined in library A.
#file --------------------------Gameobject.h
#ifdef InLibraryA
class Gameobject
{
int GetPosx(){return data.x;}
}
#else
class Gameobject
{
int GetPosx(){return data.x;}
}
#endif
#file --------------------------MoveGameObject.h
class MoveGameObject
{
int MoveGObj_inline(Gameobject* g)
{
return g->getPosx();
}
int MoveGObj(Gameobject* g);
}
#file --------------------------MoveGameObject.cpp
int MoveGameObject::MoveGObj(Gameobject* g)
{
return g->getPosx();
}
Now in library B , I do this within a subscription callback
SomeCallback()
{
Gameobject* g = GetGObjectFromPool();
MoveGameObject* m= new MoveGameObject();
//this will return NULL value
int posx = m->MoveGObj(g);
//this will be fine because it was inlined?
int possx = m->MoveGObj_inline(g);
}
You will see that I get null when calling the function that was NOT inlined for calling the getter function from Gameobject. I dont get null for the inlined function even though they run the exact same code. Note that this only happens to non-inline functions that call Gameobject functions. Does not happen to functions that do not read memory from Gameobject. addingTwoInts() for example works fine Non-inline. There are no errors. It is undefined behavior. Any ideas on what I could be doing wrong for this to happen? The simplified code above is the same as to what is happening in my code, just removed unnecessary details.

Defining a struct member with private module types

I've been using a bunch of modules that have a build() function which returns a struct. However, when I try to create my own "super" struct to bundle them together, I run into the error module `xxx` is private rustc(E0603). If there is a trait I can pass the individual variable as a parameter but cannot figure out how to define/box it up for a struct.
The current example of this I'm hitting is when creating a hyper client.
// Error due to privacy and cannot use the trait to define the member type
// Both the "hyper_rustls::connector" and "hyper::client::connect::http" modules are private.
struct SecureClient {
client: hyper::client::Client<
hyper_rustls::connector::HttpsConnector<hyper::client::connect::http::HttpConnector>>
}
// Works, but passing the client everywhere as an individual variable is not realistic.
fn use_client(client: hyper::client::Client<impl hyper::client::connect::Connect>) -> () {
()
}
let https_conn = hyper_rustls::HttpsConnector::new(4);
let client: hyper::client::Client<_, hyper::Body> = hyper::Client::builder().build(https_conn);
Being newish to Rust, I'm struggling to figure out what the proper jargon is for what I'm trying to do, let alone make it work. Links to any docs or code examples about this would be appreciated.
Thanks
I'm not sure what you want to do, but you can use the public re-export hyper_rustls::HttpsConnector instead of the private hyper_rustls::connector::HttpsConnector and the public re-export hyper::client::HttpConnector instead of the private hyper::client::connect::http::HttpConnector.
You can read about re-exports here: https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#re-exporting-names-with-pub-use

How does Node.js module system handle modifying and accessing variable of the same module of different versions?

I have a custom Node.js module fooModule that has a private variable foo and public getter and setter to modify this variable.
I have another two modules: zooModule depends on the fooModule#^1.0.1 and cannot use pre-release so far and barModule that depends on the fooModule#^1.0.2-0 (pre-release patch version that contains some fix) and zooModule at the same time.
The barModule firstly sets the foo variable value and then zooModule reads the value of foo.
I have noticed that when the version of the fooModule dependency is the same, then it works as expected, in other words the foo variable is shared between two modules. However, using different versions results in undefined when accessing foo from zooModule.
Here is a small pseudo example to demonstrate the logic. Each of the modules is a standalone npm package.
// fooModule.js
let foo;
export const getFoo = () => foo;
export const setFoo = (newFoo) => foo = newFoo;
// zooModule.js uses v.1.0.1 of the fooModule
import { getFoo } from './fooModule.js'
export const zooFunc = () => {
const zoo = getFoo();
if(!zoo) return;
...
return zoo; //result depends on zoo
};
// barModule.js uses v.1.0.2-0 of the fooModule
import { setFoo } from './fooModule.js'
import { zooFunc } from './zooModule.js'
setFoo('foo');
zooFunc(); // What is the output?
As far as I am concerned, in case of different versions of the fooModule, we become two different instances of the module and accordingly of the variable foo?
I tried to explain the question best I could, but it was hard to explain what I mean, sorry if it is still unclear.
Could give me some hints where to read more about this or give some explanation on how this is supposed to work. Thanks for your time.
EDIT:I forgot to mention that I have this use case in a frontend project bundled by a webpack.

Struct declaration order

If I define structs at the module level, I can reference not-yet defined structs.
struct S {
ComesLater c;
}
struct ComesLater {}
But If I do the same inside an unittest or a function block, it doesn't work:
unittest {
struct S {
ComesLater c;
}
struct ComesLater {}
}
Error: undefined identifier 'ComesLater'
Why is that? How can I get order-independent declarations inside functions? Is there some kind of forward-declaration in d? I need this because I generate structs using mixin and ordering the declarations in the order of their inner-dependencies would be quite some effort, sometimes impossible, if there are circularly referencing structs. (using pointers.)
Declarations inside functions, unittests, or anywhere else that statements can actually be executed are indeed order-dependent because their values may depend on the code before them running. Think of a local variable:
int a;
writeln(a);
a = b;
int b = get_user_line();
If order wasn't important there, when would the two functions get called? Would the user be asked for a line before the writeln as the declarations are rewritten?
The current behavior of making b an undefined variable error keeps it simple and straightforward.
It works independent of order in other contexts because there is no executable code that it can depend on, so there's no behavior that can change if the compiler needs to internally think about it differently.
So:
How can I get order-independent declarations inside functions?
Change the context such that there is no executable code... put it all inside another struct!
void main() { // or unittest { }
struct Holder {
static struct S {
C c;
}
static struct C {}
}
}
Since execution happens around the holder and doesn't happen inside it, the order of declaration inside doesn't matter again. Since you can define almost anything inside a struct, you can use this for variables, functions, other structs, and so on. Basically all you have to do is wrap your existing code inside the struct Holder {} brackets.
By making everything static inside, you can just use it like a container and reference the stuff with Holder.S, etc., on the outside.

How to make typedef type accessible in multiple files?

typedef has great benefits, but I don't know how to include the same type in multiple files is haxe?
for example I need to use this type in a.hx and b.hx files:
typedef JsonControl = {
var name:String;
var type:String;
var user_answer:String;
var answer:Array<String>;
var htmlID:String;
}
how to do so?
It works like a regular class definition, so preferably you would define a package and save it as a .hx file. Then simply import it where you want to use it.
As another answer states, the rules for how haxe discovers typedefs is identical to how it finds classes.
Type-Named Module Files
The easiest way to share a type between multiple files in Haxe is to create a module named after that type. For example, suppose you are in package x and want to access the type MyType from multiple files. You would create the file x/MyType.hx and place MyType’s definition in x/MyType.hx. This file might look like this:
package x;
typedef MyType = {
id:String,
}
Module Sub-Types
If you want, you may place MyType in a differently-named file such as x/MySharedTypes.hx. If you do that, accessing it takes more work. To consume it in another file, you either need to have an import x.MySharedTypes statement or type the module-qualified name x.MySharedTypes.MyType (from a file in package x, you only need to type MySharedTypes.MyType). In this example, you might have:
x/MySharedTypes.hx:
package x;
typedef MyType = {
id:String,
}
x/MyFirstConsumer.hx:
package x;
import x.MySharedTypes;
class MyFirstConsumer {
var instance:MyType;
}
x/MySecondConsumer.hx:
package x;
class MySecondConsumer {
var instance:MySharedTypes.MyType;
}
Documentation
You may read further details in the page titled Module Sub-Types in the manual.

Resources