here are the code folks :
#include <stdio.h>
#include <stdlib.h>
#include <gnokii.h>
#include <signal.h>
/*
*
*/
#define _(x) x
struct gn_statemachine *state = NULL;
void busterminate(void) {
gn_lib_phone_close(state);
gn_lib_phoneprofile_free(&state);
gn_lib_library_free();
}
void businit(void) {
gn_error error;
atexit(busterminate);
error = gn_lib_phoneprofile_load(NULL, &state);
if (GN_ERR_NONE == error) {
error = gn_lib_phone_open(state);
}
if (GN_ERR_NONE != error) {
fprintf(stderr, "%s\n", gn_error_print(error));
exit(-1);
}
}
void signal_handler(int signal) {
(void)signal;
exit(-2);
}
int main(int argc, char *argv[]) {
gn_data *data;
gn_sms sms;
gn_error error;
businit();
signal(SIGINT, signal_handler);
gn_data_clear(data);
sprintf(sms.remote.number,"%s","+628571641111");
sprintf(sms.user_data[0].u.text,"%s","tesss");
data->message_center = calloc(1, sizeof(gn_sms_message_center));
data->message_center->id= 1;
error = gn_sm_functions(GN_OP_GetSMSCenter, data, state);
if(error == GN_ERR_NONE)
{
snprintf(sms.smsc.number,sizeof(sms.smsc.number),"%s",data->sms->smsc.number); // set to sms.smsc.number from data.sms.smsc.number
sms.smsc.type = data->message_center->smsc.type;
//g_slice_free(gn_sms_message_center,data->message_center); // free the ram
free(data->message_center);
}
if(!sms.smsc.number[0])
{
printf("failed once getting sms center number\n");
}
if(!sms.smsc.type)
{
sms.smsc.type = GN_GSM_NUMBER_Unknown;
}
data->sms = &sms;
//send the message
error = gn_sms_send(data,state);
if(error == GN_ERR_NONE)
{
if(sms.parts > 1)
{
int j;
printf("sms sent with : %d parts, and reference number is : ", sms.parts);
for(j=0; j < sms.parts; j++)
{
printf("%d\n",sms.reference[j]);
}
}
else
{
printf("one sms sent with reference number : %d\n",sms.reference[0]);
}
}
else
{
printf("libgnokii error : %s\n",gn_error_print(error));
}
free(sms.reference);
return 0;
}
im gonna send an sms to +628571641111, with the text "tesss", but unfortunately the OS said it segmentation fault, thus, where is my fault ?
$ gnokii --identify
GNOKII Version 0.6.29
IMEI : 3556XXXXX509XXX
Manufacturer : ZTE INCORPORATED
Model : MF627
Product name : MF627
Revision : BD_3GHAP673A4V1.0.0
$ gdb -q ./gnokii_send_sms
Reading symbols from /root/gnokii_send_sms...(no debugging symbols found)...done.
(gdb) r
Starting program: /root/gnokii_send_sms
[Thread debugging using libthread_db enabled]
Program received signal SIGSEGV, Segmentation fault.
0x00317334 in ?? () from /lib/libc.so.6
(gdb)
You're passing to gn_data_clear a pointer you haven't initialized yet. In the beginning of your main function you need to have
gn_data data;
Not
gn_data *data;
Here's the function implementation:
GNOKII_API void gn_data_clear(gn_data *data)
{
memset(data, 0, sizeof(gn_data));
}
Related
(I have completely rewritten this question, as I have a minimal example now; see in history how original post looked like)
Consider this libusb program, which I compile under MINGW64 (part of MSYS2, which is updated as of today) on Windows 10 (also with latest updates as of today):
libusb-test.c
// build under MINGW64 on Windows with (assuming mingw64/mingw-w64-x86_64-libusb 1.0.26-1 installed):
// gcc -Wall -g -I/mingw64/include/libusb-1.0 libusb-test.c -o libusb-test.exe -lusb-1.0
#include <inttypes.h> //PRIu64
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "libusb.h"
int main(int argc, char *argv[]) {
libusb_device **devs;
ssize_t cnt;
int r=0, i;
struct libusb_device_descriptor desc;
r = libusb_init(NULL);
if (r < 0) {
printf("error: Cannot libusb_init, exiting\r\n");
return r;
}
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) {
printf("error: Cannot libusb_get_device_list (Failed to enumerate USB devices), exiting\r\n");
libusb_exit(NULL);
return 1;
}
for (i = 0; devs[i]; i++) { // or: for (libusb_device **dev = devs; *dev; dev++)
libusb_device *dev = devs[i];
libusb_device_handle *handle = NULL;
printf("Trying device %d: %p\r\n", i, dev);
int ret = libusb_get_device_descriptor(dev, &desc);
if (ret) {
printf(" Failed to read device %d descriptor (%d)\r\n", i, ret);
} else {
ret = libusb_open(dev, &handle);
if (ret) {
printf(" Failed to open device %d (%d)\r\n", i, ret);
} else {
printf( " device %d open ( handle %p )\r\n", i, handle);
}
if (handle) {
libusb_close(handle);
handle = NULL;
}
}
} // end for
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
printf("Program finished; exiting.");
return r;
}
The program, after compiling, actually seems to run fine:
$ ./libusb-test.exe
Trying device 0: 000001eeb9321890
Failed to open device 0 (-5)
Trying device 1: 000001eeb9320c30
device 1 open ( handle 000001eeb93242e0 )
...
Trying device 12: 000001eeb9322640
device 12 open ( handle 000001eeb93242e0 )
Trying device 13: 000001eeb7a7bc50
Failed to open device 13 (-12)
Program finished; exiting.
... however, if I try to debug with gdb by breaking into main - it fails with "Cannot insert breakpoint":
$ gdb --args ./libusb-test.exe
GNU gdb (GDB) 12.1
...
Reading symbols from ./libusb-test.exe...
(gdb) b main
Breakpoint 1 at 0x140001593: file libusb-test.c, line 11.
(gdb) r
Starting program: C:\msys64\tmp\libusb-test.exe
[New Thread 20144.0x24c0]
[New Thread 20144.0x436c]
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x140001584
Command aborted.
(gdb)
Why does this happen - and how can I get gdb to break into this program?
I'm new to Stackoverflow and c and pulseaudio. Thanks for all help.
I get a the Segmentation fault (core dumped) error in the mainloop of a custom pulseaudiolib code. It it takes 1 to 30s to apear.
I am trying to get the audio data of a custom sink
pacmd load-module module-null-sink sink_name=MySink
pacmd update-sink-proplist MySink device.description=MySink
pacmd load-module module-loopback sink=MySink
use its data and pip it back to the speaker.
I'm pretty new at C and pulseaudio so I used the doc and https://menno.io/posts/pulseaudio_monitoring/
This is my code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <pulse/context.h>
#include <pulse/def.h>
#include <pulse/format.h>
#include <pulse/introspect.h>
#include <pulse/stream.h>
#include <pulse/thread-mainloop.h>
const char * SINKNAME = "MySink";
const char * SPEAKNAME = "alsa_output.pci-0000_05_00.6.analog-stereo";
char found = 0;
const uint32_t RATE = 344;
pa_stream * gStream = NULL;
void stream_read_cb(pa_stream *stream, size_t length, void *index_incr){
printf("in read\n");
const void * samples[length];
size_t * leng;
(*leng) = length;
printf("length : %d, %d\n",length,(*leng));
int res = pa_stream_peek(stream, samples, leng);
pa_stream_drop(stream);
if(gStream != NULL){
int result = pa_stream_write( gStream, samples,length, NULL,0, PA_SEEK_RELATIVE);
}
printf("[");
if (res == 0){
for(int i = 0 ; i< length; i++){
printf("%d,",samples[i]);
}
}
printf("]\n");
}
void sink_info_cb(pa_context* context, const pa_sink_info *sink_info_p, int eol, void *userdata){
printf( "in sinkinfo\n");
const pa_sink_info* sink_info = sink_info_p;
if(found !=1){
printf( "index: %d\n", (*sink_info).index);
printf("help\n");
printf( "name: %s\n", (*sink_info).name);
printf( "description: %s\n", (*sink_info).description);
printf("searchName: %s\n",SINKNAME);
if (strcmp((*sink_info).name, SINKNAME) == 0){
found = 1;
// Found the sink we want to monitor for peak levels.
// Tell PA to call stream_read_cb with peak samples.
printf( "setting up peak recording using: %s\n ", (*sink_info).monitor_source_name);
pa_sample_spec *samplespec;
(*samplespec).channels = 1;
(*samplespec).format = PA_SAMPLE_U8;
(*samplespec).rate = RATE;
pa_stream * stream = pa_stream_new(context, "peak detect demo", samplespec, NULL);
pa_stream_set_read_callback(stream,
stream_read_cb,
( *sink_info).index);
pa_stream_connect_record(stream,
(*sink_info).monitor_source_name,
NULL,
PA_STREAM_PEAK_DETECT);
printf("settetd \n");
}/*
if((*sink_info).name[0] == 'a'){
//printf( "setting up peak recording using: %s\n ", (*sink_info).monitor_source_name);
//pa_sample_spec *samplespec;
// (*samplespec).channels = 1;
// (*samplespec).format = PA_SAMPLE_U8;
// (*samplespec).rate = RATE;
printf("set gStream");
//gStream = pa_stream_new(context, "peak detect demo", samplespec, NULL);
//printf("gSteam setted\n");
}*/
printf("out sink\n");
}
}
void context_notify_cb(pa_context *context, void *userdata){
printf("in context\n");
pa_context_state_t state = pa_context_get_state(context);
if( state == PA_CONTEXT_READY){
printf( "Pulseaudio connection ready...");
// Connected to Pulseaudio. Now request that sink_info_cb
// be called with information about the available sinks.
pa_operation * o = pa_context_get_sink_info_list(context, sink_info_cb, userdata);
pa_operation_unref(o);
}
else if( state == PA_CONTEXT_FAILED){
printf( "Connection failed");
}
else if( state == PA_CONTEXT_TERMINATED){
printf( "Connection terminated");
}
printf("out context\n");
}
void init(){
// Wrap callback methods in appropriate ctypefunc instances so
// that the Pulseaudio C API can call them
printf("in init\n");
pa_context_notify_cb_t _context_notify_cb;
//context_notify_cb(context_notify_cb void ** userdata);
pa_sink_info_cb_t(sink_info_cb);
pa_stream_request_cb_t(stream_read_cb);
// Create the mainloop thread and set our context_notify_cb
// method to be called when there's updates relating to the
// connection to Pulseaudio
pa_threaded_mainloop* _mainloop = pa_threaded_mainloop_new();
pa_mainloop_api* _mainloop_api = pa_threaded_mainloop_get_api(_mainloop);
pa_context *context = pa_context_new(_mainloop_api, "peak_demo");
pa_context_set_state_callback(context, context_notify_cb,NULL);
pa_context_connect(context, NULL, 0, NULL);
pa_threaded_mainloop_start(_mainloop);
printf("out intit\n");
}
int main(int argc, char*argv[]) {
init();
printf("Hello World\n");
while(1){
;
}
printf("end World\n");
return 0;
}
if I try to uncomment
/*
if((*sink_info).name[0] == 'a'){
//printf( "setting up peak recording using: %s\n ", (*sink_info).monitor_source_name);
//pa_sample_spec *samplespec;
// (*samplespec).channels = 1;
// (*samplespec).format = PA_SAMPLE_U8;
// (*samplespec).rate = RATE;
printf("set gStream");
//gStream = pa_stream_new(context, "peak detect demo", samplespec, NULL);
//printf("gSteam setted\n");
}
*/
the error shots like directly.
the output:
...
in read
length : 1, 1
[-1261830080,]
in read
length : 6, 6
[-1246625728,-1017902384,0,0,-1017902304,591610535,]
in read
length : 1, 1
[-1259864000,]
in read
length : 6, 6
[-1227227072,-1017902384,0,0,-1017902304,591610535,]
in read
length : 1, 1
[-1255800768,]
in read
length : 6, 6
[-1234960320,-1017902384,0,0,-1017902304,591610535,]
in read
length : 7, 7
[-1249247168,-939474438,-1017902512,-1017902384,0,0,-1017902304,]
in read
length : 7, 7
[-1242693568,-939474438,-1017902464,-1017902336,0,0,-1017902256,]
Segmentation fault (core dumped)
the uncomment output:
in init
in context
out context
out intit
Hello World
in context
out context
in context
out context
in context
Pulseaudio connection ready...out context
in sinkinfo
index: 0
help
name: alsa_output.pci-0000_05_00.6.analog-stereo
description: Family 17h (Models 10h-1fh) HD Audio Controller Analog Stereo
searchName: MySink
set gStreamout sink
in sinkinfo
index: 1
help
name: MySink
description: MySink
searchName: MySink
setting up peak recording using: MySink.monitor
settetd
Segmentation fault (core dumped)
In Ubuntu, I wrote a new system call:
SYSCALL_DEFINE1(print_other, pid_t, targetpid)
{
struct task_struct *p;
int found = 0;
for(p = &init_task; next_task(p) != &init_task; p=next_task(p))
{
if(p->pid == targetpid)
{
found = 1;
break;
}
}
if (found)
{
for(p = current; p != &init_task; p = p->parent)
{
printk("Task:\n");
printk("Process ID: %d\n", p->pid);
printk("Running state: %ld\n", p->state);
printk("Program name: %s\n", p->comm);
printk("Start time: %llu\n", p->start_time);
printk("Virtual runtime: %llu\n\n", p->se.vruntime);
}
}
else
{
printk("Your process was not found");
}
return 0;
}
This is my testing file:
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define __NR_print_other 337
int main(int argc, char *argv[])
{
char search[10];
char *error;
pid_t in_pid;
unsigned long pid;
while (true)
{
printf("Enter PID to search: ");
scanf("%s", search);
printf("passed scanf\n");
pid = strtoul(search, &error, 10);
printf("passed strtoul\n");
if (*error || error == argv[1] || ((pid_t)pid != pid ||
(pid_t)pid <= 0))
{
printf("in if statement\n");
printf("\nError: Invalid PID entered\n");
printf("Try again\n");
}
else
{
printf("in else statement\n");
in_pid = pid;
syscall(__NR_print_other, in_pid);
printf("about to return, in_pid = %d\n", in_pid);
return 0;
}
}
}
But the testing file is good. The system call is not doing anything and I cannot see why. What am I supposed to do that I am doing wrong?
I don't really have anything left to find. I checked the testing file and it runs properly. It returns that the in_pid is correct and runs the error bounds checks properly. There must be a logical error in the system call but I don't see what the problem would be.
Your system call is working and doing something. Just run dmesg and you should see something similar to this:
[ 3755.306897] Task:
[ 3755.306898] Process ID: 1
[ 3755.306899] Running state: 1
[ 3755.306900] Program name: systemd
[ 3755.306902] Start time: 371331827
[ 3755.306903] Virtual runtime: 1757840935
I'm new to stack overflow so bear with me :)
I am trying to create a custom linux shell as a project.
Right now I want to make the default output going to a file instead of console when the user uses > symbol for example ls > filename.txt
But , the program crashes and a Bad address error pops yet it writes the command output to the file.
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
typedef char* string;
int main (int argc,char** argv) {
int error = 0;
int posIn=0; int posOut=0; int appendMark=0;
string Output,Input;
while (1) {
error=0;
char progName[255];
printf("\nmysh3 > ");
if(fgets(progName,500,stdin)==NULL){return 0 ;}
char dir[1024];
string params[40];
string pch=progName;
int i=0;
while ((pch = strtok (pch," \n")) != NULL){
params[i]=pch;
if (strcmp(pch,"<")==0) { posIn = i;params[i]=NULL;i--;} // < is skipped and we put only the name of the command on the params array
if (strcmp(pch,">")==0) { posOut = i;params[i]=NULL;i--;} //output
if (strcmp(pch,">>")==0) {appendMark=1;params[i]=NULL;i--;}
i++;
pch = NULL;
}
params[i]=NULL;
if(strlen(progName)>255){
printf("The commands can't be over 255 characters\n");
return 0;
}
if (posOut) {
int out = open(params[posOut], O_CREAT|O_TRUNC|O_WRONLY, 0777);
params[posOut]=NULL;
if (out < 0) {
error = 1;
fprintf(stderr,"open error: %d [%s]\n",errno,strerror(errno));
exit(1); }
int k = dup2(out,1);
if (k<0) {
error = 1;
perror("Cannot redirect output");
}
close(out);
}
pid_t proccess1,waitpid;
int status;
proccess1 = fork();
if (proccess1<0) {perror("Out of memory"); } //Monos tropos na apotyxei h fork einai na mhn yparxei mnhmh
else if (proccess1==0) {
execvp(params[0],params);
if (!error) {perror("Unknown command");}
}
else {
waitpid=wait(&status);
if (waitpid==-1) {perror("ERROR: A NEW ZOMBIE IS BORN 3:)");return 0;}
}
}
return 0; }
The following basic SDL2 code taken from a tutorial website is causing some strange trouble :
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define SCREENH 768
#define SCREENW 1366
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Surface *windowSurface = NULL;
int init_SDL() {
int success = 0;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! ");
printf("SDL_Error: %s\n",SDL_GetError());
success = -1;
}
else {
window = SDL_CreateWindow("SDL2_Tutorial02",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREENW,SCREENH,SDL_WINDOW_SHOWN);
if(window == NULL) {
printf("Window could not be created! ");
printf("SDL Error: %s\n",SDL_GetError());
}
else {
screenSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
int loadMedia() {
int success = 0;
windowSurface = SDL_LoadBMP("Images/Hallo.bmp");
if(windowSurface == NULL) {
printf("Unable to load image! ");
printf("SDL Error: %s\n",SDL_GetError());
success = -1;
}
return success;
}
void close() {
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main(int argc,char *argv[]) {
assert(init_SDL() == 0);
assert(loadMedia() == 0);
SDL_BlitSurface(windowSurface,NULL,screenSurface,NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
close();
exit(EXIT_SUCCESS);
}
As soon as SDL_Quit(), placed in close(), is invoked I receive a memory access error. Using GDB the following is revealed:
49 SDL_Quit();
(gdb) n
Program received signal SIGBUS, Bus error.
0x00007ffff68a5895 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
(gdb)
The strange about that is when I place SDL_Quit() outside of close() like this:
void close() {
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
}
int main(int argc,char *argv[]) {
assert(init_SDL() == 0);
assert(loadMedia() == 0);
SDL_BlitSurface(windowSurface,NULL,screenSurface,NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
close();
SDL_Quit();
exit(EXIT_SUCCESS);
}
all things are fine. SDL_Quit() works without error. Why does it cause a SIGBUS Error when I invoke SDL_Quit() in another function ?
EDIT: This code was compiled on ubuntu 14.04 with gcc and the following compile command
gcc -g3 -o tutorial tutorial.c `sdl2-config --cflags --libs`
Your function close() is in conflict with an internal SDL function with the same name causing weird behavior (actually, it is the libc standard close() syscall called by SDL).
Rename your function and it should be fine.