Seg. Fault and not sure why - c

I am not sure why I am getting a seg fault. I know it is somewhere in my pidspec function but I'm not sure why it is happening. The goal of this program is to have the process id passed in as first argument to program, from there, the pid is located in the proc folder and the contents of that file are displayed to console. Any help would be greatly appreciated. I haven't written any C for a year so I'm a bit rusty.
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
void pidspec(char *v){
DIR *myDirectory;
struct dirent *myFile;
char *proc = "/proc";
printf("Made it here");
myDirectory = opendir(proc);
if(myDirectory){
printf("Made it here");
if(strcmp(myFile->d_name, v) == 0){
myDirectory = opendir(v);
if(myDirectory){
while ((myFile = readdir(myDirectory)))
printf("%s\n", myFile->d_name);
}
}
}
return;
}
int main(int argc, char *argv[]){
printf("Made it here");
if(argc == 2){
printf("%s",argv[1]);
pidspec(argv[1]);
}
return 0;
}

On your first run, myFile is not initialised, that is not pointing to anything, and then you de-refernece it!
struct dirent *myFile;
...
if(strcmp(myFile->d_name, v) == 0)
So either you didn't mean to use myFile here, or ensure it's pointing at something first.

Related

How to print hard link numbers with file names?

I'm trying to make a program
printing files that has the same inode numbers
printing the amount of hard links in the current directory.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
int main() {
DIR *dp;
dp = opendir(".");
struct dirent *dent1, *dent2;
while((dent1 = readdir(dp))) {
while((dent2 = readdir(dp))) {
struct stat buf;
if(stat(dent1->d_name, &buf)) {
perror("stat");
exit(1);
}
if((dent1->d_ino == dent2->d_ino) && (dent1->d_name != dent2->d_name) {
printf("---Same inode files---\n");
printf("%s\n", dent1->d_name);
printf("%s\n", dent2->d_name);
printf("Number of hard links = %o\n", (unsigned int)buf.st_nlink);
}
}
}
closedir(dp);
return 0;
}
This is my code I wrote.
I used two loops to compare each files inode numbers.
There was no error in the program. But it didn't work well as I expected.
There were 3 hard links in the current directory, but it only printed one of them.
I know my coding skills are not good, but I did my best and this is as far as I can get on my own.
Is there anything wrong with my program?
Do I have to add something more?
Is my code inefficient?

SIGSEGV segmentation fault with regards to the pwd.c file?

#include <unistd.h>
#include <pwd.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
uid_t userIdFromName(char *name)
{
struct passwd* pwd;
pwd=getpwnam(name);
if(pwd==NULL)
{
perror("gepwnam\n");
exit(EXIT_FAILURE);
}
return pwd->pw_uid;
}
int main(int argc, char* argv[])
{
uid_t uid;
int totalEntries=0;
DIR* dir;
struct dirent* newFile;
char strUID[65];
if(argc<1)
{
fprintf(stderr,"The format is: %s name",argv[0]);
exit(EXIT_FAILURE);
}
uid=userIdFromName(argv[1]);
dir=opendir("/proc/");
sprintf(strUID,"%d",uid);
while((readdir(dir))!=NULL)
{
totalEntries++;
}
char* dirNames[1000];
char* dirIds[1000];
int newCount=0;
dir=opendir("/proc/");
while((newFile=readdir(dir))!=NULL)
{
char statusFilePath[65]="/proc/";
strcat(statusFilePath,newFile->d_name);
strcat(statusFilePath,"/status");
FILE* statusFile=fopen(statusFilePath,"r");//one of the file has been opened;
size_t size;
char* lineData;
if(statusFile==NULL)
continue;
int currentPoisition=0;
while((getline(&lineData,&size,statusFile))!=-1)
{
if(strncmp("Uid:",lineData,strlen("Uid:"))==0)
{
char* dataLine;
currentPoisition=ftell(statusFile);
fseek(statusFile,0,SEEK_SET);
if(strstr(lineData,strUID)!=NULL)
{
int forName=0,forPid=0;
while((getline(&dataLine,&size,statusFile))!=-1)
{
if(strncmp("Name:",dataLine,strlen("Name:"))==0)
{
printf("%s\n",dataLine);
dirNames[newCount]=dataLine;
forName=1;
}
if(strncmp("Pid:",dataLine,strlen("Pid:"))==0)
{
printf("%s\n",dataLine);
dirIds[newCount]=dataLine;
forPid=1;
}
if(forName==1&&forPid==1)
{
newCount++;
}
}
}
fseek(statusFile,0,SEEK_SET);
fseek(statusFile,currentPoisition,SEEK_SET);
}
}
}
/* int runner=0;
while(runner<newCount)
printf("%s\n",dirNames[runner++]);*/
}
I have been unable to understand the core-dumps generated for this program.The program crashes halfway. Here is the core-dump info:
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00007f4ed8699d9c in _nss_files_getpwnam_r (name=0x0, result=0x7f4ed8896140 , buffer=0x559f75c7e2a0 "root", buflen=1024, errnop=0x7f4ed889a4c0) at nss_files/files-pwd.c:32
32 nss_files/files-pwd.c: No such file or directory.
I thank you for your help.
Have you qualified the contents of argv[1] before using it here?
uid=userIdFromName(argv[1]);
Also, among other issues, using a variable created as:
char* lineData;
in a function such as:
while((getline(&lineData,&size,statusFile))!=-1)
Will likely cause a seg-fault, as you are attempting to write to a location that you do not own.
Same for:
char *dataLine;
...
while((getline(&dataLine,&size,statusFile))!=-1)
etc.
Create memory and an address for these (and any other like them) before using.
eg:
size_t size = 1000;
char *lineData = malloc(size*(sizeof(*lineData));
if(lineDate)
{
//continue to use lineData
free(lineData);//when done using it
...

Can't store values on a struct defined in a library

In main:
#include stdio.h
#include stdlib.h
#include string.h
#include dictionary.h
int main( int argc, char ** argv ){
dictionary_t dictionary = NULL;
dictionary->entries = 1;
return 0;
}
//In header
#ifndef DICTIONARY_H
#define DICTIONARY_H
struct dictionary_s{
char * name;
llist_t content;
int entries;
};
typedef struct dictionary_s* dictionary_t;
#endif
//It compiles but shows Segmentation Fault (Core dump) in the console screen.
I have tried almost everything I can think of and checked several posts, but I've been unable to solve this problem.
In main:
#include stdio.h
#include stdlib.h
#include string.h
#include dictionary.h
int main( int argc, char ** argv ){
//dictionary_t dictionary = NULL;//This was your old line that leads to a null pointer voilation..
dictionary_t dictionary = (dictionary_t *) malloc(sizeof(dictionary_t));
if( NULL == dictionary){
//malloc failed, what do you wanna do now?
printf("Malloc failed\n");
//exit(-1);
while(1){} //just spin forever so you can see the error i suppose?
}
dictionary->entries = 1;
return 0;
}
Here is a malloc example, the stack example is similar but different.
https://en.wikibooks.org/wiki/C_Programming/stdlib.h/malloc

Segmentation Fault outside the loop only

Using C on Linux, I'm writing a code that stores all the information about the files in a directory using function stat() and prints them on the Terminal
The algorithm is quite simple, I made a structure array of "files" and dynamically allocated them. The structure contains a char array (string) so I dynamically allocated it too.
The thing is .. the dynamic allocation works fine but if I'm inside the while loop I can access the other element inside the structure - which is a structure stat object - but if I access it after the loop finishes, it gives me "Segmentation Fault"!
Here's the code
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
struct file{
char* name;
struct stat fbuf;
};
int main(int argc, char **argv)
{
char* dir=NULL;
int k;
dir=(char *)malloc(strlen(argv[argc-1])+1);
dir=argv[argc-1];
strcpy(dir,argv[argc-1]);
DIR *curr_dir;
struct dirent *dir_inode;
int i,j=0;
char* sum=NULL;
struct file* files=NULL;
if ((curr_dir = opendir(dir)) == NULL) {
fprintf(stderr, "Can't Open %s\n", argv[1]);
exit(2);
}
while (((dir_inode = readdir(curr_dir))) != NULL) {
files=(struct file*) realloc(files,((j)+1)*(sizeof(char*)+sizeof(struct stat))); // Structure array reallocation
(files+(j))->name=(char *)(malloc(strlen(dir_inode->d_name)+1));//name allocation
for(i=0;i<strlen(dir_inode->d_name);i++)
(files+(j))->name[i]=dir_inode->d_name[i];//name storage
(files+(j))->name[i]='\0';
sum= (char *) malloc(strlen(dir)+strlen(dir_inode->d_name)+2);//To add file name to its directory
for(i=0;i<strlen(dir);i++)
sum[i]=dir[i];
sum[i]='/';
i++;
for(k=0;dir_inode->d_name[k]!='\0';k++)
sum[i+k]=dir_inode->d_name[k];
sum[i+k]='\0';//file name with directory in sum
if( stat(sum,&((files+j)->fbuf)) == -1){ // the function gets information from the file name and stores them in fbuf
printf("error stat\n");
exit(1);
}
free(sum);
if( S_ISDIR( ( (files+(j))->fbuf ).st_mode ) ){
printf("d");
}
else {
printf("-");
}
//Here the output appears fine
//The output depends on accessing fbuf in files array
printf("statOK\n");
(j)++; // index
}
printf("%d %d %d\n",files,j,files+1);
printf("%d\n",j);
printf("\n\n\n\n");
for(i=0;i<j;i++){
printf("%s\n",(files+i)->name);
printf("%d\n",files);
//Starting from here, same syntax but outside the loop it gives the error
if( S_ISDIR( ( (files+i)->fbuf ).st_mode ) ){
printf("d");
else {
printf("-");
}
}
free(files);
free(dir);
closedir(curr_dir);
exit(1);
}
The code isn't complete yet but all what I want is to access the fbuf outside the loop, then I can complete it
Any ideas?
Bad size assumption
This allocation is wrong:
files=(struct file*) realloc(files,((j)+1)*(sizeof(char*)+sizeof(struct stat)));
Here, you assumed that the size of struct file was the sum of the sizes of its two components. But in fact, you don't know how that structure is packed and aligned, so the size of struct file could be larger than what you thought. You should just be using sizeof(struct file) instead:
files=(struct file*) realloc(files,(j+1)*(sizeof(struct file)));

Want to create a file with write mode in C

I am new in C. I want to create a file in linux C program and write environment variables in it. If file already exist I want to open and append. I have written the following code.
char *envFile=getenv("FILENAME");
int fdEnv=-1;
fdEnv=open(envFile,O_CREAT,O_RDWR,O_APPEND);
printf("%d",fdEnv);
char** env;
if(fdEnv>0)
{
for (env = environ; *env != 0; env++)
{
char *thisEnv = *env;
printf("%s",thisEnv);
write(fdEnv,thisEnv,strlen(thisEnv));
}
close(fdEnv);
}
But when I run it first time. A blank file is created. And it stays locked after execution. Looks like some error. Second time it fdEnv stays less than 0.
I really don't understand what is happening here. Please help.
Try using | to separate the flags.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
extern char **environ;
int main(void)
{
char *envFile = getenv("FILENAME");
int fdEnv = -1;
fdEnv = open(envFile, O_CREAT|O_RDWR|O_APPEND, 0644);
printf("%d\n", fdEnv);
int i = 0;
while (environ[i]) {
printf("%s\n", environ[i]);
write(fdEnv, environ[i], strlen(environ[i]));
char lf = '\n';
write(fdEnv, &lf, 1);
i++;
}
close(fdEnv);
return 0;
}
I've run above code on my linux computer and it works.
extern char **environ;
int main()
{
char **env;
char* filename = getenv("FILENAME")
const char* mode = "a";
FILE* file = fopen( filename, mode );
for ( env = environ; *env; ++env )
fprintf( file, "%s\n", *env );
fclose(file);
return(0);
}
You should think about handling when getenv fails, is blank, etc; let me know if you have any questions.

Resources