I'm working on a rpc sample program on linux. When I try to compile my remote procedure I get this error :
msg_proc.c:10:7: error: conflicting types for ‘printmessage_1’
In file included from msg_proc.c:8:0:
msg.h:22:15: note: previous declaration of ‘printmessage_1’ was here
This is the command I used to complie :
cc msg_proc.c msg_svc.c -o msg_server -lnsl
And these are my header and procedure files :
/*msg.h
*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#ifndef _MSG_H_RPCGEN
#define _MSG_H_RPCGEN
#include <rpc/rpc.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MESSAGEPROG 0x20000001
#define PRINTMESSAGEVERS 1
#if defined(__STDC__) || defined(__cplusplus)
#define PRINTMESSAGE 1
extern int * printmessage_1(char **, CLIENT *);
extern int * printmessage_1_svc(char **, struct svc_req *);
extern int messageprog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);
#else /* K&R C */
#define PRINTMESSAGE 1
extern int * printmessage_1();
extern int * printmessage_1_svc();
extern int messageprog_1_freeresult ();
#endif /* K&R C */
#ifdef __cplusplus
}
#endif
#endif /* !_MSG_H_RPCGEN */
/*
* msg_proc.c: implementation of the
* remote procedure "printmessage"
*/
#include <stdio.h>
#include <rpc/rpc.h>
#include "msg.h"
int * printmessage_1(char **msg, struct svc_req *req) {
static int result; /* must be static! */
FILE *f;
f = fopen("/dev/console", "w");
if (f == (FILE *) NULL) {
result = 0;
return (&result);
}
fprintf(f, "%s\n", *msg);
fclose(f);
result = 1;
return (&result);
}
What's wrong with my code ?
The argument types in your printmessage_1 function match the declaration of printmessage_1_svc, not printmessage_1. – Barmar
Related
Good morning.
I have a problem - I created definition of the myOwnType and I still have a problem with compile. Please help. C language (not C++).
main.c:
#include <stdio.h>
#include "additional.h"
#include "config.h"
int main(int argc, char *argv[])
{
return 0;
}
config.h:
#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_
#include "additional.h"
typedef struct MyType
{
int x;
} MyOwnType_t;
#endif /* SRC_CONFIG_H_ */
additional.c:
#include "config.h"
void foo(MyOwnType_t x)
{
}
additional.h:
#ifndef SRC_ADDITIONAL_H_
#define SRC_ADDITIONAL_H_
#include "config.h"
extern void foo(MyOwnType_t x);
#endif /* SRC_ADDITIONAL_H_ */
Compiler generate error:
..\Src\additional.h:13:10: error: unknown type name 'MyOwnType_t'
Help me please.
Expanding your includes in additional.c gets you:
#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_
#ifndef SRC_ADDITIONAL_H_
#define SRC_ADDITIONAL_H_
/* no effect #include "config.h" */
#ifndef SRC_CONFIG_H_
/* not seen because SRC_CONFIG_H_ is already defined here, from above line 2ish */
#endif /* SRC_CONFIG_H_ */
extern void foo(MyOwnType_t x);
#endif /* SRC_ADDITIONAL_H_ */
typedef struct MyType
{
int x;
} MyOwnType_t;
#endif /* SRC_CONFIG_H_ */
void foo(MyOwnType_t x)
{
}
"no effect" refers to the fact that the second include of config.h meets an already defined reinclusion guard, the content is not seen in this place. The content IS seen from the first include, but later, AFTER the content of additional.h which would need the content from config.h.
Cleaning that up gets you:
/* no effect #include "config.h" */
extern void foo(MyOwnType_t x);
typedef struct MyType
{
int x;
} MyOwnType_t;
void foo(MyOwnType_t x)
{
}
In the first line, extern void foo(MyOwnType_t x); you use a not yet known type, MyOwnType_t.
The compiler does not like that.
I'm developing a library and I would like to know some data about the caller of one of the functions I'm offering. In particular, I would need to know the file name, function name and line where my function (a redefined malloc) is being called.
EDIT: Here's a minimum working example where I can detect when a user calls malloc and "redirect" him to my own malloc function:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include "myLib.h"
int main(){
printf("Inside main, asking for memory\n");
int *p = malloc(sizeof(int));
*p = 3;
free(p);
return 0;
}
myLib.c:
#include "myLib.h"
void * myAlloc (size_t size){
void * p = NULL;
fprintf(stderr, "Inside my own malloc\n");
p = (malloc)(size);
return p;
}
#undef malloc
#define malloc(size) myAlloc(size)
myLib.h:
#ifndef MYLIB_H
#define MYLIB_H
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define malloc(size) myAlloc(size)
void * myAlloc(size_t size);
#endif
I've tried using _FILE_ _func_ and _LINE_ keywords, but I can't make it work since it's in a different module.
You could:
//mylib.h
#ifndef MYLIB_H
#define MYLIB_H
#include <stdlib.h>
// replace malloc in case it's already a macro
#ifdef malloc
#undef malloc
#endif
// I believe that from the standards point of view, this is undefined behavior
#define malloc(size) my_alloc(size, __FILE__, __LINE__, __func__)
#ifdef __GNUC__
// Allow compiler to do static checking.
__attribute__((__alloc_size__(1), __malloc__))
#endif
void *my_alloc(size_t size, const char *file, int line, const char *func);
// ^^^^^^^^ I do not like camelCase case - one snake case to rule them all.
#endif
// mylib.c
#include "mylib.h" // do not ever mix uppercase and lowercase in filenames
#undef malloc // undef malloc so we don't call ourselves recursively
#include <stdio.h>
void *my_alloc(size_t size, const char *file, int line, const char *func){
fprintf(stderr, "Och my god, you wouldn't believe it!\n"
"A function %s in file %s at line %d called malloc!\n",
func, file, line);
return malloc(size);
}
You might also see how assert does it. If you are aiming at glibc, read glibc docs replacing malloc.
Still as you discovered a user may do (malloc)(size) cicumvent macro expansion. You could do:
void *my_alloc(size_t size, const char *file, int line, const char *func);
static inline void *MY_ALLOC(size_t size) {
return my_alloc(size, NULL, 0, NULL);
}
#define MY_ALLOC(size) my_alloc(size, __FILE__, __LINE__, __func__)
// if called with `malloc()` then MY_ALLOC is expanded
// if called as `(malloc)`, then just expands to MY_ALLOC.
#define malloc MY_ALLOC
int main() {
malloc(10); // calls my_alloc(10, "main.c", 62, "main");
(malloc)(20); // calls my_alloc(20, NULL, 0, NULL);
}
GLIBC defines hidden symbols for malloc(), free()... which are called __libc_malloc(), __libc_free()...
So, you can tremendously simplify your debug macros.
In m.h, just define the following:
#if DEBUG_LEVEL > 0
extern void *__libc_malloc (size_t bytes);
extern void *myMalloc(size_t size, const char *filename, const char *funcname, int line);
#define malloc(size) myMalloc(size, __FILE__, __FUNCTION__, __LINE__)
#endif
Then you can write a program defining myMalloc() as follow (e.g. file name is m.c):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "m.h"
#if DEBUG_LEVEL > 0
void *myMalloc(
size_t size,
const char *filename,
const char *funcname,
int line
) {
fprintf(stderr, "malloc(%zu) called from %s/%s()#%d\n", size, filename, funcname, line);
return __libc_malloc(size);
}
#endif
char *dup_str(char *string) {
char *str = malloc(strlen(string) + 1);
strcpy(str, string);
return str;
}
int main(int ac, char *av[]) {
char *str;
if (av[1]) {
str = dup_str(av[1]);
} else {
str = dup_str("NULL");
}
printf("String = '%s'\n", str);
free(str);
return 0;
}
When you compile this example program in non debug mode:
$ gcc m.c -DDEBUG_LEVEL=0
$ ./a.out azerty
String = 'azerty'
When you compile your program in debug mode:
$ gcc m.c -DDEBUG_LEVEL=1
$ ./a.out azerty
malloc(7) called from m.c/dup_str()#27
String = 'azerty'
I wrote a c-code designed for linux platform.
Now, I want to make it cross-platform so to use in Windows as-well.
In my code, I dlopen an so file and utilize the functions inside it.
Below is how my code looks like. But I just found out that in windows, the way to load and use dynamic library is quite different.
void *mydynlib
mydynlib= dlopen("/libpath/dynlib.so",RTLD_LAZY);
void (*dynfunc1)() = dlsym(mydynlib,"dynfunc1");
void (*dynfunc2)(char*, char*, double) = dlsym(mydynlib,"dynfunc2");
int (*dynfunc3)() = dlsym(mydynlib,"dynfunc3");
From what I found, I need to use LoadLibrary & GetProcAddress instead of dlopen & dlsym. However, I do not know how to convert above line for windows using those. I've tried to search some examples for hours but couldn't find exact solution. If someone had this kind of experience, please give me a tip.
Excuse me if this is too obvious problem. I'm quite new to C. I usually write my program in python.
Once in my youth I created something like this:
/* dlfcn.h */
#ifndef DLFCN_H
#define DLFCN_H
#define RTLD_GLOBAL 0x100 /* do not hide entries in this module */
#define RTLD_LOCAL 0x000 /* hide entries in this module */
#define RTLD_LAZY 0x000 /* accept unresolved externs */
#define RTLD_NOW 0x001 /* abort if module has unresolved externs */
/*
How to call in Windows:
void *h = dlopen ("path\\library.dll", flags)
void (*fun)() = dlsym (h, "entry")
*/
#ifdef __cplusplus
extern "C" {
#endif
void *dlopen (const char *filename, int flag);
int dlclose (void *handle);
void *dlsym (void *handle, const char *name);
const char *dlerror (void);
#ifdef __cplusplus
}
#endif
#endif
and dlfcn.c:
/* dlfcn.c */
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
static struct {
long lasterror;
const char *err_rutin;
} var = {
0,
NULL
};
void *dlopen (const char *filename, int flags)
{
HINSTANCE hInst;
hInst= LoadLibrary (filename);
if (hInst==NULL) {
var.lasterror = GetLastError ();
var.err_rutin = "dlopen";
}
return hInst;
}
int dlclose (void *handle)
{
BOOL ok;
int rc= 0;
ok= FreeLibrary ((HINSTANCE)handle);
if (! ok) {
var.lasterror = GetLastError ();
var.err_rutin = "dlclose";
rc= -1;
}
return rc;
}
void *dlsym (void *handle, const char *name)
{
FARPROC fp;
fp= GetProcAddress ((HINSTANCE)handle, name);
if (!fp) {
var.lasterror = GetLastError ();
var.err_rutin = "dlsym";
}
return (void *)(intptr_t)fp;
}
const char *dlerror (void)
{
static char errstr [88];
if (var.lasterror) {
sprintf (errstr, "%s error #%ld", var.err_rutin, var.lasterror);
return errstr;
} else {
return NULL;
}
}
You could use a set of macros that change depending on the OS you're on:
#ifdef __linux__
#define LIBTYPE void*
#define OPENLIB(libname) dlopen((libname), RTLD_LAZY)
#define LIBFUNC(lib, fn) dlsym((lib), (fn))
#elif defined(WINVER)
#define LIBTYPE HINSTANCE
#define OPENLIB(libname) LoadLibraryW(L ## libname)
#define LIBFUNC(lib, fn) GetProcAddress((lib), (fn))
#endif
I have create, with rpcgen, these two file (.h and .c) with my own two structs (the environment is Linux and the programming language is C).
The two structs are these below (this is the .h file):
/*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#ifndef _XDRTYPES_H_RPCGEN
#define _XDRTYPES_H_RPCGEN
#include <rpc/rpc.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Request {
struct {
u_int data_len;
float *data_val;
} data;
bool_t last;
};
typedef struct Request Request;
struct Response {
bool_t error;
float result;
};
typedef struct Response Response;
/* the xdr functions */
#if defined(__STDC__) || defined(__cplusplus)
extern bool_t xdr_Request (XDR *, Request*);
extern bool_t xdr_Response (XDR *, Response*);
#else /* K&R C */
extern bool_t xdr_Request ();
extern bool_t xdr_Response ();
#endif /* K&R C */
#ifdef __cplusplus
}
#endif
#endif /* !_XDRTYPES_H_RPCGEN */
In my main I call #include <rpc/rpc.h> and #include "xdrtypes.h".Then I want send a Request message to the server with a socket (s is the file descriptor s = socket(...)):
stream_socket_w = fdopen(s, "w");
xdrstdio_create(&xdrs_w, stream_socket_w, XDR_ENCODE);
x.data.data_len = 5;
x.last = 1;
x.data.data_val[0] = 5.3;
x.data.data_val[1] = 2.7;
x.data.data_val[2] = 8.4;
x.data.data_val[3] = 2.9;
x.data.data_val[4] = 1.7;
xdr_Request(&xdrs_w, &x);
fflush(stream_socket_w);
The problem is that the execution stop at the line xdr_Request(&xdrs_w, &x) and I obtain "Segmentation Fault". Everybody Can Help me please? Thank you very much.
Are you allocating memory for data_val?
x.data.data_val = malloc(x.data.data_len * sizeof(float));
Where xdr_Request function is defined? do you have its API?
I have the following .x file called paper.x . When I create a server in rpc in order to call the function the error below occurs
paperserverproc.c:23:5: error: conflicting types for ‘add_procedure_1_svc’
paper.h:46:15: note: previous declaration of ‘add_procedure_1_svc’ was here
#include <limits.h>
struct paper_saved{
char author_name[CHAR_MAX];
char paper_title[CHAR_MAX];
int paper_id;
char paper_file_name[CHAR_MAX];
char paper_content[CHAR_MAX];
};
struct paper_info_saved{
char author_name[CHAR_MAX];
char paper_title[CHAR_MAX];
int paper_id;
char paper_file_name[CHAR_MAX];
};
struct list_papers{
paper_saved paper;
struct list_papers *next;
};
program PAPER_PROGRAM
{
version PAPER_VERSION
{
int ADD_PROCEDURE(paper_saved) = 1; /* Procedure nb */
void LIST_PROCEDURE(void)=2;
paper_info_saved INFO_PROCEDURE(int)=3;
paper_saved FETCH_PROCEDURE(int)=4;
void REMOVE_PROCEDURE(int)=5;
} = 1; /* Version nb */
} = 0x20001234; /* Program number */
I call the procedure with this way and the line 23 is the line when I declare the function as below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "paper.h"
#include <ctype.h>
#include <limits.h>
.......
int add_procedure_1_svc(paper_saved *paper_pointer, struct svc_req *rqstp)
This is the paper.h file generated by rpcgen and I dont know what's the problem in line 46 that it mentions
/*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#ifndef _PAPER_H_RPCGEN
#define _PAPER_H_RPCGEN
#include <rpc/rpc.h>
#ifdef __cplusplus
extern "C" {
#endif
struct paper_saved {
char author_name[127];
char paper_title[127];
int paper_id;
char paper_file_name[127];
char paper_content[127];
};
typedef struct paper_saved paper_saved;
struct paper_info_saved {
char author_name[127];
char paper_title[127];
int paper_id;
char paper_file_name[127];
};
typedef struct paper_info_saved paper_info_saved;
struct list_papers {
paper_saved paper;
struct list_papers *next;
};
typedef struct list_papers list_papers;
#define PAPER_PROGRAM 0x20001234
#define PAPER_VERSION 1
#if defined(__STDC__) || defined(__cplusplus)
#define ADD_PROCEDURE 1
extern int * add_procedure_1(paper_saved *, CLIENT *);
extern int * add_procedure_1_svc(paper_saved *, struct svc_req *);
#define LIST_PROCEDURE 2
extern void * list_procedure_1(void *, CLIENT *);
extern void * list_procedure_1_svc(void *, struct svc_req *);
#define INFO_PROCEDURE 3
extern paper_info_saved * info_procedure_1(int *, CLIENT *);
extern paper_info_saved * info_procedure_1_svc(int *, struct svc_req *);
#define FETCH_PROCEDURE 4
extern paper_saved * fetch_procedure_1(int *, CLIENT *);
extern paper_saved * fetch_procedure_1_svc(int *, struct svc_req *);
#define REMOVE_PROCEDURE 5
extern void * remove_procedure_1(int *, CLIENT *);
extern void * remove_procedure_1_svc(int *, struct svc_req *);
extern int paper_program_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);
#else /* K&R C */
#define ADD_PROCEDURE 1
extern int * add_procedure_1();
extern int * add_procedure_1_svc();
#define LIST_PROCEDURE 2
extern void * list_procedure_1();
extern void * list_procedure_1_svc();
#define INFO_PROCEDURE 3
extern paper_info_saved * info_procedure_1();
extern paper_info_saved * info_procedure_1_svc();
#define FETCH_PROCEDURE 4
extern paper_saved * fetch_procedure_1();
extern paper_saved * fetch_procedure_1_svc();
#define REMOVE_PROCEDURE 5
extern void * remove_procedure_1();
extern void * remove_procedure_1_svc();
extern int paper_program_1_freeresult ();
#endif /* K&R C */
/* the xdr functions */
#if defined(__STDC__) || defined(__cplusplus)
extern bool_t xdr_paper_saved (XDR *, paper_saved*);
extern bool_t xdr_paper_info_saved (XDR *, paper_info_saved*);
extern bool_t xdr_list_papers (XDR *, list_papers*);
#else /* K&R C */
extern bool_t xdr_paper_saved ();
extern bool_t xdr_paper_info_saved ();
extern bool_t xdr_list_papers ();
#endif /* K&R C */
#ifdef __cplusplus
}
#endif
#endif /* !_PAPER_H_RPCGEN */
So the correct one .h file is
#include <limits.h>
struct paper_saved{
char author_name[CHAR_MAX];
char paper_title[CHAR_MAX];
int paper_id;
char paper_file_name[CHAR_MAX];
char paper_content[CHAR_MAX];
};
struct paper_info_saved{
char author_name[CHAR_MAX];
char paper_title[CHAR_MAX];
int paper_id;
char paper_file_name[CHAR_MAX];
};
struct list_papers{
paper_saved paper;
struct list_papers *next;
};
typedef int p_id;
program PAPER_PROGRAM
{
version PAPER_VERSION
{
p_id ADD_PROCEDURE(paper_saved) = 1; /* Procedure nb */
void LIST_PROCEDURE(void)=2;
paper_info_saved INFO_PROCEDURE(int)=3;
paper_saved FETCH_PROCEDURE(int)=4;
void REMOVE_PROCEDURE(int)=5;
} = 1; /* Version nb */
} = 0x20001234; /* Program number */
The function is declared as returning int:
int add_procedure_1_svc(paper_saved *paper_pointer, struct svc_req *rqstp)
but the extern as returning int*
extern int * add_procedure_1_svc(paper_saved *, struct svc_req *);
one of them must be wrong.
What does line 46 of file paper.h have? . Check the paper.h file on the line 46 to see if the function signature is same.Is it auto generated by rpcgen ?