Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

Does anyone know why I'm getting a segmentation fault after the returning value of a RPC function?

$
0
0

I have a RPC program which implements two method:

   1): int * delete_occurrences_1(char **, CLIENT *);   2): ListDir * subdirectory_list_1(char **, CLIENT *);

with ListDir defined as:

#include <linux/limits.h>#define N 6struct DirectoryName{    char nameD[PATH_MAX]; //PATH_MAX=4096};struct ListDir{    DirectoryName directories[N];    int cont;};

in the client, when the function subdirectory_list_1 is called i get segmentation fault in the same line, even though the server side sends it correctly or at least set the structure with the correct value.

Here's the code of the client RPC (line 65 gives me seg fault):

#include "RPC_xFile.h"#include <rpc/rpc.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#define BUFF 256int main(int argc,char ** argv){    if(argc!=2){        printf("Name Program , ipAddress");        exit(EXIT_FAILURE);    }    CLIENT * cl;    char * host;    int * result;    ListDir * out =(ListDir *) malloc (sizeof(ListDir));    if (out == NULL) {        perror("Errore durante l'allocazione di memoria per ListDir");        exit(EXIT_FAILURE);    }    out->cont=0;    char choiche[BUFF];    host = argv[1];    char * path = (char*) malloc(PATH_MAX);    cl = clnt_create(host, RPC_XFILE, RPC_XFILEVERS, "udp"); //fondamentale FATTOREX E FATTOREXVERS STESSI DEL FILE.X    if (cl == NULL) {        clnt_pcreateerror(host);        exit(1);    }    char * pos;    printf("Avviato servizio RPC\n");    printf("Inserire:\n");    printf("E - per eliminare le occorrenze numeriche da un file di testo specificato presente lato server\n");    printf("L - per avere la lista dei sottodirettori di primo di livello contenenti almeno 5 file di testo dato un direttorio\n");    printf("ctrl^D per terminare\n");    while(fgets(choice,sizeof(choice),stdin)!=NULL){        if ((pos=strchr(choice, '\n')) != NULL)            *pos = '\0';        switch(choice[0]){            case 'E':                    printf("Dimmi il nome del file da esaminare\n");                    fgets(path,PATH_MAX,stdin);                    if ((pos=strchr(path, '\n')) != NULL)                        *pos = '\0';                    result = delete_occurences_1(&path, cl);                    if(*result == -1){                        printf("File inesistente oppure problemi di creazione/modifica file\n");                    }                    else{                        if(*result == 0){                            printf("Nel file non sono state trovate occorrenze numeriche\n");                        }                        else{                            printf("Nel file sono state rimosse %d occorrenze numeriche\n",*result);                        }                    }                break;            case 'L':                    printf("Dimmi il nome del direttorio\n");                    fgets(path,PATH_MAX,stdin);                    if ((pos=strchr(path, '\n')) != NULL)                        *pos = '\0';                    out = subdirectory_list_1(&path,cl);                    printf("Ricevuto risultato");                    if(out->cont == -1){                        printf("Directory non trovata oppure problemi gestione file lato server\n");                    }                    else if(out->cont == 0){                        printf("Non sono state trovate sottocartelle di primo livello che soddisfano tali specifiche\n");                    }                    else{                        printf("Sottocartelle di primo livello trovate:\n");                        for(int i=0;i<out->cont;i++){                            printf("%s\n",out->directories[i].nameD);                        }                    }                break;            default:                printf("Servizio non disponibile, inserire E o L\n");        }        printf("Inserire:\n");        printf("E - per eliminare le occorrenze numeriche da un file di testo specificato presente lato server\n");        printf("L - per avere la lista dei sottodirettori di primo di livello contenenti almeno 5 file di testo dato un direttorio\n");        printf("ctrl^D per terminare\n");    }    free(out);    free(path);    clnt_destroy(cl);    printf("Programma terminato\n");    exit(0);}

for completeness, wishing that anyone could solve my problem, I paste also The server the .x file and the .h file.

Server:

#include "RPC_xFile.h"#include <rpc/rpc.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <dirent.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#include <dirent.h>#include <unistd.h>#define DIM 256int * delete_occurences_1_svc(char ** nameFile, struct svc_req * rq){    static int result=0;    int fd=open(*nomeFile,O_RDONLY);    if(fd<0){        printf("Errore apertura file %s\n",*nomeFile);        result=-1;        return &result;    }    char tempFile[DIM];    int cont=0;    for(int i=0;i<strlen(*nameFile);i++){        if((*(*nameFile)+i)=='.')            break;        else            cont++;    }    if(cont==(strlen(*nameFile))){        strcpy(tempFile,"temp.txt");    }    else{        memcpy(tempFile,*nameFile,cont);        strcat(tempFile,"_temp.txt");    }    printf("Creo file temporaneo %s\n",tempFile);    int tempFd=creat(tempFile,O_CREAT | O_TRUNC | S_IRUSR | S_IWUSR);    if(tempFd<0){        printf("Errore creazione file temporaneo %s\n",tempFile);        close(fd);        result=-1;        return &result;    }    char temp;    while(read(fd,&temp,1)>0){        if(temp<'0' || temp>'9'){            write(tempFd,&temp,1);        }        else{            result++;        }    }    close(fd);    if(rename(tempFile,*nameFile)<0){        printf("Modifica avvenuta con successo, ma errore avvenuto nella modifica del nome file temporaneo\n");        close(tempFd);        result=-1;        return &result;    }    printf("Modifica avvenuta correttamente, eliminati %d numeri\n",result);    return &result;}int EndsWith(char *str, char *suffix){    if(strlen(str) >= strlen(suffix))    {        if(!strcmp(str + strlen(str) - strlen(suffix), suffix))        {            return 1;        }    }    return 0;}ListDir * subdirectory_list_1_svc(char ** nameDir, struct svc_req * rq){    static ListDir out;    out.cont=0;    char pwd[PATH_MAX];    char finallyDir[PATH_MAX];    if(strlen(*nameDir)<=0){        printf("Errore input nome direttorio\n");        out.cont=-1;        return &out;    }    if((*nameDir[0])=='/'){        strcpy(finallyDir,*nomeDir);    }    else{        if(getcwd(pwd,sizeof(pwd))==0){            printf("Errore getcwd\n");                             //restituisce pwd            out.cont=-1;            return &out;        }        snprintf(finallyDir,PATH_MAX,"%s/%s",pwd,*nameDir);    }    DIR * dir;    out.cont=0;    dir=opendir(finallyDir);    if(dir==NULL){        printf("Directory non trovata\n");        out.cont=-1;        return &out;    }    struct stat statFile;    struct dirent * dr;    char filePath[PATH_MAX];    char filePath2[PATH_MAX];    DIR * dir2;    struct dirent * dr2;    int contAllow=0;    while((dr=readdir(dir))!=NULL && out.cont<6){        snprintf(filePath,PATH_MAX,"%s/%s",finallyDir,dr->d_name);        if(stat(filePath,&statFile)<0){            printf("Errore con stat file\n");            closedir(dir);            out.cont=-1;            return &out;        }        else if(S_ISDIR(statFile.st_mode)){            dir2 = opendir(filePath);            if(dir2==NULL){                printf("Apertura directory %s null\n",filePath);                out.cont=-1;                return &out;            }            else{                while((dr2=readdir(dir2))!=NULL && contAllow<5){                    snprintf(filePath2,PATH_MAX,"%s/%s",filePath,dr2->d_name);                    if(stat(filePath2,&statFile)<0){                        printf("Errore con stat file\n");                        closedir(dir);                        closedir(dir2);                        out.cont=-1;                        return &out;                    }                    else if(S_ISREG(statFile.st_mode)){                        if(EndsWith(dr2->d_name,".txt")==1){                            contAllow++;                        }                    }                }                if(!(contAllow<5)){                    strncpy(out.directories[out.cont].nameD,filePath,PATH_MAX);                    out.cont++;                }                contAllow=0;                closedir(dir2);            }        }    }    closedir(dir);    return &out;}

file.x:

#include <linux/limits.h>#define N 6struct DirectoryName{    char nameD[PATH_MAX];};struct ListDir{    DirectoryName directories[N];    int cont;};program RPC_XFILE {    version RPC_XFILEVERS{            int DELETE_OCCURENCES(string) = 1;            ListDir SUBDIRECTORY_LIST(string) = 2;    } = 1;} = 0x20000013;

file.h:

#define RPC_XFILE 0x20000013#define RPC_XFILEVERS 1#if defined(__STDC__) || defined(__cplusplus)#define DELETE_OCCURENCES 1extern  int * delete_occurences_1(char **, CLIENT *);extern  int * delete_occurences_1_svc(char **, struct svc_req *);#define SUBDIRECTORY_LIST 2extern  ListDir * subdirectory_list_1(char **, CLIENT *);extern  ListDir * subdirectory_list_1_svc(char **, struct svc_req *);extern int rpc_xfile_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);#else /* K&R C */#define DELETE_OCCURRENCES 1extern  int * delete_occurrences_1();extern  int * delete_occurrences_1_svc();#define SUBDIRECTORY_LIST 2extern  ListDir * subdirectory_list_1();extern  ListDir * subdirectory_list_1_svc();extern int rpc_xfile_1_freeresult ();#endif /* K&R C *//* the xdr functions */#if defined(__STDC__) || defined(__cplusplus)extern  bool_t xdr_DirectoryName (XDR *, DirectoryName*);extern  bool_t xdr_ListDir (XDR *, ListDir*);#else /* K&R C */extern bool_t xdr_DirectoryName ();extern bool_t xdr_ListDir ();#endif /* K&R C */#ifdef __cplusplus}#endif#endif /* !_RPC_XFILE_H_RPCGEN */

thank you for the attention


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>