I want to pass a string > 1024 chars to my module (filesystem).
As kernel parameters are limited to 1024 chars, someone recommended to use sysfs instead.
I tried to include this example in my super.c class to create a string 'filename' & string 'code' entry in sysfs for my module.
static decl_subsys(myfs, NULL, NULL);
struct myfs_attr {
struct attribute attr;
char *value;
};
static struct myfs_attr fname = {
.attr.name="filename",
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value = "/my/test/path",
};
static struct myfs_attr code = {
.attr.name="code",
.attr.owner = THIS_MODULE,
.attr.mode = 0644,
.value = "0101",
};
When compiling my module I get a lot of errors (line 41 is decl_subsys):
fs/myfs/super.c:41:26: error: expected ‘)’ before ‘(’ token
fs/myfs/super.c:50:2: error: unknown field ‘owner’ specified in initializer
fs/myfs/super.c:50:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:50:2: warning: (near initialization for ‘fname.attr.name’) [enabled by default]
...
fs/myfs/super.c: At top level:
fs/myfs/super.c:83:15: error: variable ‘myfsops’ has initializer but incomplete type
fs/myfs/super.c:84:2: error: unknown field ‘show’ specified in initializer
fs/myfs/super.c:84:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:84:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:85:2: error: unknown field ‘store’ specified in initializer
fs/myfs/super.c:85:2: warning: excess elements in struct initializer [enabled by default]
fs/myfs/super.c:85:2: warning: (near initialization for ‘myfsops’) [enabled by default]
fs/myfs/super.c:89:2: error: unknown field ‘myfs_ops’ specified in initializer
fs/myfs/super.c:89:2: warning: initialization from incompatible pointer type [enabled by default]
fs/myfs/super.c:89:2: warning: (near initialization for ‘myfstype.release’) [enabled by default]
fs/myfs/super.c: In function ‘init_myfs_fs’:
fs/myfs/super.c:1554:2: error: implicit declaration of function ‘kobj_set_kset_s’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1554:19: error: ‘myfs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1554:19: note: each undeclared identifier is reported only once for each function it appears in
fs/myfs/super.c:1554:32: error: ‘fs_subsys’ undeclared (first use in this function)
fs/myfs/super.c:1557:2: error: implicit declaration of function ‘subsystem_register’ [-Werror=implicit-function-declaration]
fs/myfs/super.c: In function ‘exit_myfs_fs’:
fs/myfs/super.c:1579:2: error: implicit declaration of function ‘subsystem_unregister’ [-Werror=implicit-function-declaration]
fs/myfs/super.c:1579:24: error: ‘myfs_subsys’ undeclared (first use in this function)
Is this tutorial just outdated for my 3.5 kernel or am I missing something else?
How could I create 2 char string entries for my module in sysfs?
Here is the source code for transmitting data to a "param_buf" string. As requested, without read method. Only store.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <asm/string.h>
static struct kobject *register_kobj;
static char *param_buf;
// function for many symbol data enter
static ssize_t __used store_value(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){
printk(KERN_ALERT "you entered %s\n", buf);
strncpy(param_buf, buf, PAGE_SIZE - 1);
return count;
}
// register function to attribute
static struct kobj_attribute store_val_attribute = __ATTR( put_parameters, 0220, NULL, store_value);
// put attribute to attribute group
static struct attribute *register_attrs[] = {
&store_val_attribute.attr,
NULL, /* NULL terminate the list*/
};
static struct attribute_group reg_attr_group = {
.attrs = register_attrs
};
static int hello_init(void){
param_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
// create sysfs object ( /sys/kernel/test_1025_sym directory )
register_kobj = kobject_create_and_add("test_1025_sym", kernel_kobj);
if (!register_kobj)
return -ENOMEM;
//create attributes (files)
if(sysfs_create_group(register_kobj, ®_attr_group)){
kobject_put(register_kobj);
return -ENOMEM;
}
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "last value was %s\n", param_buf);
kfree(param_buf);
kobject_put(register_kobj);
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(hello_init);
module_exit(hello_exit);
You can test it in such a way:
cat /etc/fstab > /sys/kernel/test_1025_sym/put_parameters
For two string entries: copy a store_value function, register one more store_val_attribute and put it to attributes list.
Related
I am trying to use some old devices called sensinodes . There are a couple of pieces of software needed for this, contiki, sdcc and the some nano_usb_programmer
I'm working from these instruction and I can't seem to make the nano_usb_programmer on ubuntu 16.04 LTS as this pretty old now and things have moved on.
https://github.com/avian2/contiki
When making the nano element I get the error *Edited
In file included from main.c:17:0:
opts.h:25:2: error: redefinition of typedef ‘mode_t’ with different type
}mode_t;
^
In file included from /usr/include/stdlib.h:314:0,
from main.c:10:
/usr/include/x86_64-linux-gnu/sys/types.h:70:18: note: previous declaration of ‘mode_t’ was here
typedef __mode_t mode_t;
^
main.c: In function ‘main’:
main.c:57:6: warning: implicit declaration of function ‘opts_parse’ [-Wimplicit-function-declaration]
if (opts_parse(argc, argv) < 0)
^
main.c:77:4: warning: implicit declaration of function ‘prog_scan’ [-Wimplicit-function-declaration]
prog_scan();
^
main.c:178:7: warning: implicit declaration of function ‘hexfile_out’ [-Wimplicit-function-declaration]
hexfile_out(line_data, 0x04, 0, ptr, 2);
^
main.c:220:15: warning: implicit declaration of function ‘hexfile_build_tables’ [-Wimplicit-function-declaration]
if((rval = hexfile_build_tables(opts.filename, page_buffer, page_table)) == -1)
^
main.c:231:4: warning: implicit declaration of function ‘hexfile_program’ [-Wimplicit-function-declaration]
hexfile_program(page_buffer, page_table);
^
Makefile:25: recipe for target 'main.o' failed
make: *** [main.o] Error 1
It is pointing to line 25 the following file, my c knowledge just isn't any where near good enough to fix this. Any help would be great.
/*
* opts.h
*
* Created on: Dec 21, 2009
* Author: martti
*/
#ifndef OPTS_H_
#define OPTS_H_
#include "cdi.h"
typedef enum
{
USAGE,
VERSION,
SCAN,
WRITE,
WRITE_FAST,
READ,
WRITE_MAC,
READ_MAC,
LOCK,
ERASE
}mode_t;
typedef struct opts_t
{
int port;
chip_t chip;
mode_t mode;
uint8_t write_mac[8];
ramcode_t code_in_ram;
char *filename;
}opts_t;
extern opts_t opts;
#endif /* OPTS_H_ */
i have next simple code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "hiredis.h"
#include "async.h"
#include "adapters/libevent.h"
void onMessage(redisAsyncContext *c, void *reply, void *privdata) {
redisReply *r = reply;
if (reply == NULL) return;
int j;
if (r->type == REDIS_REPLY_ARRAY) {
for (j = 0; j < r->elements; j++) {
printf("%u) %s\n", j, r->element[j]->str);
}
}*/
}
int main (int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
struct event_base *base = event_base_new();
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c->err) {
printf("error: %s\n", c->errstr);
return 1;
}
redisLibeventAttach(c, base);
redisAsyncCommand(c, onMessage, NULL, "SUBSCRIBE messages");
event_base_dispatch(base);
return 0;
}
program compiles and runs well. but it returns error "Segmentation fault" when executes. if i comment redisAsyncCommand() function error disappears.
where is the error in my code?
added:
when i compile my code i have warnings:
make all
'Building file: ../src/redis.c'
'Invoking: Cross GCC Compiler'
arm-linux-gnueabihf-gcc -I"C:\SysGCC\Raspberry\include" -I"C:\SysGCC\Raspberry\include\hiredis" -I"C:\SysGCC\Raspberry\include\event2" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/redis.d" -MT"src/redis.o" -o "src/redis.o" "../src/redis.c"
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventAddRead':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:56:15: warning: passing argument 1 of 'event_add' from incompatible pointer type
event_add(&e->rev,NULL);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:937:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_add(struct event *ev, const struct timeval *timeout);
^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventDelRead':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:61:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
event_del(&e->rev);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_del(struct event *);
^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventAddWrite':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:66:15: warning: passing argument 1 of 'event_add' from incompatible pointer type
event_add(&e->wev,NULL);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:937:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_add(struct event *ev, const struct timeval *timeout);
^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventDelWrite':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:71:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
event_del(&e->wev);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_del(struct event *);
^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventCleanup':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:76:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
event_del(&e->rev);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_del(struct event *);
^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:77:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
event_del(&e->wev);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_del(struct event *);
^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventAttach':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:102:5: warning: implicit declaration of function 'event_set' [-Wimplicit-function-declaration]
event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);
^
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:104:25: warning: passing argument 2 of 'event_base_set' from incompatible pointer type
event_base_set(base,&e->rev);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:627:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_base_set(struct event_base *, struct event *);
^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:105:25: warning: passing argument 2 of 'event_base_set' from incompatible pointer type
event_base_set(base,&e->wev);
^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:627:5: note: expected 'struct event *' but argument is of type 'struct event **'
int event_base_set(struct event_base *, struct event *);
^
'Finished building: ../src/redis.c'
' '
'Building target: redis'
'Invoking: Cross GCC Linker'
arm-linux-gnueabihf-gcc -L"C:\SysGCC\Raspberry\arm-linux-gnueabihf\sysroot\usr\local\lib" -L"C:\SysGCC\Raspberry\arm-linux-gnueabihf\lib" -o "redis" ./src/redis.o -lhiredis -levent_core -lpthread
'Finished building target: redis'
' '
00:20:55 Build Finished (took 1s.130ms)
EDIT
It seems the main issue was in the included adapter file.
The issue I pointed towards was unrelated.
Here is just one quick issue I noticed:
if (r->type == REDIS_REPLY_ARRAY) {
for (j = 0; j < r->elements; j++) {
printf("%u) %s\n", j, r->element[j]->str);
}
}
Your code assumes that the reply is an array of Strings.
This is incorrect.
The Subscribe command also returns an Array response and the last member of that response is a NUMBER (not a string).
Hence, the assumption that it's an array of strings will fail.
I'm not sure if that's the cause, but it is likely to be an important issue.
Good Luck!
P.S.
I might be shamelessly promoting my own project here, but you might enjoy using facil.io's integrated Redis Pub/Sub engine. If you need Websocket or HTTP support, it might save you a lot of coding.
I have this function:
void simul(char *new)
{
segment_table seg = malloc(sizeof(st));
segcode newcode = malloc(sizeof(sc));
int clen = 0;
seg -> segname = new;
clen = code_length(seg -> segname);
seg -> code = read_hexcode(seg -> segname, newcode, clen);
load_image(seg, clen-3);
if (strstr(new, "paper_boot-strap loader.hexcode") == NULL)
run(segment_offset(seg));
}
When I try to compile my program I get the following errors:
error: ‘st’ undeclared (first use in this function)
error: ‘sc’ undeclared (first use in this function)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
What is it that I am doing wrong?
Header file, for example header.h:
// First an include guard (https://en.wikipedia.org/wiki/Include_guard)
#ifndef HEADER_
#define HEADER_
// Define the structures and their type-aliases
typedef struct st { ... } st;
typedef struct sc { ... } sc;
// Declare a couple of other type-aliases
// (Note that I personally recommend against declaring pointer type-aliases)
// (I personally recommend against using type-aliases for simple types
// like the above structures too)
typedef st *segment_table;
typedef sc *segcode;
// Declare a function prototype
void simul(char *new);
#endif // HEADER_
One source file
#include "header.h"
void simul(char *new)
{
...
}
Second source file
#include "header.h"
int main(void)
{
sumul("some string");
}
I am trying to upload a solution to an OJ ,the judge uses GCC
I have received the following errors and having no idea about them.
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token
void insert(int in){
^
In function ‘main’:
error: ‘struct mymultiset_int’ has no member named ‘insert’
x.insert(t);
^
error: ‘struct mymultiset_int’ has no member named ‘getmax’
printf("%d\n",x.getmax());
^
error: ‘struct mymultiset_int’ has no member named ‘_delete’
x._delete(0);
^
My code looks like this:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int t;
#define swap(a,b) t=b,b=a,a=t
/*
when using swap, I use format like swap(x,y); or swap(x,y),
*/
struct mymultiset_int{
int e[100000],end;
void insert(int in){...}
int getmax(){ return e[0]; }
void _delete(int i){...}
}x;
int main(){
x.end=0;memset(x.e,0,sizeof(x.e));
int N,t;scanf("%d",&N);
char i[2];
while (N--){
scanf("%s",i);
if (i[0]=='A'){
scanf("%d",&t);
x.insert(t);
}
else{
printf("%d\n",x.getmax());
x._delete(0);
}
}
}
you cannot define function in structure in C.
But it is possible in c++.
I need a little script that appends "\n exit" to the end of all files in ~/ae23054/ my code is:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
const char *path_dir ="~ae23054/Giuseppe";//Inserire la directory qui
dp = opendir (path_dir);
if (dp != NULL)
{
while (ep = readdir (dp)){
printf(ep->d_name);
char nome_file[256]=ep->d_name;
FILE *fd=fopen(nome_file, a+);
fprint(fd,"\nEXIT");
fclose(fd);
}
(void) closedir (dp);
}
else
perror ("Non posso aprire la directory");
return -1;
}
But i have this error when i compile it: gcc test.c
esempio_giuseppe.c: In function ‘main’:
esempio_giuseppe.c:16: error: invalid initializer
esempio_giuseppe.c:18: error: ‘a’ undeclared (first use in this function)
esempio_giuseppe.c:18: error: (Each undeclared identifier is reported only once
esempio_giuseppe.c:18: error: for each function it appears in.)
esempio_giuseppe.c:18: error: expected expression before ‘)’ token
Thanks for help
i have this error with :
char nome_file[256];
strcpy(nome_file, ep->d_name);
ae23054#el088soh:/home/risorse/ae23054/Giuseppe> gcc esempio_giuseppe.c
esempio_giuseppe.c: In function ‘main’:
esempio_giuseppe.c:18: warning: incompatible implicit declaration of built-in function ‘strcpy’
esempio_giuseppe.c:20: error: ‘a’ undeclared (first use in this function)
esempio_giuseppe.c:20: error: (Each undeclared identifier is reported only once
esempio_giuseppe.c:20: error: for each function it appears in.)
esempio_giuseppe.c:20: error: expected expression before ‘)’ token
You need to make the second argument to fopen a string as it requires a const char*
FILE *fd=fopen(nome_file, "a+");
^ ^
^ ^
instead of
FILE *fd=fopen(nome_file, a+);
See also #AlterMann answer regarding your file name buffer.
Problem is with char nome_file[256]=ep->d_name;
You need to use strcpy() to copy strings like, strcpy(nome_file,ep->d_name); and also change FILE *fd=fopen(nome_file, a+); to FILE *fd=fopen(nome_file, "a+");