segfault because of pointerarythmetic

This commit is contained in:
ccppi 2024-09-05 14:48:23 +02:00
parent 43146c7596
commit 8c4815cd7e

30
main.c
View File

@ -1,4 +1,5 @@
#include<stdio.h>
#include<stdlib.h>
#include<curl/curl.h>
#include<string.h>
#include<dbus/dbus.h>
@ -6,7 +7,7 @@
int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *,char *, char *);
int checkIsActive(DBusConnection *connection, char *service_name);
int systemdUnitCall(DBusConnection *connection,char *method,char *param);
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result);
/*
*sytemctl show gotify-server | grep PID
* */
@ -20,6 +21,7 @@ int main(void)
dbus_bool_t allow_user=0;
DBusConnection *connection=NULL;
DBusError error;
char *result;
dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SESSION,&error);//DBUS_BUS_SYSTEM
@ -67,7 +69,9 @@ int main(void)
//systemdCall(connection,"GetUnit","timers.target");
//systemdCall(connection,"ListUnits",NULL);
systemdUnitCall(connection,"Get","org.freedesktop.systemd1.Unit");
systemdUnitCall(connection,"/org/freedesktop/systemd1/unit/timers_2etarget","Get","SubState",&result);
systemdUnitCall(connection,"/org/freedesktop/systemd1/unit/sysinit_2etarget","Get","SubState",&result);
printf("result systemdUnitCall: %s\n",result);
gotify_message("https://5ccppi.org:4433/message?token=ApPFbwayM5zDXf3","anothertitle","anothermessage");
return 0;
@ -225,7 +229,7 @@ int systemdCall(DBusConnection *connection,char *method,char *param)
return 0;
}
int systemdUnitCall(DBusConnection *connection,char *method,char *param)
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result)
{
DBusError error;
@ -233,15 +237,16 @@ int systemdUnitCall(DBusConnection *connection,char *method,char *param)
DBusMessage *ret_msg;
DBusMessageIter iter;
DBusMessageIter array_iter;
DBusMessage *value;
DBusMessageIter variant_iter;
result=NULL;
char *reply_msg=NULL;
int ret1,ret2;
char *param2 = "org.freedesktop.systemd1.Unit";
dbus_error_init(&error);
//dest(servcice/bus) ,path(object) ,iface method
msg = dbus_message_new_method_call("org.freedesktop.systemd1","/org/freedesktop/systemd1/unit/timers_2etarget","org.freedesktop.DBus.Properties",method);
msg = dbus_message_new_method_call("org.freedesktop.systemd1",target,"org.freedesktop.DBus.Properties",method);
if(NULL == msg)
{
fprintf(stderr,"Message null, no message created\n");
@ -253,10 +258,10 @@ int systemdUnitCall(DBusConnection *connection,char *method,char *param)
}
if(param != NULL)
if(param2 != NULL)
{
dbus_message_iter_init_append(msg,&iter);
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param))
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param2))
{
fprintf(stderr,"Error appending");
return -1;
@ -264,8 +269,7 @@ int systemdUnitCall(DBusConnection *connection,char *method,char *param)
else{
printf("message appended\n");
}
char *param2="LoadState";
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param2))
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param))
{
fprintf(stderr,"Error appending");
return -1;
@ -288,7 +292,7 @@ int systemdUnitCall(DBusConnection *connection,char *method,char *param)
printf("ret1 type:%c\n",ret1);
while(ret1 != DBUS_TYPE_INVALID)
{
if(ret1 == *DBUS_TYPE_OBJECT_PATH_AS_STRING)
if(ret1 == DBUS_TYPE_OBJECT_PATH)
{
printf("succesfull received message\narg type %c\n",ret1);
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
@ -307,9 +311,9 @@ int systemdUnitCall(DBusConnection *connection,char *method,char *param)
printf("str_val:%s\n",type);
if(type[0] == DBUS_TYPE_STRING)
{
char *str_val;
dbus_message_iter_get_basic(&variant_iter,&str_val);
printf("str_val:%s\n",str_val);
// result = malloc(sizeof(char)*64);
dbus_message_iter_get_basic(&variant_iter,*result);
// printf("result inside function:%s\n",*result);
}
//dbus_message_iter_get_basic(&reply_iter,&value);