Matlab S-Function crashing when set to dynamically sized - matlab

i have following problem. I have created a S-Function which should read a matrix from a text file, where the output size of the output port is not known and therefore set as dynamically sized. Matlab is then crashing. When setting for fixed size, there is no problem. I hope someone could help me out
#define S_FUNCTION_NAME Data_Input
#define S_FUNCTION_LEVEL 2
/*---- Define size of input ports ---------------------------------------------------*/
#define OUTPUTSIZE 50
/*-----------------------------------------------------------------------------------*/
#include <stdio.h>
#include "simstruc.h"
int_T in;
/*---- Define data types ------------------------------------------------------------*/
typedef struct SBufferData
{
real32_T data[50];
int32_T number;
} SBuffer;
/*==== mdlCheckParameters ===========================================================*/
#undef MDL_CHECK_PARAMETERS /* Change to #undef to remove function */
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
static void mdlCheckParameters(SimStruct *S)
{
}
#endif /* MDL_CHECK_PARAMETERS */
/*===================================================================================*/
/*=== mdlInitializeSizes ============================================================*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S,1); /* number of expected parameters */
ssSetSFcnParamTunable(S, 0, 0); /* sets parameter 1 to be non-tunable */
ssSetSFcnParamTunable(S, 1, 0); /* sets parameter 2 to be non-tunable */
ssSetNumContStates(S,0); /* number of continuous states */
ssSetNumDiscStates(S,0); /* number of discrete states */
ssSetNumInputPorts(S,0); /* number of input ports */
ssSetNumOutputPorts(S,1); /* number of output ports */
ssSetOutputPortWidth(S,0,DYNAMICALLY_SIZED); /* first output port width */
ssSetOutputPortDataType(S,0,SS_SINGLE); /* first output port data type */
ssSetNumSampleTimes(S,0); /* number of sample times */
ssSetNumRWork(S,0); /* number real work vector elements */
ssSetNumIWork(S,0); /* number int_T work vector elements */
ssSetNumPWork(S,1); /* number ptr work vector elements */
ssSetNumModes(S,0); /* number mode work vector elements */
ssSetNumNonsampledZCs(S,0); /* number of nonsampled zero crossing */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch reported by the Simulink engine*/
}
}
/*===================================================================================*/
/*==== mdlInitializeSampleTimes =====================================================*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
real_T dSampleTime=(mxGetPr(ssGetSFcnParam(S,0))[0]);
ssSetSampleTime(S, 0, dSampleTime);
ssSetOffsetTime(S, 0, 0.0);
}
/*===================================================================================*/
/*==== mdlStart =====================================================================*/
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
FILE *fp;
int_T i, index=0;
SBuffer *buffer;
float temp;
/*---- Retrieve pointer to pointers work vector -------------------------------------*/
void **PWork = ssGetPWork(S);
buffer = (SBuffer *)malloc(OUTPUTSIZE * sizeof(SBuffer));
PWork[0] = (void *)buffer;
/*---- Read from text-file ----------------------------------------------------------*/
fp = fopen("myfile.txt", "r");
while(feof(fp)==0)
{
fscanf(fp,"%f",&temp);
buffer -> data[index] = temp;
index++;
}
buffer -> number = (int32_T) index;
fclose(fp);
}
#endif /* MDL_START */
/*===================================================================================*/
/*==== mdlOutputs ===================================================================*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
/*---- Get input ports --------------------------------------------------------------*/
real_T *y1=(real_T *)ssGetOutputPortSignal(S,0);
int_T i;
SBuffer *buffer;
/*---- Retrieve pointer to pointers work vector -------------------------------------*/
void **PWork = ssGetPWork(S);
buffer = PWork[0];
for (i = 0; i < buffer->number; i++)
{
y1[i] = buffer->data[i];
}
}
/*===================================================================================*/
/*==== mdlTerminate =================================================================*/
static void mdlTerminate(SimStruct *S)
{
SBuffer *buffer;
/*---- Retrieve pointer to pointers work vector -------------------------------------*/
void **PWork = ssGetPWork(S);
buffer = PWork[0];
/*---- Deallocate structure 'info' and 'buffer' -------------------------------------*/
free(buffer);
}
/*===================================================================================*/
/*==== Required S-function trailer ==================================================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
/*===================================================================================*/

Related

mbed os: using timer

I am using the board STM32F446RE to run [this project][1]. I am trying to set up and use timer 10 to count. The main.c file is below.
#include "radio.h"
#include <cstdio>
#if defined(SX128x_H)
#define BW_KHZ 200
#define SPREADING_FACTOR 7
#define CF_HZ 2487000000
#define TX_DBM 6
#else
#if defined(SX126x_H)
#define TX_DBM (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14)
#else
#define TX_DBM 20
#endif
#define BW_KHZ 125
#define SPREADING_FACTOR 7
#define CF_HZ 915000000
#endif
/**********************************************************************/
EventQueue queue(4 * EVENTS_EVENT_SIZE);
TIM_HandleTypeDef htim10;
static void MX_TIM10_Init(void)
{
/* USER CODE BEGIN TIM10_Init 0 */
/* USER CODE END TIM10_Init 0 */
/* USER CODE BEGIN TIM10_Init 1 */
/* USER CODE END TIM10_Init 1 */
htim10.Instance = TIM10;
htim10.Init.Prescaler = 0;
htim10.Init.CounterMode = TIM_COUNTERMODE_UP;
htim10.Init.Period = 65535;
htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
//htim10.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim10) != HAL_OK)
{
printf("Error Error\r\n");
}
/* USER CODE BEGIN TIM10_Init 2 */
/* USER CODE END TIM10_Init 2 */
}
void tx_test()
{
static uint8_t seq = 0;
Radio::radio.tx_buf[0] = seq++; /* set payload */
Radio::Send(1, 0, 0, 0); /* begin transmission */
printf("sent\r\n");
/* {
mbed_stats_cpu_t stats;
mbed_stats_cpu_get(&stats);
printf("canDeep:%u ", sleep_manager_can_deep_sleep());
printf("Uptime: %llu ", stats.uptime / 1000);
printf("Sleep time: %llu ", stats.sleep_time / 1000);
printf("Deep Sleep: %llu\r\n", stats.deep_sleep_time / 1000);
}*/
}
void txDoneCB()
{
printf("got-tx-done\r\n");
queue.call_in(500, tx_test);
}
void rxDoneCB(uint8_t size, float rssi, float snr)
{
}
void radio_irq_callback()
{
queue.call(Radio::service);
}
const RadioEvents_t rev = {
/* DioPin_top_half */ radio_irq_callback,
/* TxDone_topHalf */ NULL,
/* TxDone_botHalf */ txDoneCB,
/* TxTimeout */ NULL,
/* RxDone */ rxDoneCB,
/* RxTimeout */ NULL,
/* RxError */ NULL,
/* FhssChangeChannel */NULL,
/* CadDone */ NULL
};
int main()
{
printf("\r\nreset-tx ");
Radio::Init(&rev);
Radio::Standby();
Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
Radio::SetChannel(CF_HZ);
Radio::set_tx_dbm(TX_DBM);
// preambleLen, fixLen, crcOn, invIQ
Radio::LoRaPacketConfig(8, false, true, false);
MX_TIM10_Init();
HAL_TIM_Base_Start_IT(&htim10);
HAL_Delay(500);
__HAL_TIM_SET_COUNTER(&htim10, 15);
uint16_t timer_val = __HAL_TIM_GET_COUNTER(&htim10);
printf("timer value = %d", timer_val);
queue.call_in(500, tx_test);
queue.dispatch();
}
Basically, I used the initialisation code taken from STM32CubeMX and I configured the clock of timer 10 in the file system_clock.c (normally in STM32CubeMX, clocks are configured in Clock Configuration tab). Then I initialized and started the time before getting the counter from that function. However, all I get from __HAL_TIM_GET_COUNTER is 0.
I want to ask what I did wrong.
Thank you,
Huy Nguyen
[1]: https://os.mbed.com/users/dudmuck/code/simple_tx/
This is purely speculation, but maybe you've not enabled the clock for TIM10. I don't know if that would explain what you are seeing or not. You might need something like __HAL_RCC_TIM10_CLK_ENABLE();

PyArg_ParseTuple doesn't parse a string correctly

So I'm learning how to make Python modules in C and today I wanted to learn how to create a custom type.
I wrote a module with one class named "Car" which stores a name, year and maxGas.
Problem is, when I try to create an instance of this class in Python the name property contains random characters instead of the given name.
#include <Python.h>
#include <stdio.h> //printf debugging
#include <stdlib.h> //free
#include "structmember.h"
typedef struct {
PyObject_HEAD
char* name; //allocated and freed by Python
int year;
float maxGas;
} cars_Car;
static int Car_init(cars_Car* self, PyObject* args, PyObject* kwargs){
/*
//Method #1
char* tmp;
// v- Note the &
if(!PyArg_ParseTuple(args, "sif", &tmp, &self->year, &self->maxGas)){
return -1;
}
self->name = (char*)malloc( sizeof(char) * (strlen(tmp) + 1) );
sprintf(self->name, "%s", tmp);
printf("Given name: %s\n", self->name);
//Works
*/
//Method #2
// v- Note the &
if(!PyArg_ParseTuple(args, "sif", &self->name, &self->year, &self->maxGas)){
return -1;
}
printf("Given name: %s\n", self->name); // Works, but repr() and name/getName() are broken
return 0;
}
static void Car_dealloc(cars_Car* self){
//free(self->name); //For Method #2 in init()
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject* Car_repr(cars_Car* self){
return PyUnicode_FromFormat("<Car name=\"%s\" year=%d tankSize=%SL>",
self->name, self->year, Py_BuildValue("f", self->maxGas) /* %f not supported, so we convert it to a Python string */ );
}
static PyObject* Car_new(PyTypeObject* type, PyObject* args, PyObject* kwargs){
cars_Car* self;
self = (cars_Car*)type->tp_alloc(type, 0);
self->name = NULL;
self->year = 0;
self->maxGas = 0.0f;
return (PyObject*)self;
}
/* Methods */
static PyObject* Car_getName(cars_Car* self, PyObject* args){
return Py_BuildValue("s", self->name);
}
static PyObject* Car_getYear(cars_Car* self, PyObject* args){
return Py_BuildValue("i", self->year);
}
static PyObject* Car_getTankSize(cars_Car* self, PyObject* args){
return Py_BuildValue("f", self->maxGas);
}
static PyMethodDef Car_methods[] = {
{"getName", (PyCFunction)Car_getName, METH_NOARGS,
"Get the car's name.",
},
{"getYear", (PyCFunction)Car_getYear, METH_NOARGS,
"Get the car's year.",
},
{"getTankSize", (PyCFunction)Car_getTankSize, METH_NOARGS,
"Get the car's tank size.",
},
{NULL}
};
static struct PyMemberDef Car_members[] = {
{"name", T_STRING, offsetof(cars_Car, name), READONLY, "Car name"},
{"year", T_INT, offsetof(cars_Car, year), READONLY, "Car year"},
{"maxGas", T_FLOAT, offsetof(cars_Car, maxGas), READONLY, "Car maxGas"},
{NULL}
};
static PyTypeObject cars_CarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cars.Car", /* tp_name */
sizeof(cars_Car), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Car_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
(reprfunc)Car_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"Cars objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Car_methods, /* tp_methods */
Car_members, /* tp_members */
NULL, /* tp_getset */
NULL, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Car_init, /* tp_init */
0, /* tp_alloc */
Car_new, /* tp_new */
0, /* tp_free */
0 /* tp_is_gc */
};
static PyModuleDef modcars = {
PyModuleDef_HEAD_INIT,
"cars",
"Example module that creates an extension type.",
-1,
NULL, NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_Cars(void) {
PyObject* m;
cars_CarType.tp_new = PyType_GenericNew;
if (PyType_Ready(&cars_CarType) < 0)
return NULL;
m = PyModule_Create(&modcars);
if (m == NULL)
return NULL;
Py_INCREF(&cars_CarType);
PyModule_AddObject(m, "Car", (PyObject *)&cars_CarType);
return m;
}
As you can see, I mentioned 2 different methods in the Car_init function.
When using the first method, I get the following result:
>>> import Cars
>>> x = Cars.Car("The Big Car", 2020, 4.2)
Given name: The Big Car #printf() works
>>> x
<Car name="????" year=2020 tankSize=4.199999809265137L #??? - bunch of random characters
I'm not sure what's causing this, although my guess is that my code is probably writing to some memory address where it's not supposed to. It also looks like there's a floating point precision error.
Interestingly when I directly call getName() or try to access the name attribute, it works.
>>> Cars.Car("The Big Car", 2020, 4.2).name
'The Big Car'
>>> Cars.Car("The Big Car", 2020, 4.2).getName()
'The Big Car'
>>> repr(Cars.Car("The Big Car", 2020, 4.2))
'<Car name="The Big Car" year=2020 tankSize=4.199999809265137L>'
If I switch to the second method everything works fine. I'm not sure why using a temporary variable helps. Also my code doesn't write anything to name, except when initialising it. Everywhere else it's just being read. I also thought about the garbage collector, maybe it's freeing name?
The documentation for the s argument says
A pointer to an existing string is stored in the character pointer variable whose address you pass
Implicitly this pointer is only valid while the existing object is valid (i.e. possibly only until the args tuple is freed).
For your first example that's fine - you allocate new memory with malloc and copy from it. You do that during your initial function call so you know the args tuple and all its contents still exists. You need to remember to free the memory of course.
For your second example you just take a pointer to some memory owned by something else. Keeping a pointer to the memory doesn't do anything to tell Python to keep the memory valid. Therefore the memory is valid during Car_init but most likely invalid shortly after that.

Same S-Function used twice, one works one doesn't

I have a simulink model and I made a simple s-function block to add to my model and it works.
The catch if if I copy or use that same s-function twice in my model, when I run it only the last s-function called will do what it's supposed to do, the other will output a 0.
#define S_FUNCTION_NAME DivByZero
#define S_FUNCTION_LEVEL 2
#define NUMBER_OF_INPUTS 1
#define NUMBER_OF_OUTPUTS 1
#include "DivByZero.h"
#include "mex.h"
#if !defined(MATLAB_MEX_FILE)
/*
* This file cannot be used directly
*/
# error This_file_can_be_used_only_during_simulation_inside_Simulink
#endif
float32 DivByZeroInput;
float32 DivByZeroOutput;
const void * inputPorts[NUMBER_OF_INPUTS];
const void * outputPorts[NUMBER_OF_OUTPUTS];
static void mdlInitializeSizes(SimStruct *S)
{
uint8 i;
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
{
return; /* Parameter mismatch reported by the Simulink engine */
}
if (!ssSetNumInputPorts(S, NUMBER_OF_INPUTS)) return;
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortDataType(S, 0, SS_SINGLE);
for (i = 0; i < NUMBER_OF_INPUTS; i++)
{
ssSetInputPortDirectFeedThrough(S, i, 1); /* Set direct feedthrough flag */
ssSetInputPortRequiredContiguous(S, i, 1); /*direct input signal access*/
}
if (!ssSetNumOutputPorts(S, NUMBER_OF_OUTPUTS)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetOutputPortDataType(S, 0, SS_SINGLE);
/* Set Sample Time */
ssSetNumSampleTimes(S, 1);
/* Specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S, SS_OPTION_ALLOW_PORT_SAMPLE_TIME_IN_TRIGSS);
ssSupportsMultipleExecInstances(S, true); //set so the s-function can be used in a for each subsystem block
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
/* Inherits sample time from the driving block */
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}
#define MDL_START
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
uint8 i;
/* Save Input ports */
for (i = 0; i < NUMBER_OF_INPUTS; i++)
{
inputPorts[i] = ssGetInputPortSignal(S, i);
}
/* Save Output ports */
for (i = 0; i < NUMBER_OF_OUTPUTS; i++)
{
outputPorts[i] = ssGetOutputPortRealSignal(S, i);
}
}
#endif
static void mdlOutputs(SimStruct *S, int_T tid)
{
memcpy(&DivByZeroInput, inputPorts[0], sizeof(DivByZeroInput));//gets the input port info
if (fabs(DivByZeroInput) > 0.00001f)
{
DivByZeroOutput = DivByZeroInput;
}
else
{
if (DivByZeroInput >= 0.0f)
{
DivByZeroOutput = 0.00001f;
}
else
{
DivByZeroOutput= -0.00001f;
}
}
memcpy(outputPorts[0], &DivByZeroOutput, sizeof(DivByZeroOutput));//sets the output port
}
static void mdlTerminate(SimStruct *S)
{
/* MODEL TERMINATE */
}
/* END OF TEMPLATE */
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
The s-function is supposed to provide a simple division by zero protection as you can see from the code.
In the image below, if I delete S-Function1, S-Function will then work as intended.
I tried putting it in a library block and just put 2 library blocks and I got the same result
You are defining these variables:
float32 DivByZeroInput;
float32 DivByZeroOutput;
const void * inputPorts[NUMBER_OF_INPUTS];
const void * outputPorts[NUMBER_OF_OUTPUTS];
These variables are shared across both instances, which means both S-Functions write to the same output addresses.
Side note: Do you really have to use an S-Function? Creating the same from a few native simulink blocks is probably much easier.

Can kernel module take initiative to send message to user space with netlink?

I am trying to run following code, which was copied from here. I have made few changes to run it with older kernel versions.
When I insert kernel module, nlmsg_multicast() fails and logs as nlmsg_multicast() error: -3 in /var/log/messages.
While running user space program, socket() fails.
What exactly I want to do is,
kernel module creates a socket, regardless of any process in user space
kernel module send some events to user space
If any process in user space reply to an event, kernel module process on that reply
Since, It may happen that no process in user space available to reply on event, even in that case module must send event and wait for a while for response.
Is it possible to send first message from kernel module to a process in user space? How can I do this?
Kernel module code:
Makefile
obj-m := foo.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
foo.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netlink.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
/* Protocol family, consistent in both kernel prog and user prog. */
#define MYPROTO NETLINK_USERSOCK
/* Multicast group, consistent in both kernel prog and user prog. */
#define MYGRP 21
static struct sock *nl_sk = NULL;
static void send_to_user(void)
{
struct sk_buff *skb;
struct nlmsghdr *nlh;
char *msg = "Hello from kernel";
int msg_size = strlen(msg) + 1;
int res;
pr_info("Creating skb.\n");
skb = nlmsg_new(NLMSG_ALIGN(msg_size + 1), GFP_KERNEL);
if (!skb) {
pr_err("Allocation failure.\n");
return;
}
nlh = nlmsg_put(skb, 0, 1, NLMSG_DONE, msg_size + 1, 0);
strcpy(nlmsg_data(nlh), msg);
pr_info("Sending skb.\n");
res = nlmsg_multicast(nl_sk, skb, 0, MYGRP, GFP_KERNEL);
if (res < 0)
pr_info("nlmsg_multicast() error: %d\n", res);
else
pr_info("Success.\n");
}
static int __init hello_init(void)
{
pr_info("Inserting hello module.\n");
//nl_sk = netlink_kernel_create(&init_net, MYPROTO, NULL);
nl_sk = netlink_kernel_create(&init_net, MYPROTO, 0, NULL, NULL, THIS_MODULE);
if (!nl_sk) {
pr_err("Error creating socket.\n");
return -10;
}
send_to_user();
netlink_kernel_release(nl_sk);
return 0;
}
static void __exit hello_exit(void)
{
pr_info("Exiting hello module.\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
The user space program:
(Compiled with gcc somename.c)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <unistd.h>
/* Protocol family, consistent in both kernel prog and user prog. */
#define MYPROTO NETLINK_USERSOCK
/* Multicast group, consistent in both kernel prog and user prog. */
#define MYMGRP 21
int open_netlink(void)
{
int sock;
struct sockaddr_nl addr;
int group = MYMGRP;
sock = socket(AF_NETLINK, SOCK_RAW, MYPROTO);
if (sock < 0) {
printf("sock < 0.\n");
return sock;
}
memset((void *) &addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
/* This doesn't work for some reason. See the setsockopt() below. */
addr.nl_groups = MYMGRP;
if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
printf("bind < 0.\n");
return -1;
}
/*
* 270 is SOL_NETLINK. See
* http://lxr.free-electrons.com/source/include/linux/socket.h?v=4.1#L314
* and
* https://stackoverflow.com/questions/17732044/
*/
/*if (setsockopt(sock, 270, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) < 0) {
printf("setsockopt < 0\n");
return -1;
}*/
return sock;
}
void read_event(int sock)
{
struct sockaddr_nl nladdr;
struct msghdr msg;
struct iovec iov;
char buffer[65536];
int ret;
iov.iov_base = (void *) buffer;
iov.iov_len = sizeof(buffer);
msg.msg_name = (void *) &(nladdr);
msg.msg_namelen = sizeof(nladdr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
printf("Ok, listening.\n");
ret = recvmsg(sock, &msg, 0);
if (ret < 0)
printf("ret < 0.\n");
else
printf("Received message payload: %s\n", NLMSG_DATA((struct nlmsghdr *) &buffer));
}
int main(int argc, char *argv[])
{
int nls;
nls = open_netlink();
if (nls < 0)
return nls;
while (1)
read_event(nls);
return 0;
}
Thank you for your time!
This looks like bad design (because upper layers should depend on lower layers, not the other way around). But if you're convinced the kernel cannot sit idle or operate using default configuration until userspace can fetch info, then first install this tool (might want to read the core guide too), and then do something like this:
Kernel:
#include <linux/module.h>
#include <linux/kernel.h>
#include <net/netlink.h>
#include <net/net_namespace.h>
#define MYPROTO NETLINK_USERSOCK
#define MYGRP 22
static struct sock *nl_sk;
static struct timer_list timer;
void try_send(unsigned long data)
{
struct sk_buff *skb;
struct nlmsghdr *nlh;
char *msg = "Hello from kernel";
int msg_size = strlen(msg) + 1;
int res;
skb = nlmsg_new(NLMSG_ALIGN(msg_size + 1), GFP_ATOMIC);
if (!skb) {
pr_err("Allocation failure.\n");
return;
}
nlh = nlmsg_put(skb, 0, 1, NLMSG_DONE, msg_size + 1, 0);
strcpy(nlmsg_data(nlh), msg);
pr_info("Sending multicast.\n");
res = nlmsg_multicast(nl_sk, skb, 0, MYGRP, GFP_ATOMIC);
if (res < 0) {
pr_info("nlmsg_multicast() error: %d. Will try again later.\n", res);
/* Wait 1 second. */
mod_timer(&timer, jiffies + msecs_to_jiffies(1000));
} else {
pr_info("Success.\n");
}
}
static int handle_netlink_message(struct sk_buff *skb_in, struct nlmsghdr *nl_hdr)
{
char *hello;
hello = NLMSG_DATA(nl_hdr);
pr_info("Userspace says '%s.'\n", hello);
return 0;
}
static void receive_answer(struct sk_buff *skb)
{
netlink_rcv_skb(skb, &handle_netlink_message);
}
static int __init hello_init(void)
{
pr_info("Inserting module.\n");
nl_sk = netlink_kernel_create(&init_net, MYPROTO, 0, receive_answer, NULL, THIS_MODULE);
if (!nl_sk) {
pr_err("Error creating socket.\n");
return -10;
}
init_timer(&timer);
timer.function = try_send;
timer.expires = jiffies + 1000;
timer.data = 0;
add_timer(&timer);
return 0;
}
static void __exit hello_exit(void)
{
del_timer_sync(&timer);
netlink_kernel_release(nl_sk);
pr_info("Exiting module.\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
User (I'm compiling using gcc usr.c -I/usr/include/libnl3 -lnl-3 -Wall, your mileage may vary):
#include <netlink/netlink.h>
#include <netlink/msg.h>
#define MYPROTO NETLINK_USERSOCK
#define MYMGRP 22
struct nl_sock *sk;
void respond_to_kernel(void)
{
char *response = "foo bar";
int error;
error = nl_send_simple(sk, 12345, NLMSG_DONE, response, strlen(response) + 1);
if (error < 0) {
printf("nl_send_simple() threw errcode %d.\n", error);
printf("libnl's message: %s", nl_geterror(error));
} else {
printf("Responded %d bytes.\n", error);
}
}
int receive_kernel_request(struct nl_msg *msg, void *arg)
{
char *hello;
hello = nlmsg_data(nlmsg_hdr(msg));
printf("Kernel says '%s'.\n", hello);
respond_to_kernel();
return 0;
}
int prepare_socket(void)
{
int error;
sk = nl_socket_alloc();
if (!sk) {
printf("nl_socket_alloc() returned NULL.\n");
return -1;
}
nl_socket_disable_seq_check(sk);
error = nl_socket_modify_cb(sk, NL_CB_FINISH, NL_CB_CUSTOM, receive_kernel_request, NULL);
if (error < 0) {
printf("Could not register callback function. Errcode: %d\n", error);
goto fail;
}
error = nl_connect(sk, MYPROTO);
if (error < 0) {
printf("Connection failed: %d\n", error);
goto fail;
}
error = nl_socket_add_memberships(sk, MYMGRP, 0);
if (error) {
printf("Could not register to the multicast group. %d\n", error);
goto fail;
}
return 0;
fail:
printf("libnl's message: %s\n", nl_geterror(error));
nl_socket_free(sk);
return error;
}
int wait_for_kernel_message(void)
{
int error;
printf("Waiting for kernel request...\n");
error = nl_recvmsgs_default(sk);
if (error < 0) {
printf("nl_send_simple() threw errcode %d.\n", error);
printf("libnl's message: %s\n", nl_geterror(error));
return error;
}
return 0;
}
void destroy_socket(void)
{
nl_socket_free(sk);
}
int main(int argc, char *argv[])
{
int error;
error = prepare_socket();
if (error)
return error;
error = wait_for_kernel_message();
destroy_socket();
return error;
}
Tested on kernel 3.2. (Sorry; that's the lowest I have right now.)
This is an example without libnl.
I put all functions in a one file. The coding style is not good. It is only for an example.
I hope it's helpful for you.
I have tested the code in Ubuntu 15.04 which's kernel is kernel 3.19.0-15.
Kernel Module
#include <linux/kernel.h>
#include <linux/module.h>
#include <net/genetlink.h>
static struct timer_list timer;
/* Code based on http://stackoverflow.com/questions/26265453/netlink-multicast-kernel-group/33578010#33578010 */
/**
* This callback runs whenever the socket receives messages.
* We don't use it now, but Linux complains if we don't define it.
*/
static int hello(struct sk_buff *skb, struct genl_info *info)
{
pr_info("Received a message in kernelspace.\n");
return 0;
}
/**
* Attributes are fields of data your messages will contain.
* The designers of Netlink really want you to use these instead of just dumping
* data to the packet payload... and I have really mixed feelings about it.
*/
enum attributes {
/*
* The first one has to be a throwaway empty attribute; I don't know
* why.
* If you remove it, ATTR_HELLO (the first one) stops working, because
* it then becomes the throwaway.
*/
ATTR_DUMMY,
ATTR_HELLO,
ATTR_FOO,
/* This must be last! */
__ATTR_MAX,
};
/**
* Here you can define some constraints for the attributes so Linux will
* validate them for you.
*/
static struct nla_policy policies[] = {
[ATTR_HELLO] = { .type = NLA_STRING, },
[ATTR_FOO] = { .type = NLA_U32, },
};
/**
* Message type codes. All you need is a hello sorta function, so that's what
* I'm defining.
*/
enum commands {
COMMAND_HELLO,
/* This must be last! */
__COMMAND_MAX,
};
/**
* Actual message type definition.
*/
struct genl_ops ops[] = {
{
.cmd = COMMAND_HELLO,
.flags = 0,
.policy = policies,
.doit = hello,
.dumpit = NULL,
},
};
/**
* A Generic Netlink family is a group of listeners who can and want to speak
* your language.
* Anyone who wants to hear your messages needs to register to the same family
* as you.
*/
struct genl_family family = {
.id = GENL_ID_GENERATE,
.hdrsize = 0,
.name = "PotatoFamily",
.version = 1,
.maxattr = __ATTR_MAX,
};
/**
* And more specifically, anyone who wants to hear messages you throw at
* specific multicast groups need to register themselves to the same multicast
* group, too.
*/
struct genl_multicast_group groups[] = {
{ .name = "PotatoGroup" },
};
void send_multicast(unsigned long arg)
{
struct sk_buff *skb;
void *msg_head;
unsigned char *msg = "TEST";
int error;
pr_info("----- Running timer -----\n");
pr_info("Newing message.\n");
skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (!skb) {
pr_err("genlmsg_new() failed.\n");
goto end;
}
pr_info("Putting message.\n");
msg_head = genlmsg_put(skb, 0, 0, &family, 0, COMMAND_HELLO);
if (!msg_head) {
pr_err("genlmsg_put() failed.\n");
kfree_skb(skb);
goto end;
}
pr_info("Nla_putting string.\n");
error = nla_put_string(skb, ATTR_HELLO, msg);
if (error) {
pr_err("nla_put_string() failed: %d\n", error);
kfree_skb(skb);
goto end;
}
pr_info("Nla_putting integer.\n");
error = nla_put_u32(skb, ATTR_FOO, 12345);
if (error) {
pr_err("nla_put_u32() failed: %d\n", error);
kfree_skb(skb);
goto end;
}
pr_info("Ending message.\n");
genlmsg_end(skb, msg_head);
pr_info("Multicasting message.\n");
/*
* The family has only one group, so the group ID is just the family's
* group offset.
* mcgrp_offset is supposed to be private, so use this value for debug
* purposes only.
*/
pr_info("The group ID is %u.\n", family.mcgrp_offset);
error = genlmsg_multicast_allns(&family, skb, 0, 0, GFP_KERNEL);
if (error) {
pr_err("genlmsg_multicast_allns() failed: %d\n", error);
pr_err("(This can happen if nobody is listening. "
"Because it's not that unexpected, "
"you might want to just ignore this error.)\n");
goto end;
}
pr_info("Success.\n");
end:
mod_timer(&timer, jiffies + msecs_to_jiffies(2000));
}
static int init_socket(void)
{
int error;
pr_info("Registering family.\n");
error = genl_register_family_with_ops_groups(&family, ops, groups);
if (error)
pr_err("Family registration failed: %d\n", error);
return error;
}
static void initialize_timer(void)
{
pr_info("Starting timer.\n");
init_timer(&timer);
timer.function = send_multicast;
timer.expires = 0;
timer.data = 0;
mod_timer(&timer, jiffies + msecs_to_jiffies(2000));
}
static int __init hello_init(void)
{
int error;
error = init_socket();
if (error)
return error;
initialize_timer();
pr_info("Hello module registered.\n");
return 0;
}
static void __exit hello_exit(void)
{
del_timer_sync(&timer);
genl_unregister_family(&family);
pr_info("Hello removed.\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
Kernel Module Makefile
PWD := $(shell pwd)
KVERSION := $(shell uname -r)
KERNEL_DIR = /usr/src/linux-headers-$(KVERSION)/
MODULE_NAME = genl_kern_grp
obj-m := $(MODULE_NAME).o
all:
make -C $(KERNEL_DIR) M=$(PWD) modules
clean:
make -C $(KERNEL_DIR) M=$(PWD) clean
User Code - main.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <linux/genetlink.h>
/* Code based on libnl-3 */
/* Code based on http://stackoverflow.com/questions/26265453/netlink-multicast-kernel-group/33578010#33578010 */
/* Code based on http://www.electronicsfaq.com/2014/02/generic-netlink-sockets-example-code.html */
/* Code based on http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.html */
/* Based on libnl-3 attr.h */
/**
* #ingroup attr
* Basic attribute data types
*
* See section #core_doc{core_attr_parse,Attribute Parsing} for more details.
*/
enum {
NLA_UNSPEC, /**< Unspecified type, binary data chunk */
NLA_U8, /**< 8 bit integer */
NLA_U16, /**< 16 bit integer */
NLA_U32, /**< 32 bit integer */
NLA_U64, /**< 64 bit integer */
NLA_STRING, /**< NUL terminated character string */
NLA_FLAG, /**< Flag */
NLA_MSECS, /**< Micro seconds (64bit) */
NLA_NESTED, /**< Nested attributes */
NLA_NESTED_COMPAT,
NLA_NUL_STRING,
NLA_BINARY,
NLA_S8,
NLA_S16,
NLA_S32,
NLA_S64,
__NLA_TYPE_MAX,
};
#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
/**
* #ingroup attr
* Attribute validation policy.
*
* See section #core_doc{core_attr_parse,Attribute Parsing} for more details.
*/
struct nla_policy {
/** Type of attribute or NLA_UNSPEC */
uint16_t type;
/** Minimal length of payload required */
uint16_t minlen;
/** Maximal length of payload allowed */
uint16_t maxlen;
};
/**
* Attributes and commands have to be the same as in kernelspace, so you might
* want to move these enums to a .h and just #include that from both files.
*/
enum attributes {
ATTR_DUMMY,
ATTR_HELLO,
ATTR_FOO,
/* This must be last! */
__ATTR_MAX,
};
enum commands {
COMMAND_HELLO,
/* This must be last! */
__COMMAND_MAX,
};
/* Generic macros for dealing with netlink sockets. Might be duplicated
* elsewhere. It is recommended that commercial grade applications use
* libnl or libnetlink and use the interfaces provided by the library
*/
#define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
#define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
#define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
/* Family string */
#define GEN_FAMILY_STR "PotatoFamily"
#define GEN_ML_GRP_STR "PotatoGroup"
/* SOL_NETLINK is only defined in <kernel src>/include/linux/socket.h
* It is not defined in <kernel src>/include/uapi/linux/socket.h
* Thus, copy the define to here if we don't include kernel header
*/
#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif
/**
* #ingroup attr
* Iterate over a stream of attributes
* #arg pos loop counter, set to current attribute
* #arg head head of attribute stream
* #arg len length of attribute stream
* #arg rem initialized to len, holds bytes currently remaining in stream
*/
#define nla_for_each_attr(pos, head, len, rem) \
for (pos = head, rem = len; \
nla_ok(pos, rem); \
pos = nla_next(pos, &(rem)))
/**
* #ingroup attr
* Iterate over a stream of nested attributes
* #arg pos loop counter, set to current attribute
* #arg nla attribute containing the nested attributes
* #arg rem initialized to len, holds bytes currently remaining in stream
*/
#define nla_for_each_nested(pos, nla, rem) \
for (pos = nla_data(nla), rem = nla_len(nla); \
nla_ok(pos, rem); \
pos = nla_next(pos, &(rem)))
/* Variables used for netlink */
int nl_fd; /* netlink socket's file descriptor */
struct sockaddr_nl nl_address; /* netlink socket address */
int nl_family_id; /* The family ID resolved by the netlink controller for this userspace program */
int nl_rxtx_length; /* Number of bytes sent or received via send() or recv() */
struct nlattr *nl_na; /* pointer to netlink attributes structure within the payload */
struct { /* memory for netlink request and response messages - headers are included */
struct nlmsghdr n;
struct genlmsghdr g;
char buf[256];
} nl_request_msg, nl_response_msg;
/* Base on libnl-3 attr.c */
/**
* Return type of the attribute.
* #arg nla Attribute.
*
* #return Type of attribute.
*/
int nla_type(const struct nlattr *nla)
{
return nla->nla_type & NLA_TYPE_MASK;
}
/**
* Return pointer to the payload section.
* #arg nla Attribute.
*
* #return Pointer to start of payload section.
*/
void *nla_data(const struct nlattr *nla)
{
return (char *) nla + NLA_HDRLEN;
}
/**
* Return length of the payload .
* #arg nla Attribute
*
* #return Length of payload in bytes.
*/
int nla_len(const struct nlattr *nla)
{
return nla->nla_len - NLA_HDRLEN;
}
/**
* Check if the attribute header and payload can be accessed safely.
* #arg nla Attribute of any kind.
* #arg remaining Number of bytes remaining in attribute stream.
*
* Verifies that the header and payload do not exceed the number of
* bytes left in the attribute stream. This function must be called
* before access the attribute header or payload when iterating over
* the attribute stream using nla_next().
*
* #return True if the attribute can be accessed safely, false otherwise.
*/
int nla_ok(const struct nlattr *nla, int remaining)
{
return remaining >= sizeof(*nla) &&
nla->nla_len >= sizeof(*nla) &&
nla->nla_len <= remaining;
}
/**
* Return next attribute in a stream of attributes.
* #arg nla Attribute of any kind.
* #arg remaining Variable to count remaining bytes in stream.
*
* Calculates the offset to the next attribute based on the attribute
* given. The attribute provided is assumed to be accessible, the
* caller is responsible to use nla_ok() beforehand. The offset (length
* of specified attribute including padding) is then subtracted from
* the remaining bytes variable and a pointer to the next attribute is
* returned.
*
* nla_next() can be called as long as remainig is >0.
*
* #return Pointer to next attribute.
*/
struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
{
int totlen = NLA_ALIGN(nla->nla_len);
*remaining -= totlen;
return (struct nlattr *) ((char *) nla + totlen);
}
static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = {
[NLA_U8] = sizeof(uint8_t),
[NLA_U16] = sizeof(uint16_t),
[NLA_U32] = sizeof(uint32_t),
[NLA_U64] = sizeof(uint64_t),
[NLA_STRING] = 1,
[NLA_FLAG] = 0,
};
static int validate_nla(const struct nlattr *nla, int maxtype,
const struct nla_policy *policy)
{
const struct nla_policy *pt;
unsigned int minlen = 0;
int type = nla_type(nla);
if (type < 0 || type > maxtype)
return 0;
pt = &policy[type];
if (pt->type > NLA_TYPE_MAX)
return -1;
if (pt->minlen)
minlen = pt->minlen;
else if (pt->type != NLA_UNSPEC)
minlen = nla_attr_minlen[pt->type];
if (nla_len(nla) < minlen)
return -2;
if (pt->maxlen && nla_len(nla) > pt->maxlen)
return -3;
if (pt->type == NLA_STRING) {
const char *data = nla_data(nla);
if (data[nla_len(nla) - 1] != '\0')
return -4;
}
return 0;
}
/**
* Create attribute index based on a stream of attributes.
* #arg tb Index array to be filled (maxtype+1 elements).
* #arg maxtype Maximum attribute type expected and accepted.
* #arg head Head of attribute stream.
* #arg len Length of attribute stream.
* #arg policy Attribute validation policy.
*
* Iterates over the stream of attributes and stores a pointer to each
* attribute in the index array using the attribute type as index to
* the array. Attribute with a type greater than the maximum type
* specified will be silently ignored in order to maintain backwards
* compatibility. If \a policy is not NULL, the attribute will be
* validated using the specified policy.
*
* #see nla_validate
* #return 0 on success or a negative error code.
*/
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
struct nla_policy *policy)
{
struct nlattr *nla;
int rem, err;
memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
nla_for_each_attr(nla, head, len, rem) {
int type = nla_type(nla);
if (type > maxtype)
continue;
if (policy) {
err = validate_nla(nla, maxtype, policy);
if (err < 0)
goto errout;
}
if (tb[type])
fprintf(stderr, "Attribute of type %#x found multiple times in message, "
"previous attribute is being ignored.\n", type);
tb[type] = nla;
}
if (rem > 0)
fprintf(stderr, "netlink: %d bytes leftover after parsing "
"attributes.\n", rem);
err = 0;
errout:
return err;
}
/**
* Create attribute index based on nested attribute
* #arg tb Index array to be filled (maxtype+1 elements).
* #arg maxtype Maximum attribute type expected and accepted.
* #arg nla Nested Attribute.
* #arg policy Attribute validation policy.
*
* Feeds the stream of attributes nested into the specified attribute
* to nla_parse().
*
* #see nla_parse
* #return 0 on success or a negative error code.
*/
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
struct nla_policy *policy)
{
return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
}
static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
[CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
[CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING,
.maxlen = GENL_NAMSIZ },
[CTRL_ATTR_VERSION] = { .type = NLA_U32 },
[CTRL_ATTR_HDRSIZE] = { .type = NLA_U32 },
[CTRL_ATTR_MAXATTR] = { .type = NLA_U32 },
[CTRL_ATTR_OPS] = { .type = NLA_NESTED },
[CTRL_ATTR_MCAST_GROUPS] = { .type = NLA_NESTED },
};
static struct nla_policy family_grp_policy[CTRL_ATTR_MCAST_GRP_MAX+1] = {
[CTRL_ATTR_MCAST_GRP_NAME] = { .type = NLA_STRING },
[CTRL_ATTR_MCAST_GRP_ID] = { .type = NLA_U32 },
};
int genlctrl_msg_parse(struct nlmsghdr *nlh, int *family_id, char **family_name,
int *mcast_id, char **mcast_name)
{
struct nlattr *tb[CTRL_ATTR_MAX+1];
struct nlattr *nla_hdr;
int nla_length;
int ret = 0;
nla_hdr = (struct nlattr *)((unsigned char *) nlh + NLMSG_HDRLEN + GENL_HDRLEN);
nla_length = nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN;
if(ret = nla_parse(tb, CTRL_ATTR_MAX, nla_hdr, nla_length, ctrl_policy)) {
fprintf(stderr, "nla_parse error! ret = %d\n", ret);
return -1;
}
if (tb[CTRL_ATTR_FAMILY_ID])
*family_id = *(const uint16_t *) nla_data(tb[CTRL_ATTR_FAMILY_ID]);
if (tb[CTRL_ATTR_FAMILY_NAME])
*family_name = (char *) nla_data(tb[CTRL_ATTR_FAMILY_NAME]);
if (tb[CTRL_ATTR_MCAST_GROUPS]) {
struct nlattr *nla, *grp_attr;
int remaining, err;
grp_attr = tb[CTRL_ATTR_MCAST_GROUPS];
nla_for_each_nested(nla, grp_attr, remaining) {
struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
int id = 0;
char *name = NULL;
err = nla_parse_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, nla,
family_grp_policy);
if (err < 0) {
fprintf(stderr, "nla_parse_nested error! err = %d\n", err);
return -1;
}
if (tb[CTRL_ATTR_MCAST_GRP_ID])
id = *(const uint32_t *) nla_data(tb[CTRL_ATTR_MCAST_GRP_ID]);
if (tb[CTRL_ATTR_MCAST_GRP_NAME])
name = (char *) nla_data(tb[CTRL_ATTR_MCAST_GRP_NAME]);
if (id || name) {
*mcast_id = id;
*mcast_name = name;
}
}
}
return 0;
}
void genlmsg_recv(void) {
struct nlmsghdr *nlh;
struct nlattr *tb[__ATTR_MAX];
struct nlattr *nla_hdr;
int nla_length;
int ret = 0;
while(1)
{
memset(&nl_response_msg, 0, sizeof(nl_response_msg));
nl_rxtx_length = recv(nl_fd, &nl_response_msg, sizeof(nl_response_msg), 0);
if (nl_rxtx_length < 0) {
perror("recv()");
goto out;
}
nlh = &nl_response_msg.n;
nla_hdr = (struct nlattr *)((unsigned char *) nlh + NLMSG_HDRLEN + GENL_HDRLEN);
nla_length = nlh->nlmsg_len - GENL_HDRLEN - NLMSG_HDRLEN;
if(ret = nla_parse(tb, __ATTR_MAX-1, nla_hdr, nla_length, NULL)) {
fprintf(stderr, "nla_parse error! ret = %d\n", ret);
goto out;
}
if (tb[1])
printf("ATTR_HELLO: len:%u type:%u data:%s\n", tb[1]->nla_len,
tb[1]->nla_type, (char *)nla_data(tb[1]));
else
printf("ATTR_HELLO: null\n");
if (tb[2])
printf("ATTR_FOO: len:%u type:%u data:%u\n", tb[2]->nla_len,
tb[2]->nla_type, *((__u32 *)nla_data(tb[2])));
else
printf("ATTR_FOO: null\n");
}
out:
return;
}
int main(void) {
struct nlattr *nla1, *nla2;
int len, rem, remaining;
struct nlmsghdr *nlh;
int family_id;
char *family_name;
int mcast_id;
char *mcast_name;
int err;
/* Step 1: Open the socket. Note that protocol = NETLINK_GENERIC */
nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
if (nl_fd < 0) {
perror("socket()");
return -1;
}
/* Step 2: Bind the socket. */
memset(&nl_address, 0, sizeof(nl_address));
nl_address.nl_family = AF_NETLINK;
nl_address.nl_groups = 0;
if (bind(nl_fd, (struct sockaddr *) &nl_address, sizeof(nl_address)) < 0) {
perror("bind()");
goto out;
}
/* Step 3: Resolve the family ID corresponding to the string GEN_FAMILY_STR */
/* Populate the netlink header */
nl_request_msg.n.nlmsg_type = GENL_ID_CTRL;
nl_request_msg.n.nlmsg_flags = NLM_F_REQUEST;
nl_request_msg.n.nlmsg_seq = 0;
nl_request_msg.n.nlmsg_pid = getpid();
nl_request_msg.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
/* Populate the payload's "family header" : which in our case is genlmsghdr */
nl_request_msg.g.cmd = CTRL_CMD_GETFAMILY;
nl_request_msg.g.version = 0x1;
/* Populate the payload's "netlink attributes" */
nl_na = (struct nlattr *) GENLMSG_DATA(&nl_request_msg); /* get location of genl data where to put */
nl_na->nla_type = CTRL_ATTR_FAMILY_NAME;
nl_na->nla_len = strlen(GEN_FAMILY_STR) + 1 + NLA_HDRLEN;
strcpy(NLA_DATA(nl_na), GEN_FAMILY_STR); /* Family name length can be upto 16 chars including \0 */
nl_request_msg.n.nlmsg_len += NLMSG_ALIGN(nl_na->nla_len);
memset(&nl_address, 0, sizeof(nl_address));
nl_address.nl_family = AF_NETLINK;
/* Send the family ID request message to the netlink controller */
nl_rxtx_length = sendto(nl_fd, (char *) &nl_request_msg, nl_request_msg.n.nlmsg_len,
0, (struct sockaddr *) &nl_address, sizeof(nl_address));
if (nl_rxtx_length != nl_request_msg.n.nlmsg_len) {
perror("sendto()");
goto out;
}
/* Wait for the response message */
nl_rxtx_length = recv(nl_fd, &nl_response_msg, sizeof(nl_response_msg), 0);
if (nl_rxtx_length < 0) {
perror("recv()");
goto out;
}
/* Validate response message */
if (!NLMSG_OK((&nl_response_msg.n), nl_rxtx_length)) {
fprintf(stderr, "family ID request : invalid message\n");
goto out;
}
if (nl_response_msg.n.nlmsg_type == NLMSG_ERROR) { /* error */
fprintf(stderr, "family ID request : receive error\n");
goto out;
}
/* Step 4: Extract family ID and mcast group ID*/
nlh = &nl_response_msg.n;
genlctrl_msg_parse(nlh, &family_id, &family_name, &mcast_id, &mcast_name);
printf("[INFO] family_id = %d, family_name = %s\n", family_id, family_name);
printf("[INFO] mcast_id = %d, mcast_name = %s\n", mcast_id, mcast_name);
/* Step 5: Add to mulitcast group */
err = setsockopt(nl_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &mcast_id, sizeof(mcast_id));
if (err < 0) {
perror ("setsockopt()");
goto out;
}
/* Step 6: Receive multicast data */
genlmsg_recv();
/* Step 7: Close the socket and quit */
close(nl_fd);
return 0;
out:
close(nl_fd);
return -1;
}
User Code Makefile
PWD := $(shell pwd)
TARGET := genl_ml
SRC := main.c
HDR_DIR = /usr/include/
LDFLAGS =
all:
gcc $(SRC) $(LDFLAGS) -o $(TARGET)
clean:
rm -fr $(TARGET)

How High the Pin of Parallel Port

/* Necessary includes for drivers */
#include <linux/init.h>
//#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/ioport.h>
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */
#include <asm/io.h> /* inb, outb */
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Nikunj");
/* Function declaration of parlelport.c */
int parlelport_open(struct inode *inode, struct file *filp);
int parlelport_release(struct inode *inode, struct file *filp);
ssize_t parlelport_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t parlelport_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);
void parlelport_exit(void);
int parlelport_init(void);
/* Structure that declares the common */
/* file access fcuntions */
struct file_operations parlelport_fops = {
read : parlelport_read,
write : parlelport_write,
open : parlelport_open,
release : parlelport_release
};
/* Driver global variables */
/* Major number */
int parlelport_major = 61;
/* Control variable for memory */
/* reservation of the parallel port*/
int port;
module_init(parlelport_init);
module_exit(parlelport_exit);
int parlelport_init(void)
{
int result;
/* Registering device */
result = register_chrdev(parlelport_major, "parlelport", &parlelport_fops);
if (result < 0)
{
printk("<1>parlelport: cannot obtain major number %d\n",parlelport_major);
return result;
}
/* Registering port */
port = check_region(0x378, 1);
if (port)
{
printk("<1>parlelport: cannot reserve 0x378\n");
result = port;
goto fail;
}
request_region(0x378, 1, "parlelport");
printk("<1>Inserting parlelport module\n");
return 0;
fail:
parlelport_exit();
return result;
}
void parlelport_exit(void)
{
/* Make major number free! */
unregister_chrdev(parlelport_major, "parlelport");
/* Make port free! */
if (!port)
{
release_region(0x378,1);
}
printk("<1>Removing parlelport module\n");
}
int parlelport_open(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
int parlelport_release(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
ssize_t parlelport_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
/* Buffer to read the device */
char parlelport_buffer;
/* Reading port */
parlelport_buffer = inb(0x378);
/* We transfer data to user space */
copy_to_user(buf,&parlelport_buffer,1);
/* We change the reading position as best suits */
if (*f_pos == 0)
{
*f_pos+=1;
return 1;
}
else
{
return 0;
}
}
ssize_t parlelport_write( struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
char *tmp;
/* Buffer writing to the device */
char parlelport_buffer;
tmp=buf+count-1;
copy_from_user(&parlelport_buffer,tmp,1);
/* Writing to the port */
outb(parlelport_buffer,0x378);
return 1;
}
this is the code of parallel port device driver and this is my first c code for that.
Please help me to solve the below problem
i have successfully compile the code and create the .ko file successfully and successfully load in the ubuntu 9.10 OS but thee is no high or low the pin so please help me
How you are checking answer?? After installed module do
dmesg
Actually you can not acquire region at 0x378. Because by default system allocate this one to parport0.
Do cat /proc/ioports so you will find that at 0x378 there is parport0.
You can directly write on that address (0x378), without using that checking and requesting.
My suggestion is that don't directly go in complex form. In write function just write
outb(0x378,1) and check (by inserting hardware having LEDs) LED on/off on data pins.