implement test get method on systemd unit file

This commit is contained in:
ccppi 2024-09-05 11:56:24 +02:00
parent 8d0dbec756
commit 43146c7596

155
main.c
View File

@ -4,7 +4,9 @@
#include<dbus/dbus.h>
int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *m,char *, char *);
int systemdCall(DBusConnection *,char *, char *);
int checkIsActive(DBusConnection *connection, char *service_name);
int systemdUnitCall(DBusConnection *connection,char *method,char *param);
/*
*sytemctl show gotify-server | grep PID
* */
@ -19,7 +21,6 @@ int main(void)
DBusConnection *connection=NULL;
DBusError error;
dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SESSION,&error);//DBUS_BUS_SYSTEM
if(dbus_error_is_set(&error))
@ -64,8 +65,9 @@ int main(void)
//org/freedesktop/systemd1
//systemdCall(connection,"GetUnit","timers.target");
systemdCall(connection,"ListUnits",NULL);
//systemdCall(connection,"ListUnits",NULL);
systemdUnitCall(connection,"Get","org.freedesktop.systemd1.Unit");
gotify_message("https://5ccppi.org:4433/message?token=ApPFbwayM5zDXf3","anothertitle","anothermessage");
return 0;
@ -148,7 +150,7 @@ int systemdCall(DBusConnection *connection,char *method,char *param)
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");
fprintf(stderr,"error on receiving message from peer\n");
}
else
{
@ -198,7 +200,7 @@ int systemdCall(DBusConnection *connection,char *method,char *param)
if(ret3 == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&sub_iter,&reply_msg);
printf("-->%s\n",reply_msg);
printf("%s\n",reply_msg);
}
ret3=dbus_message_iter_next(&sub_iter);
}
@ -217,8 +219,149 @@ int systemdCall(DBusConnection *connection,char *method,char *param)
}
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s",error.message);
fprintf(stderr,"%s\n",error.message);
return -1;
}
return 0;
}
int systemdUnitCall(DBusConnection *connection,char *method,char *param)
{
DBusError error;
DBusMessage *msg;
DBusMessage *ret_msg;
DBusMessageIter iter;
DBusMessageIter array_iter;
DBusMessage *value;
DBusMessageIter variant_iter;
char *reply_msg=NULL;
int ret1,ret2;
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);
if(NULL == msg)
{
fprintf(stderr,"Message null, no message created\n");
return 1;
}
else
{
printf("message created!\n");
}
if(param != NULL)
{
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\n");
}
char *param2="LoadState";
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&param2))
{
fprintf(stderr,"Error appending");
return -1;
}
else{
printf("message appended\n");
}
}
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);
printf("ret1 type:%c\n",ret1);
while(ret1 != DBUS_TYPE_INVALID)
{
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(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_VARIANT)
{
dbus_message_iter_recurse(&reply_iter,&variant_iter);
char *type = dbus_message_iter_get_signature(&variant_iter);
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);
}
//dbus_message_iter_get_basic(&reply_iter,&value);
}
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)
{
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);
}
}
if(dbus_error_is_set(&error))
{
fprintf(stderr,"%s\n",error.message);
return -1;
}
return 0;
}
/*
int checkIsActive(DBusConnection *connection, char *service_name)
{
systemdCall(connection,
}*/