get list of services

This commit is contained in:
ccppi 2024-09-04 14:52:19 +02:00
parent 899c001afb
commit 8d0dbec756

95
main.c
View File

@ -4,7 +4,7 @@
#include<dbus/dbus.h> #include<dbus/dbus.h>
int gotify_message(char *,char *,char *); int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *); int systemdCall(DBusConnection *m,char *, char *);
/* /*
*sytemctl show gotify-server | grep PID *sytemctl show gotify-server | grep PID
* */ * */
@ -63,7 +63,8 @@ int main(void)
} }
//org/freedesktop/systemd1 //org/freedesktop/systemd1
systemdCall(connection); //systemdCall(connection,"GetUnit","timers.target");
systemdCall(connection,"ListUnits",NULL);
gotify_message("https://5ccppi.org:4433/message?token=ApPFbwayM5zDXf3","anothertitle","anothermessage"); gotify_message("https://5ccppi.org:4433/message?token=ApPFbwayM5zDXf3","anothertitle","anothermessage");
@ -105,24 +106,21 @@ int gotify_message(char *url,char *title,char *message)
return 0; return 0;
} }
int systemdCall(DBusConnection *connection) int systemdCall(DBusConnection *connection,char *method,char *param)
{ {
DBusError error; DBusError error;
DBusMessage *msg; DBusMessage *msg;
DBusMessage *ret_msg; DBusMessage *ret_msg;
DBusMessageIter iter; DBusMessageIter iter;
DBusMessageIter array_iter;
char *reply_msg=NULL; char *reply_msg=NULL;
char *param="timers.target"; int ret1,ret2;
int ret1;
dbus_error_init(&error); dbus_error_init(&error);
msg = dbus_message_new_method_call("org.freedesktop.systemd1","/org/freedesktop/systemd1","org.freedesktop.systemd1.Manager",method);
msg = dbus_message_new_method_call("org.freedesktop.systemd1","/org/freedesktop/systemd1","org.freedesktop.systemd1.Manager","GetUnit");
if(NULL == msg) if(NULL == msg)
{ {
fprintf(stderr,"Message null, no message created"); fprintf(stderr,"Message null, no message created");
@ -133,15 +131,20 @@ int systemdCall(DBusConnection *connection)
printf("message created!\n"); printf("message created!\n");
} }
dbus_message_iter_init_append(msg,&iter);
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param)) if(param != NULL)
{ {
fprintf(stderr,"Error appending"); dbus_message_iter_init_append(msg,&iter);
return -1; if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param))
} {
else{ fprintf(stderr,"Error appending");
printf("message appended"); return -1;
}
else{
printf("message appended\n");
}
} }
ret_msg = dbus_connection_send_with_reply_and_block(connection,msg,DBUS_TIMEOUT_INFINITE,&error); ret_msg = dbus_connection_send_with_reply_and_block(connection,msg,DBUS_TIMEOUT_INFINITE,&error);
if(ret_msg == NULL) if(ret_msg == NULL)
{ {
@ -152,11 +155,63 @@ int systemdCall(DBusConnection *connection)
DBusMessageIter reply_iter; DBusMessageIter reply_iter;
dbus_message_iter_init(ret_msg,&reply_iter); dbus_message_iter_init(ret_msg,&reply_iter);
ret1=dbus_message_iter_get_arg_type(&reply_iter); ret1=dbus_message_iter_get_arg_type(&reply_iter);
if(ret1 == *DBUS_TYPE_OBJECT_PATH_AS_STRING)
while(ret1 != DBUS_TYPE_INVALID)
{ {
printf("succesfull received message\narg type %c\n",ret1); if(ret1 == *DBUS_TYPE_OBJECT_PATH_AS_STRING)
dbus_message_iter_get_basic(&reply_iter,&reply_msg); {
printf("returned value: %s\n",reply_msg); 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(ret1 == DBUS_TYPE_STRING)
{
printf("succesfull received message\narg type %c\n",ret1);
dbus_message_iter_get_basic(&reply_iter,&reply_msg);
printf("Received message:%s\n",reply_msg);
}
if(ret1 == DBUS_TYPE_ARRAY)
{
int element_count = dbus_message_iter_get_element_count(&reply_iter);
printf("Array:Elem count %d\n",element_count);
//ret2 = dbus_message_iter_get_element_type(&reply_iter);
dbus_message_iter_recurse(&reply_iter,&array_iter);
int i2=0;
ret2 = dbus_message_iter_get_arg_type(&array_iter);
while(ret2 != DBUS_TYPE_INVALID)
{
//printf("type:%c\n",ret2);
//dbus_message_iter_get_basic(&array_iter,&reply_msg);
//printf("Received message:%s\n",reply_msg);
ret2 = dbus_message_iter_get_arg_type(&array_iter);
DBusMessageIter sub_iter;
int ret3;
dbus_message_iter_recurse(&array_iter,&sub_iter);
ret3 = dbus_message_iter_get_arg_type(&sub_iter);
while(ret3 != DBUS_TYPE_INVALID)
{
ret3 = dbus_message_iter_get_arg_type(&sub_iter);
//printf("arr_sub_iter:%c\n",ret3);
if(ret3 == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&sub_iter,&reply_msg);
printf("-->%s\n",reply_msg);
}
ret3=dbus_message_iter_next(&sub_iter);
}
ret2=dbus_message_iter_next(&array_iter);
i2++;
//printf("i2:%d\n",i2);
}
}
//printf("iter:%c\n",ret1);
ret1=dbus_message_iter_next(&reply_iter);
} }
} }