/
mender-troubleshoot.c
1684 lines (1429 loc) · 59.7 KB
/
mender-troubleshoot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file mender-troubleshoot.c
* @brief Mender MCU Troubleshoot add-on implementation
*
* MIT License
*
* Copyright (c) 2022-2023 joelguittet and mender-mcu-client contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "mender-api.h"
#include "mender-client.h"
#include "mender-inventory.h"
#include "mender-troubleshoot.h"
#include "mender-log.h"
#include "mender-rtos.h"
#ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT
#include <msgpack.h>
/**
* @brief Default troubleshoot healthcheck interval (seconds)
*/
#ifndef CONFIG_MENDER_CLIENT_TROUBLESHOOT_HEALTHCHECK_INTERVAL
#define CONFIG_MENDER_CLIENT_TROUBLESHOOT_HEALTHCHECK_INTERVAL (30)
#endif /* CONFIG_MENDER_CLIENT_TROUBLESHOOT_HEALTHCHECK_INTERVAL */
/**
* msgpack zone chunk initialization size
*/
#define MENDER_TROUBLESHOOT_ZONE_CHUNK_INIT_SIZE (2048)
/**
* msgpack sbuffer initialization size
*/
#define MENDER_TROUBLESHOOT_SBUFFER_INIT_SIZE (256)
/**
* Proto type
*/
typedef enum {
MENDER_TROUBLESHOOT_PROTO_TYPE_INVALID = 0x0000, /**< Invalid */
MENDER_TROUBLESHOOT_PROTO_TYPE_SHELL = 0x0001, /**< Shell */
MENDER_TROUBLESHOOT_PROTO_TYPE_FILE_TRANSFER = 0x0002, /**< File transfer */
MENDER_TROUBLESHOOT_PROTO_TYPE_PORT_FORWARD = 0x0003, /**< Port forward */
MENDER_TROUBLESHOOT_PROTO_TYPE_MENDER_CLIENT = 0x0004, /**< Mender client */
MENDER_TROUBLESHOOT_PROTO_TYPE_CONTROL = 0xFFFF /**< Control */
} mender_troubleshoot_protohdr_type_t;
/**
* Message type
*/
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_PING "ping"
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_PONG "pong"
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_RESIZE "resize"
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_SHELL "shell"
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_SPAWN "new"
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_STOP "stop"
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_MENDER_CLIENT_CHECK_UPDATE "check-update"
#define MENDER_TROUBLESHOOT_MESSAGE_TYPE_MENDER_CLIENT_SEND_INVENTORY "send-inventory"
/**
* Status type
*/
typedef enum {
MENDER_TROUBLESHOOT_STATUS_TYPE_NORMAL = 0x0001, /**< Normal message */
MENDER_TROUBLESHOOT_STATUS_TYPE_ERROR = 0x0002, /**< Error message */
MENDER_TROUBLESHOOT_STATUS_TYPE_CONTROL = 0x0003 /**< Control message */
} mender_troubleshoot_properties_status_t;
/**
* Proto message header properties
*/
typedef struct {
uint16_t * terminal_width; /**< Terminal width */
uint16_t * terminal_height; /**< Terminal heigth */
char * user_id; /**< User ID */
uint32_t * timeout; /**< Timeout */
mender_troubleshoot_properties_status_t *status; /**< Status */
} mender_troubleshoot_protohdr_properties_t;
/**
* Proto message header
*/
typedef struct {
mender_troubleshoot_protohdr_type_t proto; /**< Proto type */
char * typ; /**< Message type */
char * sid; /**< Session ID */
mender_troubleshoot_protohdr_properties_t *properties; /**< Properties */
} mender_troubleshoot_protohdr_t;
/**
* Proto message
*/
typedef struct {
mender_troubleshoot_protohdr_t *protohdr; /**< Header */
char * body; /**< Body */
} mender_troubleshoot_protomsg_t;
/**
* @brief Mender troubleshoot configuration
*/
static mender_troubleshoot_config_t mender_troubleshoot_config;
/**
* @brief Mender troubleshoot callbacks
*/
static mender_troubleshoot_callbacks_t mender_troubleshoot_callbacks;
/**
* @brief Mender troubleshoot work handle
*/
static void *mender_troubleshoot_healthcheck_work_handle = NULL;
/**
* @brief Mender troubleshoot connection handle
*/
static void *mender_troubleshoot_handle = NULL;
/**
* @brief Mender troubleshoot shell session ID
*/
static char *mender_troubleshoot_shell_sid = NULL;
/**
* @brief Mender troubleshoot healthcheck work function
* @return MENDER_OK if the function succeeds, error code otherwise
*/
static mender_err_t mender_troubleshoot_healthcheck_work_function(void);
/**
* @brief Callback function to be invoked to perform the treatment of the data from the websocket
* @param data Received data
* @param length Received data length
* @return MENDER_OK if the function succeeds, error code if an error occured
*/
static mender_err_t mender_troubleshoot_data_received_callback(void *data, size_t length);
/**
* @brief Function called to perform the treatment of the shell messages
* @param protomsg Received proto message
* @param response Response to be sent back to the server, NULL if no response to send
* @return MENDER_OK if the function succeeds, error code if an error occured
*/
static mender_err_t mender_troubleshoot_shell_message_handler(mender_troubleshoot_protomsg_t *protomsg, mender_troubleshoot_protomsg_t **response);
/**
* @brief Function called to perform the treatment of the mender-client messages
* @param protomsg Received proto message
* @param response Response to be sent back to the server, NULL if no response to send
* @return MENDER_OK if the function succeeds, error code if an error occured
*/
static mender_err_t mender_troubleshoot_mender_client_message_handler(mender_troubleshoot_protomsg_t *protomsg, mender_troubleshoot_protomsg_t **response);
/**
* @brief Function used to format acknowledgment messages
* @param protomsg Received proto message
* @param sid Session ID
* @param status Status
* @param response Response to be sent back to the server, NULL if no response to send
* @return MENDER_OK if the function succeeds, error code if an error occured
*/
static mender_err_t mender_troubleshoot_format_acknowledgment(mender_troubleshoot_protomsg_t * protomsg,
char * sid,
mender_troubleshoot_properties_status_t status,
mender_troubleshoot_protomsg_t ** response);
/**
* @brief Function called to send shell ping protomsg
* @return MENDER_OK if the function succeeds, error code if an error occured
*/
static mender_err_t mender_troubleshoot_send_shell_ping_protomsg(void);
/**
* @brief Function called to send shell stop protomsg
* @return MENDER_OK if the function succeeds, error code if an error occured
*/
static mender_err_t mender_troubleshoot_send_shell_stop_protomsg(void);
/**
* @brief Unpack and decode Proto message
* @param data Packed data to be decoded
* @param length Length of the data to be decoded
* @return Proto message if the function succeeds, NULL otherwise
*/
static mender_troubleshoot_protomsg_t *mender_troubleshoot_unpack_protomsg(void *data, size_t length);
/**
* @brief Decode Proto message object
* @param object Proto message object
* @return Proto message if the function succeeds, NULL otherwise
*/
static mender_troubleshoot_protomsg_t *mender_troubleshoot_decode_protomsg(msgpack_object *object);
/**
* @brief Decode Proto header object
* @param object Proto header object
* @return Proto header if the function succeeds, NULL otherwise
*/
static mender_troubleshoot_protohdr_t *mender_troubleshoot_decode_protohdr(msgpack_object *object);
/**
* @brief Decode Proto header properties object
* @param object Proto header properties object
* @return Proto header properties if the function succeeds, NULL otherwise
*/
static mender_troubleshoot_protohdr_properties_t *mender_troubleshoot_decode_protohdr_properties(msgpack_object *object);
/**
* @brief Decode body object
* @param object Body object
* @return Body if the function succeeds, NULL otherwise
*/
static char *mender_troubleshoot_decode_body(msgpack_object *object);
/**
* @brief Encode and pack Proto message
* @param protomsg Proto message
* @param data Packed data encoded
* @param length Length of the data encoded
* @return MENDER_OK if the function succeeds, error code otherwise
*/
static mender_err_t mender_troubleshoot_pack_protomsg(mender_troubleshoot_protomsg_t *protomsg, void **data, size_t *length);
/**
* @brief Encode Proto message
* @param protomsg Proto message
* @param p Object key-value
* @return MENDER_OK if the function succeeds, error code otherwise
*/
static mender_err_t mender_troubleshoot_encode_protomsg(mender_troubleshoot_protomsg_t *protomsg, msgpack_object *object);
/**
* @brief Encode Proto header
* @param protohdr Proto header
* @param p Object key-value
* @return MENDER_OK if the function succeeds, error code otherwise
*/
static mender_err_t mender_troubleshoot_encode_protohdr(mender_troubleshoot_protohdr_t *protohdr, msgpack_object_kv *p);
/**
* @brief Encode Proto header properties
* @param properties Proto header properties
* @param p Object key-value
* @return MENDER_OK if the function succeeds, error code otherwise
*/
static mender_err_t mender_troubleshoot_encode_protohdr_properties(mender_troubleshoot_protohdr_properties_t *properties, msgpack_object_kv *p);
/**
* @brief Encode body
* @param body Body
* @param p Object key-value
* @return MENDER_OK if the function succeeds, error code otherwise
*/
static mender_err_t mender_troubleshoot_encode_body(char *body, msgpack_object_kv *p);
/**
* @brief Release msgpack object
* @param object msgpack object
*/
static void mender_troubleshoot_msgpack_object_release(msgpack_object *object);
/**
* @brief Release Proto message
* @param protomsg Proto message
*/
static void mender_troubleshoot_release_protomsg(mender_troubleshoot_protomsg_t *protomsg);
/**
* @brief Release Proto header
* @param protohdr Proto header
*/
static void mender_troubleshoot_release_protohdr(mender_troubleshoot_protohdr_t *protohdr);
/**
* @brief Release Proto header properties
* @param properties Proto header properties
*/
static void mender_troubleshoot_release_protohdr_properties(mender_troubleshoot_protohdr_properties_t *properties);
mender_err_t
mender_troubleshoot_init(mender_troubleshoot_config_t *config, mender_troubleshoot_callbacks_t *callbacks) {
assert(NULL != config);
mender_err_t ret;
/* Save configuration */
if (0 != config->healthcheck_interval) {
mender_troubleshoot_config.healthcheck_interval = config->healthcheck_interval;
} else {
mender_troubleshoot_config.healthcheck_interval = CONFIG_MENDER_CLIENT_TROUBLESHOOT_HEALTHCHECK_INTERVAL;
}
/* Save callbacks */
memcpy(&mender_troubleshoot_callbacks, callbacks, sizeof(mender_troubleshoot_callbacks_t));
/* Create troubleshoot healthcheck work */
mender_rtos_work_params_t healthcheck_work_params;
healthcheck_work_params.function = mender_troubleshoot_healthcheck_work_function;
healthcheck_work_params.period = mender_troubleshoot_config.healthcheck_interval;
healthcheck_work_params.name = "mender_troubleshoot_healthcheck";
if (MENDER_OK != (ret = mender_rtos_work_create(&healthcheck_work_params, &mender_troubleshoot_healthcheck_work_handle))) {
mender_log_error("Unable to create healthcheck work");
return ret;
}
return ret;
}
mender_err_t
mender_troubleshoot_activate(void) {
mender_err_t ret;
/* Activate troubleshoot healthcheck work */
if (MENDER_OK != (ret = mender_rtos_work_activate(mender_troubleshoot_healthcheck_work_handle))) {
mender_log_error("Unable to activate troubleshoot healthcheck work");
return ret;
}
return ret;
}
mender_err_t
mender_troubleshoot_deactivate(void) {
mender_err_t ret = MENDER_OK;
/* Deactivate troubleshoot healthcheck work */
mender_rtos_work_deactivate(mender_troubleshoot_healthcheck_work_handle);
/* Check if a session is already opened */
if (NULL != mender_troubleshoot_shell_sid) {
/* Invoke shell end callback */
if (NULL != mender_troubleshoot_callbacks.shell_end) {
if (MENDER_OK != (ret = mender_troubleshoot_callbacks.shell_end())) {
mender_log_error("An error occured");
}
}
}
/* Check if connection is established */
if (NULL != mender_troubleshoot_handle) {
/* Check if a session is already opened */
if (NULL != mender_troubleshoot_shell_sid) {
/* Send stop message to the server */
if (MENDER_OK != (ret = mender_troubleshoot_send_shell_stop_protomsg())) {
mender_log_error("Unable to send stop message to the server");
}
}
/* Disconnect the device of the server */
if (MENDER_OK != (ret = mender_api_troubleshoot_disconnect(mender_troubleshoot_handle))) {
mender_log_error("Unable to disconnect the device of the server");
}
mender_troubleshoot_handle = NULL;
}
/* Release session ID */
if (NULL != mender_troubleshoot_shell_sid) {
free(mender_troubleshoot_shell_sid);
mender_troubleshoot_shell_sid = NULL;
}
return ret;
}
mender_err_t
mender_troubleshoot_shell_print(uint8_t *data, size_t length) {
assert(NULL != data);
mender_troubleshoot_protomsg_t *protomsg = NULL;
mender_err_t ret = MENDER_OK;
void * payload = NULL;
/* Check if a session is already opened */
if (NULL == mender_troubleshoot_shell_sid) {
mender_log_error("No shell session opened");
ret = MENDER_FAIL;
goto FAIL;
}
/* Send shell body */
if (NULL == (protomsg = (mender_troubleshoot_protomsg_t *)malloc(sizeof(mender_troubleshoot_protomsg_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg, 0, sizeof(mender_troubleshoot_protomsg_t));
if (NULL == (protomsg->protohdr = (mender_troubleshoot_protohdr_t *)malloc(sizeof(mender_troubleshoot_protohdr_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg->protohdr, 0, sizeof(mender_troubleshoot_protohdr_t));
protomsg->protohdr->proto = MENDER_TROUBLESHOOT_PROTO_TYPE_SHELL;
if (NULL == (protomsg->protohdr->typ = strdup(MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_SHELL))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
if (NULL == (protomsg->protohdr->sid = strdup(mender_troubleshoot_shell_sid))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
if (NULL == (protomsg->protohdr->properties = (mender_troubleshoot_protohdr_properties_t *)malloc(sizeof(mender_troubleshoot_protohdr_properties_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg->protohdr->properties, 0, sizeof(mender_troubleshoot_protohdr_properties_t));
if (NULL == (protomsg->protohdr->properties->status = (mender_troubleshoot_properties_status_t *)malloc(sizeof(mender_troubleshoot_properties_status_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
*(protomsg->protohdr->properties->status) = MENDER_TROUBLESHOOT_STATUS_TYPE_NORMAL;
if (NULL == (protomsg->body = strndup((char *)data, length))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
/* Encode and pack the message */
if (MENDER_OK != (ret = mender_troubleshoot_pack_protomsg(protomsg, &payload, &length))) {
mender_log_error("Unable to encode message");
goto FAIL;
}
/* Send message */
if (MENDER_OK != (ret = mender_api_troubleshoot_send(mender_troubleshoot_handle, payload, length))) {
mender_log_error("Unable to send message");
goto FAIL;
}
FAIL:
/* Release memory */
mender_troubleshoot_release_protomsg(protomsg);
if (NULL != payload) {
free(payload);
}
return ret;
}
mender_err_t
mender_troubleshoot_exit(void) {
/* Delete troubleshoot healthcheck work */
mender_rtos_work_delete(mender_troubleshoot_healthcheck_work_handle);
mender_troubleshoot_healthcheck_work_handle = NULL;
/* Release memory */
if (NULL != mender_troubleshoot_shell_sid) {
free(mender_troubleshoot_shell_sid);
mender_troubleshoot_shell_sid = NULL;
}
mender_troubleshoot_config.healthcheck_interval = 0;
return MENDER_OK;
}
static mender_err_t
mender_troubleshoot_healthcheck_work_function(void) {
mender_err_t ret = MENDER_OK;
/* Check if connection is established */
if (NULL != mender_troubleshoot_handle) {
/* Check if a session is already opened */
if (NULL != mender_troubleshoot_shell_sid) {
/* Send healthcheck ping message over websocket connection */
if (MENDER_OK != (ret = mender_troubleshoot_send_shell_ping_protomsg())) {
mender_log_error("Unable to send healthcheck message to the server");
goto FAIL;
}
}
} else {
/* Connect the device to the server */
if (MENDER_OK != (ret = mender_api_troubleshoot_connect(&mender_troubleshoot_data_received_callback, &mender_troubleshoot_handle))) {
mender_log_error("Unable to connect the device to the server");
goto END;
}
}
goto END;
FAIL:
/* Check if a session is already opened */
if (NULL != mender_troubleshoot_shell_sid) {
/* Invoke shell end callback */
if (NULL != mender_troubleshoot_callbacks.shell_end) {
if (MENDER_OK != (ret = mender_troubleshoot_callbacks.shell_end())) {
mender_log_error("An error occured");
}
}
}
/* Check if connection is established */
if (NULL != mender_troubleshoot_handle) {
/* Disconnect the device of the server */
if (MENDER_OK != (ret = mender_api_troubleshoot_disconnect(mender_troubleshoot_handle))) {
mender_log_error("Unable to disconnect the device of the server");
}
mender_troubleshoot_handle = NULL;
}
/* Release session ID */
if (NULL != mender_troubleshoot_shell_sid) {
free(mender_troubleshoot_shell_sid);
mender_troubleshoot_shell_sid = NULL;
}
END:
return ret;
}
static mender_err_t
mender_troubleshoot_data_received_callback(void *data, size_t length) {
assert(NULL != data);
mender_err_t ret = MENDER_OK;
mender_troubleshoot_protomsg_t *protomsg;
mender_troubleshoot_protomsg_t *response = NULL;
void * payload = NULL;
/* Unpack and decode message */
if (NULL == (protomsg = mender_troubleshoot_unpack_protomsg(data, length))) {
mender_log_error("Unable to decode message");
ret = MENDER_FAIL;
goto END;
}
/* Verify integrity of the message */
if (NULL == protomsg->protohdr) {
mender_log_error("Invalid message received");
ret = MENDER_FAIL;
goto END;
}
/* Treatment of the message depending of the proto type */
switch (protomsg->protohdr->proto) {
case MENDER_TROUBLESHOOT_PROTO_TYPE_INVALID:
mender_log_error("Invalid message received");
ret = MENDER_FAIL;
break;
case MENDER_TROUBLESHOOT_PROTO_TYPE_SHELL:
ret = mender_troubleshoot_shell_message_handler(protomsg, &response);
break;
case MENDER_TROUBLESHOOT_PROTO_TYPE_MENDER_CLIENT:
ret = mender_troubleshoot_mender_client_message_handler(protomsg, &response);
break;
case MENDER_TROUBLESHOOT_PROTO_TYPE_FILE_TRANSFER:
case MENDER_TROUBLESHOOT_PROTO_TYPE_PORT_FORWARD:
case MENDER_TROUBLESHOOT_PROTO_TYPE_CONTROL:
default:
mender_log_error("Unsupported message received with proto type 0x%04x", protomsg->protohdr->proto);
ret = MENDER_FAIL;
break;
}
/* Check if response is available */
if (NULL != response) {
/* Encode and pack the message */
if (MENDER_OK != (ret = mender_troubleshoot_pack_protomsg(response, &payload, &length))) {
mender_log_error("Unable to encode response");
goto END;
}
/* Send response */
if (MENDER_OK != (ret = mender_api_troubleshoot_send(mender_troubleshoot_handle, payload, length))) {
mender_log_error("Unable to send response");
goto END;
}
}
END:
/* Release memory */
mender_troubleshoot_release_protomsg(protomsg);
mender_troubleshoot_release_protomsg(response);
if (NULL != payload) {
free(payload);
}
return ret;
}
static mender_err_t
mender_troubleshoot_shell_message_handler(mender_troubleshoot_protomsg_t *protomsg, mender_troubleshoot_protomsg_t **response) {
assert(NULL != protomsg);
assert(NULL != protomsg->protohdr);
mender_err_t ret = MENDER_OK;
/* Verify integrity of the message */
if ((NULL == protomsg->protohdr->typ) || (NULL == protomsg->protohdr->sid)) {
mender_log_error("Invalid message received");
ret = MENDER_FAIL;
goto END;
}
/* Treatment of the message depending of the message type */
if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_PING)) {
/* Nothing to do */
} else if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_PONG)) {
/* Nothing to do */
} else if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_RESIZE)) {
/* Verify integrity of the message */
if ((NULL == protomsg->protohdr->properties) || (NULL == protomsg->protohdr->properties->terminal_width)
|| (NULL == protomsg->protohdr->properties->terminal_height)) {
mender_log_error("Invalid message received");
ret = MENDER_FAIL;
goto END;
}
/* Invoke shell resize callback */
if (NULL != mender_troubleshoot_callbacks.shell_resize) {
if (MENDER_OK
!= (ret = mender_troubleshoot_callbacks.shell_resize(*protomsg->protohdr->properties->terminal_width,
*protomsg->protohdr->properties->terminal_height))) {
mender_log_error("An error occured");
goto FAIL;
}
}
} else if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_SHELL)) {
/* Verify integrity of the message */
if (NULL == protomsg->body) {
mender_log_error("Invalid message received");
ret = MENDER_FAIL;
goto END;
}
/* Invoke shell data write callback */
if (NULL != mender_troubleshoot_callbacks.shell_write) {
if (MENDER_OK != (ret = mender_troubleshoot_callbacks.shell_write((uint8_t *)protomsg->body, strlen(protomsg->body)))) {
mender_log_error("An error occured");
goto FAIL;
}
}
} else if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_SPAWN)) {
/* Check is a session is already opened */
if (NULL != mender_troubleshoot_shell_sid) {
mender_log_warning("A shell session is already opened");
goto END;
}
/* Start shell session */
mender_log_info("Starting a new shell session");
/* Save the session ID */
if (NULL == (mender_troubleshoot_shell_sid = strdup(protomsg->protohdr->sid))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
/* Format acknowledgment */
if (MENDER_OK
!= (ret
= mender_troubleshoot_format_acknowledgment(protomsg,
mender_troubleshoot_shell_sid,
(MENDER_OK == ret) ? MENDER_TROUBLESHOOT_STATUS_TYPE_NORMAL : MENDER_TROUBLESHOOT_STATUS_TYPE_ERROR,
response))) {
mender_log_error("Unable to format response");
goto FAIL;
}
/* Invoke shell begin callback */
if (NULL != mender_troubleshoot_callbacks.shell_begin) {
if ((NULL != protomsg->protohdr->properties) && (NULL != protomsg->protohdr->properties->terminal_width)
&& (NULL != protomsg->protohdr->properties->terminal_height)) {
ret = mender_troubleshoot_callbacks.shell_begin(*protomsg->protohdr->properties->terminal_width,
*protomsg->protohdr->properties->terminal_height);
} else {
ret = mender_troubleshoot_callbacks.shell_begin(0, 0);
}
if (MENDER_OK != ret) {
mender_log_error("An error occured");
goto FAIL;
}
}
} else if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_STOP)) {
/* Check is a session is already opened */
if (NULL == mender_troubleshoot_shell_sid) {
mender_log_warning("No shell session opened");
goto END;
}
/* Stop shell session */
mender_log_info("Stopping current shell session");
/* Invoke shell end callback */
if (NULL != mender_troubleshoot_callbacks.shell_end) {
if (MENDER_OK != (ret = mender_troubleshoot_callbacks.shell_end())) {
mender_log_error("An error occured");
}
}
/* Format acknowledgment */
if (MENDER_OK
!= (ret
= mender_troubleshoot_format_acknowledgment(protomsg,
mender_troubleshoot_shell_sid,
(MENDER_OK == ret) ? MENDER_TROUBLESHOOT_STATUS_TYPE_NORMAL : MENDER_TROUBLESHOOT_STATUS_TYPE_ERROR,
response))) {
mender_log_error("Unable to format response");
}
/* Release session ID */
if (NULL != mender_troubleshoot_shell_sid) {
free(mender_troubleshoot_shell_sid);
mender_troubleshoot_shell_sid = NULL;
}
} else {
mender_log_error("Unsupported message received with message type '%s'", protomsg->protohdr->typ);
ret = MENDER_FAIL;
goto FAIL;
}
END:
return ret;
FAIL:
return ret;
}
static mender_err_t
mender_troubleshoot_mender_client_message_handler(mender_troubleshoot_protomsg_t *protomsg, mender_troubleshoot_protomsg_t **response) {
assert(NULL != protomsg);
assert(NULL != protomsg->protohdr);
mender_err_t ret = MENDER_OK;
/* Verify integrity of the message */
if (NULL == protomsg->protohdr->typ) {
mender_log_error("Invalid message received");
ret = MENDER_FAIL;
goto END;
}
/* Treatment of the message depending of the message type */
if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_MENDER_CLIENT_CHECK_UPDATE)) {
/* Trigger execution of the mender-client update work */
ret = mender_client_execute();
/* Format acknowledgment */
if (MENDER_OK
!= (ret = mender_troubleshoot_format_acknowledgment(
protomsg, NULL, (MENDER_OK == ret) ? MENDER_TROUBLESHOOT_STATUS_TYPE_NORMAL : MENDER_TROUBLESHOOT_STATUS_TYPE_ERROR, response))) {
mender_log_error("Unable to format response");
goto FAIL;
}
#ifdef CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY
} else if (!strcmp(protomsg->protohdr->typ, MENDER_TROUBLESHOOT_MESSAGE_TYPE_MENDER_CLIENT_SEND_INVENTORY)) {
/* Trigger execution of the mender-inventory work */
ret = mender_inventory_execute();
/* Format acknowledgment */
if (MENDER_OK
!= (ret = mender_troubleshoot_format_acknowledgment(
protomsg, NULL, (MENDER_OK == ret) ? MENDER_TROUBLESHOOT_STATUS_TYPE_NORMAL : MENDER_TROUBLESHOOT_STATUS_TYPE_ERROR, response))) {
mender_log_error("Unable to format response");
goto FAIL;
}
#endif /* CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY */
} else {
mender_log_error("Unsupported message received with message type '%s'", protomsg->protohdr->typ);
ret = MENDER_FAIL;
goto FAIL;
}
END:
return ret;
FAIL:
return ret;
}
static mender_err_t
mender_troubleshoot_format_acknowledgment(mender_troubleshoot_protomsg_t * protomsg,
char * sid,
mender_troubleshoot_properties_status_t status,
mender_troubleshoot_protomsg_t ** response) {
assert(NULL != protomsg);
assert(NULL != protomsg->protohdr);
mender_err_t ret = MENDER_OK;
/* Format acknowledgment message */
if (NULL == (*response = (mender_troubleshoot_protomsg_t *)malloc(sizeof(mender_troubleshoot_protomsg_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(*response, 0, sizeof(mender_troubleshoot_protomsg_t));
if (NULL == ((*response)->protohdr = (mender_troubleshoot_protohdr_t *)malloc(sizeof(mender_troubleshoot_protohdr_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset((*response)->protohdr, 0, sizeof(mender_troubleshoot_protohdr_t));
(*response)->protohdr->proto = protomsg->protohdr->proto;
if (NULL == ((*response)->protohdr->typ = strdup(protomsg->protohdr->typ))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
if (NULL != sid) {
if (NULL == ((*response)->protohdr->sid = strdup(sid))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
}
if (NULL == ((*response)->protohdr->properties = (mender_troubleshoot_protohdr_properties_t *)malloc(sizeof(mender_troubleshoot_protohdr_properties_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset((*response)->protohdr->properties, 0, sizeof(mender_troubleshoot_protohdr_properties_t));
if (NULL
== ((*response)->protohdr->properties->status = (mender_troubleshoot_properties_status_t *)malloc(sizeof(mender_troubleshoot_properties_status_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
*((*response)->protohdr->properties->status) = status;
return ret;
FAIL:
/* Release memory */
mender_troubleshoot_release_protomsg(*response);
*response = NULL;
return ret;
}
static mender_err_t
mender_troubleshoot_send_shell_ping_protomsg(void) {
mender_troubleshoot_protomsg_t *protomsg = NULL;
mender_err_t ret = MENDER_OK;
void * payload = NULL;
size_t length = 0;
/* Send shell ping message */
if (NULL == (protomsg = (mender_troubleshoot_protomsg_t *)malloc(sizeof(mender_troubleshoot_protomsg_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg, 0, sizeof(mender_troubleshoot_protomsg_t));
if (NULL == (protomsg->protohdr = (mender_troubleshoot_protohdr_t *)malloc(sizeof(mender_troubleshoot_protohdr_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg->protohdr, 0, sizeof(mender_troubleshoot_protohdr_t));
protomsg->protohdr->proto = MENDER_TROUBLESHOOT_PROTO_TYPE_SHELL;
if (NULL == (protomsg->protohdr->typ = strdup(MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_PING))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
if (NULL == (protomsg->protohdr->sid = strdup(mender_troubleshoot_shell_sid))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
if (NULL == (protomsg->protohdr->properties = (mender_troubleshoot_protohdr_properties_t *)malloc(sizeof(mender_troubleshoot_protohdr_properties_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg->protohdr->properties, 0, sizeof(mender_troubleshoot_protohdr_properties_t));
if (NULL == (protomsg->protohdr->properties->timeout = (uint32_t *)malloc(sizeof(uint32_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
*protomsg->protohdr->properties->timeout = 2 * CONFIG_MENDER_CLIENT_TROUBLESHOOT_HEALTHCHECK_INTERVAL;
if (NULL == (protomsg->protohdr->properties->status = (mender_troubleshoot_properties_status_t *)malloc(sizeof(mender_troubleshoot_properties_status_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
*protomsg->protohdr->properties->status = MENDER_TROUBLESHOOT_STATUS_TYPE_CONTROL;
/* Encode and pack the message */
if (MENDER_OK != (ret = mender_troubleshoot_pack_protomsg(protomsg, &payload, &length))) {
mender_log_error("Unable to encode message");
goto FAIL;
}
/* Send message */
if (MENDER_OK != (ret = mender_api_troubleshoot_send(mender_troubleshoot_handle, payload, length))) {
mender_log_error("Unable to send message");
goto FAIL;
}
FAIL:
/* Release memory */
mender_troubleshoot_release_protomsg(protomsg);
if (NULL != payload) {
free(payload);
}
return ret;
}
static mender_err_t
mender_troubleshoot_send_shell_stop_protomsg(void) {
mender_troubleshoot_protomsg_t *protomsg = NULL;
mender_err_t ret = MENDER_OK;
void * payload = NULL;
size_t length = 0;
/* Send shell stop message */
if (NULL == (protomsg = (mender_troubleshoot_protomsg_t *)malloc(sizeof(mender_troubleshoot_protomsg_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg, 0, sizeof(mender_troubleshoot_protomsg_t));
if (NULL == (protomsg->protohdr = (mender_troubleshoot_protohdr_t *)malloc(sizeof(mender_troubleshoot_protohdr_t)))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
memset(protomsg->protohdr, 0, sizeof(mender_troubleshoot_protohdr_t));
protomsg->protohdr->proto = MENDER_TROUBLESHOOT_PROTO_TYPE_SHELL;
if (NULL == (protomsg->protohdr->typ = strdup(MENDER_TROUBLESHOOT_MESSAGE_TYPE_SHELL_STOP))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
if (NULL == (protomsg->protohdr->sid = strdup(mender_troubleshoot_shell_sid))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;