first systemd call via dbus

This commit is contained in:
ccppi 2024-09-04 13:34:10 +02:00
parent 245ee35af5
commit 899c001afb

100
main.c
View File

@ -4,6 +4,7 @@
#include<dbus/dbus.h>
int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *);
/*
*sytemctl show gotify-server | grep PID
* */
@ -11,26 +12,46 @@ int gotify_message(char *,char *,char *);
int main(void)
{
unsigned long uid;
int ret;
int ret,i;
char **objects = NULL;
dbus_bool_t allow_user=0;
DBusConnection *connection=NULL;
DBusError error;
dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SYSTEM,&error);
connection = dbus_bus_get(DBUS_BUS_SESSION,&error);//DBUS_BUS_SYSTEM
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s",error.message);
return -1;
}
printf("This is my unique dbus name: %s\n",dbus_bus_get_unique_name(connection));
ret = dbus_bus_request_name(connection,"test.method.client",DBUS_NAME_FLAG_REPLACE_EXISTING,&error);
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s",error.message);
return -1;
}
else
{
printf("name requested from dbus: %d\n",ret);
}
dbus_connection_set_allow_anonymous(connection,allow_user);
printf("user auth dbus requ: %d\n",allow_user);
dbus_connection_list_registered(connection,"/",
if(!dbus_connection_list_registered(connection,"/",&objects))
{
for(i=0;objects[i];i++)
{
printf("\nobject dbus: %s",objects[i]);
}
}
else{
printf("dbus_connection_list_registered error\n");
}
if((ret=dbus_connection_get_unix_user(connection,&uid))==1)
{
@ -41,7 +62,11 @@ int main(void)
printf("NO userID set in dbus session: %d\n",ret);
}
//org/freedesktop/systemd1
systemdCall(connection);
gotify_message("https://5ccppi.org:4433/message?token=ApPFbwayM5zDXf3","anothertitle","anothermessage");
return 0;
}
@ -80,4 +105,65 @@ int gotify_message(char *url,char *title,char *message)
return 0;
}
int systemdCall(DBusConnection *connection)
{
DBusError error;
DBusMessage *msg;
DBusMessage *ret_msg;
DBusMessageIter iter;
char *reply_msg=NULL;
char *param="timers.target";
int ret1;
dbus_error_init(&error);
msg = dbus_message_new_method_call("org.freedesktop.systemd1","/org/freedesktop/systemd1","org.freedesktop.systemd1.Manager","GetUnit");
if(NULL == msg)
{
fprintf(stderr,"Message null, no message created");
return 1;
}
else
{
printf("message created!\n");
}
dbus_message_iter_init_append(msg,&iter);
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param))
{
fprintf(stderr,"Error appending");
return -1;
}
else{
printf("message appended");
}
ret_msg = dbus_connection_send_with_reply_and_block(connection,msg,DBUS_TIMEOUT_INFINITE,&error);
if(ret_msg == NULL)
{
fprintf(stderr,"error ond receiving message from peer\n");
}
else
{
DBusMessageIter reply_iter;
dbus_message_iter_init(ret_msg,&reply_iter);
ret1=dbus_message_iter_get_arg_type(&reply_iter);
if(ret1 == *DBUS_TYPE_OBJECT_PATH_AS_STRING)
{
printf("succesfull received message\narg type %c\n",ret1);
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
printf("returned value: %s\n",reply_msg);
}
}
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s",error.message);
return -1;
}
return 0;
}