22#include "ctr_logging.h"
33#include "cli.h"
44#include "config.h"
5+ #include <ctype.h>
56#include <string.h>
67#include <sys/stat.h>
78
@@ -70,6 +71,7 @@ static char *container_id_full = NULL;
7071static char * container_id = NULL ;
7172static char * container_name = NULL ;
7273static char * container_tag = NULL ;
74+ static gchar * * container_labels = NULL ;
7375static size_t container_tag_len ;
7476static char * syslog_identifier = NULL ;
7577static size_t syslog_identifier_len ;
@@ -96,13 +98,43 @@ gboolean logging_is_passthrough(void)
9698 return use_logging_passthrough ;
9799}
98100
101+ static int count_chars_in_string (const char * str , char ch )
102+ {
103+ int count = 0 ;
104+
105+ while (str ) {
106+ str = strchr (str , ch );
107+ if (str == NULL )
108+ break ;
109+ count ++ ;
110+ str ++ ;
111+ }
112+
113+ return count ;
114+ }
115+
116+ static int is_valid_label_name (const char * str )
117+ {
118+ while (* str ) {
119+ if (* str == '=' ) {
120+ return 1 ;
121+ }
122+ if (!isupper (* str ) && !isdigit (* str ) && * str != '_' ) {
123+ return 0 ;
124+ }
125+ str ++ ;
126+ }
127+ return 1 ;
128+ }
129+
99130/*
100131 * configures container log specific information, such as the drivers the user
101132 * called with and the max log size for log file types. For the log file types
102133 * (currently just k8s log file), it will also open the log_fd for that specific
103134 * log file.
104135 */
105- void configure_log_drivers (gchar * * log_drivers , int64_t log_size_max_ , int64_t log_global_size_max_ , char * cuuid_ , char * name_ , char * tag )
136+ void configure_log_drivers (gchar * * log_drivers , int64_t log_size_max_ , int64_t log_global_size_max_ , char * cuuid_ , char * name_ , char * tag ,
137+ gchar * * log_labels )
106138{
107139 log_size_max = log_size_max_ ;
108140 log_global_size_max = log_global_size_max_ ;
@@ -126,8 +158,14 @@ void configure_log_drivers(gchar **log_drivers, int64_t log_size_max_, int64_t l
126158 }
127159 k8s_total_bytes_written = k8s_bytes_written ;
128160
129- if (!use_journald_logging && tag )
130- nexit ("k8s-file doesn't support --log-tag" );
161+ if (!use_journald_logging ) {
162+ if (!tag ) {
163+ nexit ("k8s-file doesn't support --log-tag" );
164+ }
165+ if (!log_labels ) {
166+ nexit ("k8s-file doesn't support --log-label" );
167+ }
168+ }
131169 }
132170
133171 if (use_journald_logging ) {
@@ -169,6 +207,24 @@ void configure_log_drivers(gchar **log_drivers, int64_t log_size_max_, int64_t l
169207 syslog_identifier = g_strdup_printf ("SYSLOG_IDENTIFIER=%s" , tag );
170208 syslog_identifier_len = strlen (syslog_identifier );
171209 }
210+ if (log_labels ) {
211+ container_labels = log_labels ;
212+
213+ /* Ensure that valid LABEL=VALUE pairs have been passed */
214+ for (char * * ptr = log_labels ; * ptr ; ptr ++ ) {
215+ if (* * ptr == '=' ) {
216+ nexitf ("Container labels must be in format LABEL=VALUE (no LABEL present in '%s')" , * ptr );
217+ }
218+ if (count_chars_in_string (* ptr , '=' ) != 1 ) {
219+ nexitf ("Container labels must be in format LABEL=VALUE (none or more than one '=' present in '%s')" ,
220+ * ptr );
221+ }
222+ if (!is_valid_label_name (* ptr )) {
223+ nexitf ("Container label names must contain only uppercase letters, numbers and underscore (in '%s')" ,
224+ * ptr );
225+ }
226+ }
227+ }
172228 }
173229}
174230
@@ -325,6 +381,12 @@ static int write_journald(int pipe, char *buf, ssize_t buflen)
325381 /* per docker journald logging format, CONTAINER_PARTIAL_MESSAGE is set to true if it's partial, but otherwise not set. */
326382 if (partial && writev_buffer_append_segment_no_flush (& bufv , "CONTAINER_PARTIAL_MESSAGE=true" , PARTIAL_MESSAGE_EQ_LEN ) < 0 )
327383 return -1 ;
384+ if (container_labels ) {
385+ for (gchar * * label = container_labels ; * label ; ++ label ) {
386+ if (writev_buffer_append_segment_no_flush (& bufv , * label , strlen (* label )) < 0 )
387+ return -1 ;
388+ }
389+ }
328390
329391 int err = sd_journal_sendv (bufv .iov , bufv .iovcnt );
330392 if (err < 0 ) {
0 commit comments