C language how to make a timeout function using in gethostname()? - c

This program reads the domain from a file to a string, truncates the string with "\n" as a key, and then executes the getostbyname() function for each domain to enter the resulting information into the file.
When I use gethostbyname, I want to create a function to enter fail if there is no response for a certain period of time. I tried to implement and process the timeout function, but the function did not work.
The logic that I thought of is calling gethostbyname() and entering fail in the file if there is no response for two seconds, then proceed to the next string and call gethostbyname().
The amount of files is about 150 million domains, so I took only a part of them and made them an example.
Please help me create the functions I want.
This is input.txt
www.naver.com
kbphonemall.com
kbplant.com
k-bplus.com
kbpointreestore.com
kbprint.com
kbprism.com
kbprivatebanking.com
kbpstore.com
kbr9rtudaf5ppy.com
kbrafting.com
kbraille.com
kbrainbank.com
kbrainbow.com
kbrainc.com
kbrainglocal.com
kbrandexpo.com
kbrandingschool.com
kbrandmall.com
kbrandmuseum.com
kbranking.com
k-bread.com
k-breadshop.com
kbreaknews.com
kbreasy.com
kbr-easy.com
kb-rent.com
kbrentacar.com
kb-rentcar.com
kbreport.com
k-brew.com
kbrewtec.com
kbrick.com
kbridge21.com
kbrno.com
k-broad.com
kbrockstar.com
kbrosbeauty.com
kbrosis.com
kbrworks.com
kbs1004.com
kbs123.com
kbs20.com
kbs220.com
kbs3.com
kbs31.com
kbs336.com
kbs358.com
kbs40.com
kbs556.com
kbs5678.com
kbs69.com
kbs7.com
kbs700.com
kbs79.com
kbs87.com
kbs9003.com
kbs911.com
kbs97.com
kbsarchive.com
kbsavers.com
kbsavings.com
kbsavingstar.com
kbsba.com
kbsbaechoo.com
kbsbook.com
kbsc1.com
kbscarf.com
kbsccoding.com
kbscert.com
kbschoolbanking.com
kbscyerrutb.com
kbsec.com
kbsec-greeting.com
kbsecuritynews.com
kbseschool.com
kbsestudy.com
kbsfn.com
kbsgmp.com
kbsgreen.com
kbsgulbee.com
kbs-hip.com
kbsholdings.com
kbshsg.com
kbsisa.com
kbsjeju.com
kbskadt.com
kbskiotmall.com
kbsklt.com
kbskorea.com
kbskorean.com
kbskovo.com
kbsktw.com
kbsktwidea.com
kbslogos.com
kbslove.com
kbs-lse.com
kbsmc-ibd.com
kbsmcob.com
kbsmedi.com
kbsmetal.com
kbsmol.com
kbs-mt.com
kbsmt-academy.com
kbsnd.com
kbsnews.com
kbsnnews.com
kbsonbocns.com
kbsonbodirect.com
kbsonsa.com
kbspetdoc.com
kbsplus.com
kbspro.com
kbsq.com
kbssky.com
kbssomgr.com
kbssports.com
kbsstudy.com
kbstar.com
kbstar2020.com
kbstar21.com
kbstar24.com
kbstarb.com
kbstarcard.com
kbstarclub.com
kbstardirect.com
kbstarebank.com
kbstarfg.com
kbstarfinance.com
kbstarflower.com
kbstarfx.com
kbstarg.com
kbstarir.com
kbstarland.com
kbstarmail.com
kbstarmall.com
kbstarmbank.com
kbstarmoney.com
kbstarpb.com
kbstarsvc.com
kbstarworld.com
kbstarzone.com
kbstbt.com
kbsteel.com
kbstock1.com
kbstock5.com
kbs-travel.com
kbstructure.com
kbstv.com
kbstve.com
kbsuf.com
kbsupercard.com
kbsupporters.com
kbsv.com
kbsvoice.com
kbtac.com
kbtchain.com
kbtechgate.com
kbtechone.com
kbtechpos.com
kbtechshop.com
kbtilbo.com
kb-truck.com
kbtups.com
kbtva.com
kbubi.com
kbuddhism.com
k-buddhism.com
k-buddy.com
kbujejucall.com
kbulgyonews.com
k-bulls.com
kbumall.com
kbund.com
kb-uniform.com
kbusinessnews.com
kbusinfo.com
kbusking.com
kbuvd.com
kbvalve.com
kbvipstock.com
kbweb.com
kbweddingfair.com
kbwel.com
kbwith.com
kbwlab.com
kbwnet.com
kb-world.com
kbworldwide.com
kb-world-wide.com
kby8992.com
kbynews.com
kc005.com
kc04.com
kc0522.com
kc1004.com
kc114.com
kc1357.com
kc1895.com
kc1904.com
kc22.com
kc25.com
kc712.com
kc9479.com
kc97bkyg3tvzgdws.com
kca2009.com
kca21.com
kca7.com
kcaa1.com
kcaa12.com
kcaa21.com
kca-academy.com
kcaawelfare.com
kcabiz.com
kca-charcoal.com
kcadcom.com
k-caddie.com
k-cae.com
kcafrp.com
kcagcf.com
kcagolf.com
kcagym.com
kcahub.com
kcairfiltertech.com
kcakca.com
kcakn.com
kcakorea.com
kcakyrgyz.com
kcallica.com
kcaltrip.com
kcangle.com
kcar.com
k-car.com
kcar2.com
kcaratpay.com
kcarauction.com
kcaraudio.com
kcarb2b.com
kcarcapital.com
kcar-capital.com
kcard.com
kcardirect.com
kcar-direct.com
k-cardirect.com
k-careeraptitude.com
kcarfriends.com
kcarlogis.com
kcarmall.com
kcarnet.com
kcarschool.com
kcarz.com
k-cata.com
kcateam.com
kcatfree.com
kca-therapy.com
kcb4u.com
kcb5.com
kcbc-acc.com
kcbcard.com
kcbec.com
kcbend.com
kcbetrade.com
kcbexchange.com
kcbiea.com
kc-biz.com
kcboltec.com
kcbs119.com
kcbtec.com
kcbtv.com
kcbunion.com
kcc000.com
kcc001.com
kcc002.com
kcc003.com
kcc007.com
kcc1004.com
kcc1122.com
kcc114.com
kcc2.com
kcc21.com
kcc258.com
kcc339.com
kcc5858.com
kcc775.com
kcc7777.com
kcc7979.com
kcc8282.com
kccaedu.com
kccardpos.com
kccarr.com
kccaster.com
kccasters.com
kccauto.com
kccce.com
kccchem.com
kccckorea.com
kcccolorndesign.com
kcccontainer.com
kccdeco.com
kccdepot.com
kccea.com
kccegis.com
kccei.com
kcceleader.com
kccenc.com
kccepost.com
kcceramice.com
kcceye.com
kccf2012.com
kccfilm.com
kccflooring.com
kccgift.com
kccglass.com
kccgroup.com
kcchain.com
kccheavyduty.com
kccholdings.com
kcchomecc.com
kcchomeccinterior.com
kcchwarang.com
kccia.com
kccichem.com
kccinsight.com
kccinterior.com
kccjewelry.com
kcckdong.com
kccks.com
kccmarine.com
kccmaul.com
kccnara.com
kccofficial.com
kccold.com
kccomang.com
kccond.com
kcconveyor.com
kc-coop.com
kc-cottrell.com
kccpaint114.com
kccpaintmall.com
kccplant.com
kccplus.com
kccpr.com
kccrefinish.com
kccrose.com
kccsa.com
kccscleaning.com
kccseoul.com
kccsilicon.com
kccsilicone.com
kccsmms.com
kccssafe.com
kccswitzen-patio.com
kcctm.com
kcctour.com
kccultural.com
kccview.com
kccvma.com
kccwelltztower.com
kccwindow.com
kccwindows.com
kccworld.com
kccycasters.com
kccyes.com
kcdc2020.com
kcdc21.com
kcdcode.com
kcdeaf.com
kcdfarm.com
kcdic.com
kcdkorea.com
kcdoll.com
kcdoor.com
k-cea.com
kcea98.com
kceasw.com
kceautodoor.com
kceccenter.com
kcecctv.com
kcecoin.com
kcecrs.com
and this is code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#define IPATH "example.txt"
#define OPATH "result/output.txt"
struct hostent *host;
int whatthe = 0;
void sig_handler(int signum){
host = NULL;
}
void dnslookup(char temp1[]){
host = gethostbyname(temp1);
}
int main(int argc, char *argv[]){
struct timeval tv;
double begin, end;
gettimeofday(&tv, NULL);
begin = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
char *ListBuffer;
int ListSize;
FILE *InputFile = fopen(IPATH, "r");
FILE *OutputFile = fopen(OPATH, "w");
fseek(InputFile, 0, SEEK_END);
ListSize = ftell(InputFile); //길이를 재고
ListSize ++; //NULL 문자가 들어갈 자리까지 해줌. 문자열의 끝을 알기 위해서
ListBuffer = malloc(ListSize); //잰 길이만큼 동적할당
memset(ListBuffer, 0, ListSize); //할당한 메모리 초기화
ListBuffer[ListSize] = '\0';
fseek(InputFile, 0, SEEK_SET);
fread(ListBuffer, ListSize, 1, InputFile); //읽어와서 동적할당한 배열에 넣기
char temp[] = {0, };
char *temp2;
int count = 0;
int h_count = 0;
char *str1 = NULL;
char *temp1 = strtok_r(ListBuffer, "\n", &str1);
while (temp1 != NULL){
dnslookup(temp1);
signal(SIGALRM,sig_handler);
alarm(2);
if (host != NULL){
fprintf(OutputFile, "%s / %s\n", temp1, inet_ntoa(*(struct in_addr *)host->h_addr_list[0]));
}
else{
fprintf(OutputFile, "%s / %s\n",temp1, "FAIL");
}
temp1 = strtok_r(NULL, "\n", &str1);
}
fclose(InputFile); //파일 포인터 닫기
fclose(OutputFile); //파일 포인터 닫기
free(ListBuffer); // 동적 메모리 해제
ListBuffer = NULL;
gettimeofday(&tv, NULL);
end = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
// 출력
printf("Execution time %f\n", (end - begin) / 1000);
}
what is problem?
If you need more explanation, I'll answer it quickly.
Thank you for reading my question. Have a nice day.

Use setjmp() & longjmp() pair with alarm().
#include <setjmp.h>
#define DNS_TIMEOUT 2 // timeout you need
static jmp_buf env;
void sig_handler (int signum) {
//host = NULL;
longjmp (env, 1); // jump to setjmp() location with value 1
}
int dnslookup (const char* domain) {
#define STATUS_TIMEOUT 128 // custom: timeout occurred
int status = 0;
signal (SIGALRM, sig_handler);
if (0 == setjmp(env)) { // to return here after timeout with return value 1
alarm (DNS_TIMEOUT); // setting timeout
host = gethostbyname (domain);
signal (SIGALRM, SIG_DFL);
alarm (0); // cancel alarm
} else { // after timeout-alarm we're here
// you may want to record TIMEOUT instead of FAIL
status = STATUS_TIMEOUT;
host = NULL;
//printf ("\nTimeout for %s", domain);
}
return status;
}
Also:
ListBuffer = malloc(ListSize); //잰 길이만큼 동적할당
memset(ListBuffer, 0, ListSize); //할당한 메모리 초기화 // not necessary
//ListBuffer[ListSize] = '\0'; // BUFFER-OVERFLOW
...
char *rmdList = NULL;
char *domain = strtok_r (ListBuffer, "\n", &rmdList);
while (domain != NULL) {
int status = dnslookup (domain);
// signal (SIGALRM, sig_handler);
// alarm (2);
if (host)
fprintf (OutputFile, "%s / %s\n", domain, inet_ntoa (* (struct in_addr *) host->h_addr_list[0]));
else if (STATUS_TIMEOUT == status)
fprintf (OutputFile, "%s / %s\n", domain, "TIMEOUT");
else
fprintf (OutputFile, "%s / %s\n", domain, "FAIL");
domain = strtok_r (NULL, "\n", &rmdList);
}
That said :
The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror()
functions are obsolete. Applications should use getaddrinfo(3),
getnameinfo(3), and gai_strerror(3) instead.

Related

not all PWMs are generated in Pi4 wiringPI language C

I am working on a project where I have to use multi-threading to generate multiple pwms. I am working on raspberry and I use WiringPI to generate softPWMs.
The problems is my raspberry does not want to generate 5 PWMs. Each time a run the code, the PWMs are generated randomly on 3 of the five pins I have setted. I do not know if it is the OS putting some threads on sleep or not.
I have no idea where the problem might be from. In fact, I tries using the same code to send periodically send many variable and it did work. But when I try using the same code with PWM it does not work. The code I am using is right below. If any of you has any idea please help
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <stdlib.h>
#include <pthread.h>
#define THREAD_NUM 1
struct thArg{
int PWM_pin1;
int rapportCyclique1;
int PWM_pin2;
int rapportCyclique2;
int PWM_pin3;
int rapportCyclique3;
int PWM_pin4;
int rapportCyclique4;
};
struct thArg data = {
22,20,
23,40,
24,80,
25,60
};
void* routine(void* args){
//int PWM_pin = *(int*)args->PWM_pin;
//int intensity = *(int*)args->rapportCyclique;
//pthread_mutex_lock(&mutexBuffer);
struct thArg *arrgs = (struct thArg *) args;
int PWM_pin1 = arrgs->PWM_pin1;
int rapportCyclique1 = arrgs->rapportCyclique1;
int PWM_pin2 = arrgs->PWM_pin2;
int rapportCyclique2 = arrgs->rapportCyclique2;
int PWM_pin3 = arrgs->PWM_pin3;
int rapportCyclique3 = arrgs->rapportCyclique3;
int PWM_pin4 = arrgs->PWM_pin4;
int rapportCyclique4 = arrgs->rapportCyclique4;
pinMode(PWM_pin1, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
pinMode(PWM_pin2, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
pinMode(PWM_pin3, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
pinMode(PWM_pin4, OUTPUT);
softPwmCreate(PWM_pin1, 1, 100);
softPwmWrite(PWM_pin1, rapportCyclique1);
softPwmWrite(PWM_pin2, rapportCyclique2);
softPwmWrite(PWM_pin3, rapportCyclique3);
softPwmWrite(PWM_pin4, rapportCyclique4);
//pthread_mutex_unlock(&mutexBuffer);
}
void* routine(void*);
int main(int argc, char** argv)
{
wiringPiSetup();
pthread_t th[THREAD_NUM];
for(int i=0; i<THREAD_NUM; i++)
{
struct thArg *a = malloc(sizeof(struct thArg));
*a = data;
if(pthread_create(&th[i], NULL, &routine, a) != 0)
{
perror("failed to create thread\n");
exit( EXIT_FAILURE );
}
}
for(int i = 0; i<THREAD_NUM; i++){
if(pthread_join(th[i], NULL)!= 0)
{
perror("Failed to join thread\n");
exit( EXIT_FAILURE );
}
}
//pthread_mutex_destroy(&mutexBuffer);
return EXIT_SUCCESS;
}

How to use the ARM PMU in GEM5?

I had a problem initializing the PMU in gem5 for an arm full system with the starter_fs.py in --cpu hpi.
i followed the instructions of this post Using perf_event with the ARM PMU inside gem5 and i managed to solve my problem. I added the patch and configure the system. I am not using perf. I try to access directly the registers and read them. As i see GEM5 has only some register events implemented. Can we add the others as well as :
for example EXC_TAKEN is not implemented. Is the following the way to add them?
self.addEvent(ProbeEvent(self,0x09, cpu, "EXC_TAKEN"))
#0x09: EXC_TAKEN ???
Also, reading the pmu event registers i manage to read them and extract the events but the pmccntr cycle register always returns zero? How gem5 increments this register? What are the steps to read the cycle reggister?
a code that i use to read using perf is the following:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/perf_event.h>
#define NUM_NODES 100
#define NONE 9999
struct _NODE
{
int iDist;
int iPrev;
};
typedef struct _NODE NODE;
struct _QITEM
{
int iNode;
int iDist;
int iPrev;
struct _QITEM *qNext;
};
typedef struct _QITEM QITEM;
QITEM *qHead = NULL;
int AdjMatrix[NUM_NODES][NUM_NODES];
int g_qCount = 0;
NODE rgnNodes[NUM_NODES];
int ch;
int iPrev, iNode;
int i, iCost, iDist;
void print_path (NODE *rgnNodes, int chNode)
{
if (rgnNodes[chNode].iPrev != NONE)
{
//print_path(rgnNodes, rgnNodes[chNode].iPrev);
}
//printf (" %d", chNode);
fflush(stdout);
}
void enqueue (int iNode, int iDist, int iPrev)
{
QITEM *qNew = (QITEM *) malloc(sizeof(QITEM));
QITEM *qLast = qHead;
if (!qNew)
{
//fprintf(stderr, "Out of memory.\n");
exit(1);
}
qNew->iNode = iNode;
qNew->iDist = iDist;
qNew->iPrev = iPrev;
qNew->qNext = NULL;
if (!qLast)
{
qHead = qNew;
}
else
{
while (qLast->qNext) qLast = qLast->qNext;
qLast->qNext = qNew;
}
g_qCount++;
// ASSERT(g_qCount);
}
void dequeue (int *piNode, int *piDist, int *piPrev)
{
QITEM *qKill = qHead;
if (qHead)
{
// ASSERT(g_qCount);
*piNode = qHead->iNode;
*piDist = qHead->iDist;
*piPrev = qHead->iPrev;
qHead = qHead->qNext;
free(qKill);
g_qCount--;
}
}
int qcount (void)
{
return(g_qCount);
}
int dijkstra(int chStart, int chEnd)
{
for (ch = 0; ch < NUM_NODES; ch++)
{
rgnNodes[ch].iDist = NONE;
rgnNodes[ch].iPrev = NONE;
}
if (chStart == chEnd)
{
//printf("Shortest path is 0 in cost. Just stay where you are.\n");
}
else
{
rgnNodes[chStart].iDist = 0;
rgnNodes[chStart].iPrev = NONE;
enqueue (chStart, 0, NONE);
while (qcount() > 0)
{
dequeue (&iNode, &iDist, &iPrev);
for (i = 0; i < NUM_NODES; i++)
{
if ((iCost = AdjMatrix[iNode][i]) != NONE)
{
if ((NONE == rgnNodes[i].iDist) ||
(rgnNodes[i].iDist > (iCost + iDist)))
{
rgnNodes[i].iDist = iDist + iCost;
rgnNodes[i].iPrev = iNode;
enqueue (i, iDist + iCost, iNode);
}
}
}
}
//printf("Shortest path is %d in cost. ", rgnNodes[chEnd].iDist);
//printf("Path is: ");
//print_path(rgnNodes, chEnd);
//printf("\n");
}
}
int main(int argc, char *argv[]) {
int diff = 0;
uint64_t num_cycles_nominal=0;
uint64_t num_cycles_attack=0;
uint64_t counter_cpu_cycles = 0;
//system("./load-module");
int i,j,k;
FILE *fp;
static int perf_fd_cpu_cycles;
static struct perf_event_attr attr_cpu_cycles;
attr_cpu_cycles.size = sizeof(attr_cpu_cycles);
attr_cpu_cycles.exclude_kernel = 1;
attr_cpu_cycles.exclude_hv = 1;
attr_cpu_cycles.exclude_callchain_kernel = 1;
attr_cpu_cycles.type = PERF_TYPE_RAW;
attr_cpu_cycles.config = 0x11;
/* Open the file descriptor corresponding to this counter. The counter
should start at this moment. */
if ((perf_fd_cpu_cycles = syscall(__NR_perf_event_open, &attr_cpu_cycles, 0, -1, -1, 0)) == -1)
fprintf(stderr, "perf_event_open fail %d %d: %s\n", perf_fd_cpu_cycles, errno, strerror(errno));
if (argc<2) {
//fprintf(stderr, "Usage: dijkstra <filename>\n");
//fprintf(stderr, "Only supports matrix size is #define'd.\n");
}
/* open the adjacency matrix file */
fp = fopen (argv[1],"r");
/* make a fully connected matrix */
for (i=0;i<NUM_NODES;i++) {
for (j=0;j<NUM_NODES;j++) {
/* make it more sparce */
fscanf(fp,"%d",&k);
AdjMatrix[i][j]= k;
}
}
/* Get and close the performance counters. */
read(perf_fd_cpu_cycles, &counter_cpu_cycles, sizeof(counter_cpu_cycles));
//close(perf_fd_cpu_cycles);
printf("Number of cpu_cycles before: %d\n", counter_cpu_cycles);
num_cycles_nominal = counter_cpu_cycles;
/* Get and close the performance counters. */
read(perf_fd_cpu_cycles, &counter_cpu_cycles, sizeof(counter_cpu_cycles));
//close(perf_fd_cpu_cycles);
printf("Number of cpu_cycles after attack: %d\n", counter_cpu_cycles);
num_cycles_attack = counter_cpu_cycles - num_cycles_nominal;
/* finds 10 shortest paths between nodes */
for (i=0,j=NUM_NODES/2;i<100;i++,j++) {
j=j%NUM_NODES;
dijkstra(i,j);
}
read(perf_fd_cpu_cycles, &counter_cpu_cycles, sizeof(counter_cpu_cycles));
close(perf_fd_cpu_cycles);
printf("Number of cpu_cycles end: %d\n", counter_cpu_cycles);
num_cycles_nominal = counter_cpu_cycles - num_cycles_attack;
printf("Number of cpu_cycles nominal: %d\n", num_cycles_nominal);
printf("Number of cpu_cycles attack: %d\n", num_cycles_attack);
exit(0);
}
the problem is that i can read the branch misses with perf having 0x10 instead 0f 0x11 (cycle counters RAW EVENT in GEM5) but using 0x11 for reading the cycles i get zero. When i try to reverse engineer the increment of cycle counter i do the following comments:
when simple/atomic or simple/timing i see that updateCycleCounter is called from the base.hh, also for the 03 cpu model. When HPI and considering that hpi is a MinorCPU model i see that updateCycleCounter is called only in POWER_STATE_ON, but i didnt find in the code a POWER_STATE_ON reference updateCycleCounter(CPU_STATE_ON) which will update the cycle counter. Please help me verify this assumption.
*****The problem was that in the MinorCPU the updateCycleCounter wasnt called for the CPU_STATE_ON which updates the ActiveCycles. It was fixed by the following patch https://gem5-review.googlesource.com/c/public/gem5/+/38095 .

Passing parameters to custom syscall in unix

I am attempting to create a custom system call that inputs an (long) array and outputs some basic information about this array for a class assignment.
I am having some trouble passing the appropriate arguments into the system call.
Below is the test program I am using to debug this system call:
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "array_stats.h"
#define _ARRAY_STATS_ 341 // for a 64 bit system
int main () {
struct array_stats * stats;
long ary[3];
ary[0] = 1;
ary[1] = 2;
ary[2] = 3;
long s = 3;
printf("\nDiving to kernel level\n\n");
// Calling array_stats syscall
int result = syscall(_ARRAY_STATS_, &stats, ary, s);
printf("\nRising to user level w/ result = %d\n\n", result);
return 0;
}
Here is the array_stats system call, I have inserted a bunch of printk() statements for debugging purposes:
#include <linux/kernel.h>
#include <asm/errno.h>
#include <linux/uaccess.h>
#include "array_stats.h"
asmlinkage long sys_array_stats (struct array_stats *stats, long data[], long size) {
printk("1. %s\n", "System call started");
long loc_max;
long loc_min;
long loc_sum;
long local_data[size];
int i = 1;
int result = 0;
printk("2. %s\n", "Variables created");
printk("Size %lu\n", size);
printk("data[0] = %lu\n", data[0]);
printk("data[1] = %lu\n", data[1]);
printk("data[2] = %lu\n", data[2]);
if (size <= 0) return -EINVAL;
printk("3. %s\n", "Size validated");
result = copy_from_user (local_data, data, size * sizeof * data);
if (!result) return EFAULT;
loc_max = local_data[0];
loc_min = local_data[0];
loc_sum = local_data[0];
for (; i < loc_sum; i++) {
loc_sum += local_data[i];
if (local_data[i] < loc_min) loc_min = local_data[i];
if (local_data[i] > loc_max) loc_max = local_data[i];
}
stats -> min = loc_min;
stats -> max = loc_max;
stats -> sum = loc_sum;
printk("Min: %lu\n", loc_min);
printk("Max: %lu\n", loc_max);
printk("Sum: %lu\n", loc_sum);
return result;
}
The running of the test program results in the following:
root#debian-amd64:~# ./array_stats_test
Diving to kernel level
[ 92.735994] 1. System call started
[ 92.737851] 2. Variables created
[ 92.738037] Size 0
[ 92.738155] data[0] = 0
[ 92.738417] data[1] = 6955032
[ 92.738565] data[2] = 0
Rising to user level w/ result = -1
root#debian-amd64:~#
As you can see, the arguments aren't being passed in properly. What am I doing wrong?
Thanks in advance!

SIGSEGV on strftime (libc.so.6) using gcc (GCC) 5.3.0

i have a http client written in c , and in some section i get the time and fill in in buffer , i checked all the input values are valid , but when the function runs it gives segmentation fault , Line number 77 , the line :
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
i tested the inputs by debugger and non of them is problematic
After compiling run with this command
./COMPILED_FILE_NAME https://stackoverflow.com/ -d 1:1:1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#define HEADER "-h"
#define TIME_STAMP "-d"
#define PROTOCOL "http://"
#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
#define DEFAULT_PORT 80
#define BUFFER_SIZE 256
typedef struct sockaddr_in sockaddr_t;
void printError(int flag,char * name);
int parseUrl(char * url,char** host,int * portt ,char ** path);
int checkString(const char * date);
int parseDate(char* date,int * day,int * hour,int* minute);
void freeData();
int socketConnect(char * host,int port,char * page,char* requestType , char * dateRequest );
char * host; // the url with out any addititons
char * path; // the path after the url
int main(int argc,char * argv[])
{
time_t now;
now = time(NULL);
char timebuf[BUFFER_SIZE/2];
int headrFlag=0;
int timeFlag=0;
int day=0,hour=0,minute=0;
// char * host;//=(char*)malloc(sizeof(char)*128);
// char * path;//=(char*)malloc(sizeof(char)*128);
char requestType[BUFFER_SIZE];
char dateRequest[BUFFER_SIZE];
bzero(dateRequest,sizeof(dateRequest));
strcpy(requestType,"GET");
int port=DEFAULT_PORT;
if(argc >1 && argc <=5) // the host at least their
{
// checking the incoming args
int i = 1;
for(;i<argc;i++)
{
if(strcmp(argv[i],HEADER)==0)
{
if(headrFlag==0)
{
headrFlag=1;
strcpy(requestType,"HEAD");
}
else
{
printError(0,"");
break;
}
}
else if(strcmp(argv[i],TIME_STAMP)==0)
{
// take the date from the next i , if not there as error
if(timeFlag==0)
{
timeFlag=1;
if(i+1!=argc && parseDate(argv[i+1],&day,&hour,&minute)!=-1) // there is date "thing" after the flag
{
now=now-(day*24*3600+hour*3600+minute*60); //where day, hour and min are the values
//from the input
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
//timebuf holds the correct format of the time
sprintf(dateRequest,"If-Modified-Since: %s\n",timebuf);
i++;
continue;
}
else
{
printError(1,"");
}
}
else
{
printError(0,"");
}
printError(0,"");
break;
}
else // the host
{
if(parseUrl(argv[i],&host,&port,&path)<0)
{
printError(1,"");
}
}
}
// connect after every thing is valid
if(host!=NULL){socketConnect(host,port,path,requestType,dateRequest);}else{printError(0,"");}
}
else
{
printError(1,"");
}
return 0;
}
void printError(int flag,char * name)
{
// 0 wrong command usage
//1 wrong input
//2 system call failure
if(flag==0)
{
printf("Usage: client [-h] [-d <time-interval>] <URL>\n");
}
else if(flag==1) // wrong input
{
printf("wring input\n");
}
else // flag ==2 , sys calls
{
perror(name);
}
freeData();
exit(-1);
}
// handles the validation of the url
int parseUrl(char * url,char** host,int * portt ,char ** path)
{
char * det=":";
char * det1="/";
char * port;
char * temp;
char * pathValue;
int pathSize=0,hostSize=0;
int addSize=0;
int portNumber=80;
char * urlToParse;
if((temp=strstr(url,PROTOCOL))!=NULL && temp==url) // checking http://
{
temp=&temp[strlen(PROTOCOL)];
urlToParse=temp;
if(strlen(urlToParse) <=1)
return -1;
}
else
{
return -1;
}
if((temp=strstr(temp,det))!=NULL) // checking port
{
int len1 = strlen(temp)-1; // length from the : to the end
addSize=len1;
char * temp1;
if((temp1=strstr(temp,det1))!=NULL)
{
pathSize= strlen(temp1);
pathValue=temp1;
int portLength= len1-pathSize;
port = (char*)malloc(sizeof(char)*portLength+1);
if(port==NULL)
{
printError(2,"malloc");
}
port[portLength]='\0';
strncpy(port,++temp,portLength);
if(checkString(port)<0)
return -1;
portNumber=atoi(port);
free(port);
addSize=len1+1;
}
}
else // no port
{
temp=urlToParse;
if((temp=strstr(temp,det1))==NULL) // checking filepath
{
return -1;
}
else
{
pathValue=temp;
pathSize=strlen(temp);
addSize=pathSize;
}
}
hostSize=strlen(urlToParse)-addSize;
*host =(char*)malloc(sizeof(char)*(hostSize+1));
if(*host==NULL)
{
printError(2,"malloc");
}
**host=NULL;
* path =(char*)malloc(sizeof(char)*(pathSize+1));
if(*path==NULL)
{
printError(2,"malloc");
}
**path=NULL;
// *path=pathValue;
strcpy(*path,pathValue);
*portt=portNumber;
strncpy(*host,urlToParse,hostSize);
return 0;
}
// checks if the number is valid
int checkString(const char * date)
{
if(date==NULL || strcmp(date,"")==0)
{
return -1;
}
int len = strlen(date);
if(strlen(date)>1 && strncmp(&date[0],"-",1)==0 )
{
date++;
}
while(date && strcmp(date,"")!=0)
{
if(isdigit(*(date++))==0)
{
return -1;
}
}
return 1;
}
// converts from chars to int
int parseDate(char* date,int * day,int * hour,int* minute)
{
char * det=":";
// int days,hours,minutes;
char * dayss = strtok(date,det);
char * hourss = strtok(NULL,det);
char * moinutss = strtok(NULL,det);
if(dayss ==NULL || hourss==NULL||moinutss==NULL)
{
return -1;
}
if(checkString(dayss)<0 || checkString(hourss)<0 || checkString(moinutss)<0 )
return -1;
*day = atoi(dayss);
*hour= atoi(hourss);
*minute=atoi(moinutss);
return 1;
}
// free what ever memory we used
void freeData()
{
if(host !=NULL)
{
free(host);
}
if(path!=NULL)
{
free(path);
}
}
// handles the connection to the server
int socketConnect(char * host,int port,char * page,char* requestType , char * dateRequest )
{
struct in_addr serverIp;
struct hostent * hostt =NULL;
hostt= (struct hostent *)gethostbyname(host);
if(hostt==NULL)
{
freeData();
herror("");
printf("\n");
exit(0);
}
struct addr_in** addresses = (struct in_addr **)hostt->h_addr_list;
int i=0;
// char * ss= inet_ntoa(((struct in_addr*)hostt->h_addr)->s_addr);
// serverIp.s_addr=inet_addr(ss);
struct sockaddr_in packet;
packet.sin_family=AF_INET;
packet.sin_port=htons(port);
packet.sin_addr.s_addr=((struct in_addr*) hostt->h_addr)->s_addr;
int socketFd ;
if((socketFd=socket(PF_INET,SOCK_STREAM,0))<0)
{
printError(2,"socket");
}
if(connect(socketFd,(struct sockaddr*)&packet,sizeof(packet))<0)
{
printError(2,"connect");
}
char buffer[BUFFER_SIZE*4];
char * req = malloc(sizeof(requestType)+sizeof(page)+sizeof(dateRequest)+BUFFER_SIZE);
if(req==NULL)
{
freeData(2,"malloc");
}
sprintf(req,"%s http://%s%s HTTP/1.0\r\n%s\n",requestType,host,page,dateRequest);
printf("HTTP request =\n%s\nLEN = %d\n",req,strlen(req));
send(socketFd,req,strlen(req),0);
int sizeOfResponse=0;
int reading=0 ;
do
{
bzero(buffer,sizeof(buffer));
reading= read(socketFd,buffer,sizeof(buffer),0);
if(reading >0)
{
printf("%s",buffer);
sizeOfResponse=sizeOfResponse+strlen(buffer);
}
}
while(reading>0);
printf("\nTotal received response bytes: %d\n",sizeOfResponse);
close(socketFd);
free(req);
freeData();
}
Root cause: you must #include <time.h>.
The reason is that without a declaration in scope the compiler assumes that gmtime() returns int. In reality it returns struct tm *, and apparently on your platform a pointer is wider than the integer.
You should enable all warnings and pay attention to them.

How to use fgetpwent()?

I am trying to get a list of all the users in the system (linux, fedora).
and i've heard that the function:fgetpwent is the one that i need to that mission.
the sad part is that i didnt find any documentation or example of how to use this function.
if someone would give me an example, that would be great, thanks in advance.
No idea why I ever could have used it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
#include <pwd.h>
#include <sys/types.h>
char *testentry = "testread";
static void read_etc_passwd (void) {
struct passwd *pwd_entry = NULL;
int found = 0;
setpwent(); // go to the top of /etc/passwd
while (!found && (pwd_entry = getpwent())){
if (0 == strcmp (testentry, pwd_entry->pw_name)){
found = 1;
}
}
if (found) {
printf ("name = %s\nhome = %s\n", pwd_entry->pw_name,
pwd_entry->pw_dir);
} else {
puts("could not find the entry you were looking for, or"
"some error occurred");
}
}
void change_etc_passwd (void){
struct passwd *pwd = NULL;
FILE *pwd_fd = NULL;
FILE *pwd_new = NULL;
int result = 0;
pwd_fd = fopen ("/etc/passwd", "r");
pwd_new = fopen ("/tmp/passwd.neu", "a");
// assuming everthing went fine (bad idea)
while (pwd = fgetpwent (pwd_fd)){
if (0 == strcmp (pwd->pw_name, testentry)){
pwd->pw_passwd = crypt ("new_pwd", "aa");
}
result = putpwent(pwd, pwd_new);
if (result < 0){
fprintf (stderr, "Failed to write entry, giving up\n");
exit (EXIT_FAILURE);
}
}
}
int main (void) {
/* handling of /etc/passwd */
read_etc_passwd ();
change_etc_passwd();
return 0;
}
Add error handling and it may even work without breaking ;-)

Resources