1 //--------------------------------------------------------------------------
2 // Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation. You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 //--------------------------------------------------------------------------
18 // thread.cc author Russ Combs <rucombs@cisco.com>
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "thread.h"
25
26 #include <sys/stat.h>
27
28 #include "snort_config.h"
29 #include "thread_config.h"
30
31 //-------------------------------------------------------------------------
32 // FIXIT-L instance_id zero indicates main thread during parse time and the
33 // first packet thread during runtime. not sure if i'm ok with that.
34 // works for now.
35 //-------------------------------------------------------------------------
36
37 static THREAD_LOCAL uint16_t run_num = 0;
38 static THREAD_LOCAL unsigned instance_id = 0;
39 static THREAD_LOCAL SThreadType thread_type = STHREAD_TYPE_OTHER;
40
set_run_num(uint16_t num)41 void set_run_num(uint16_t num)
42 { run_num = num; }
43
get_run_num()44 uint16_t get_run_num()
45 { return run_num; }
46
set_instance_id(unsigned id)47 void set_instance_id(unsigned id)
48 { instance_id = id; }
49
set_thread_type(SThreadType type)50 void set_thread_type(SThreadType type)
51 { thread_type = type; }
52
53 namespace snort
54 {
get_instance_id()55 unsigned get_instance_id()
56 { return instance_id; }
57
get_thread_type()58 SThreadType get_thread_type()
59 { return thread_type; }
60
61
62 //-------------------------------------------------------------------------
63 // format is:
64 // <logdir>/[<run_prefix>][<id#>][<X>]<name>
65 //
66 // where:
67 // -- <logdir> is ./ if not set
68 // -- <run_prefix> is optional
69 // -- <id#> is optionally omitted for instance 0
70 // -- <X> is either _ or / or nothing
71 //-------------------------------------------------------------------------
72
get_instance_file(std::string & file,const char * name)73 const char* get_instance_file(std::string& file, const char* name)
74 {
75 const SnortConfig* sc = SnortConfig::get_conf();
76
77 bool sep = false;
78 file = !sc->log_dir.empty() ? sc->log_dir : "./";
79
80 if ( file.back() != '/' )
81 file += '/';
82
83 if ( !sc->run_prefix.empty() )
84 {
85 file += sc->run_prefix;
86 sep = true;
87 }
88
89 if ( (ThreadConfig::get_instance_max() > 1) || sc->id_zero )
90 {
91 char id[8];
92 snprintf(id, sizeof(id), "%u",
93 get_instance_id() + sc->id_offset);
94 file += id;
95 sep = true;
96 }
97
98 if ( sc->id_subdir )
99 {
100 file += '/';
101 struct stat s;
102
103 if ( stat(file.c_str(), &s) )
104 // FIXIT-L getting random 0750 or 0700 (umask not thread local)?
105 mkdir(file.c_str(), 0770);
106 }
107 else if ( sep )
108 file += '_';
109
110 file += name;
111
112 return file.c_str();
113 }
114 }
115