--- /dev/null
+#include "command1.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+ printf("command1 %d\n", f());
+ return 0;
+}
--- /dev/null
+#include "command2.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+ printf("command2 %d\n", f() + argc);
+ return 0;
+}
--- /dev/null
+#include "just_fun.h"
+int f(){
+ return 5;
+}
--- /dev/null
+Various commmand files each with its own main for testing a library. makeheaders gets confused and puts all the
+declarations in the headers, leading to a failure.
+
+
+> ls
+command1.c command2.c just_fun.c
+> cat just_fun.c
+#include "just_fun.h"
+int f(){
+ return 5;
+}
+> cat command1.c
+#include "command1.h"
+#include <stdio.h>
+int main(){
+ printf("command1 %d\n", f());
+ return 0;
+}
+> cat command2.c
+#include "command2.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+ printf("command2 %d\n", f() + argc);
+ return 0;
+}
+> makeheaders *.c
+> gcc -o command1 command1.c
+command1.c: In function ‘main’:
+command1.c:3:1: error: number of arguments doesn’t match prototype
+ int main(){
+ ^~~
+In file included from command1.c:1:
+command1.h:5:5: error: prototype declaration
+ int main(int argc,char **argv);
+ ^~~~
+>
--- /dev/null
+Making each main call static so it won't be in the header. gcc can't find main.
+
+> cat command1.c
+#include "command1.h"
+#include <stdio.h>
+static int main(){
+ printf("command1 %d\n", f());
+ return 0;
+}
+> cat command2.c
+#include "command2.h"
+#include <stdio.h>
+static int main(int argc, char **argv){
+ printf("command2 %d\n", f() + argc);
+ return 0;
+}
+> gcc -o command1 command1.c just_fun.c
+/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: in function `_start':
+(.text+0x24): undefined reference to `main'
+collect2: error: ld returned 1 exit status
--- /dev/null
+This time making each main definition have the same prototype. Still end up with multiple main declarations,
+it is just that they agree.
+
+> rm *.h
+> makeheaders *.c
+> cat command1.c
+#include "command1.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+ printf("command1 %d\n", f());
+ return 0;
+}
+> cat command1.h
+/* \aThis file was automatically generated. Do not edit! */
+#undef INTERFACE
+int f();
+int main(int argc,char **argv);
+int main(int argc,char **argv);
+> cat command2.c
+#include "command2.h"
+#include <stdio.h>
+int main(int argc, char **argv){
+ printf("command2 %d\n", f() + argc);
+ return 0;
+}
+> gcc -o command1 command1.c just_fun.c
+> .. worked
--- /dev/null
+
+
+filename.tag.extension
+
+extension:
+ .c for C source
+ .cc for C++ source
+ .h for C header file
+ .hh for C++ header file
+ .o an object file
+
+tag:
+ .lib. The resulting .o file to be placed in release library and is part of the
+ programming interface.
+ .aux. The resulting.o file not directly part of the programming interface, but
+ it might be called by functions that are.
+ .cli. The source file has a main call and is to be relased as part of the command line interface
+ .loc. file has a main call to be made into a local uitlity function
+
+We carry the source file tag and extension to the .o file. We do not put tags
+nor extensions on command line executables.
+
+local_common.h should be included in all source files
/* \aThis file was automatically generated. Do not edit! */
#undef INTERFACE
+#include <sys/types.h>
+#include <unistd.h>
int dispatch_f_euid_egid(char *fname,int(*f)(void *arg),void *f_arg,uid_t euid,gid_t egid);
int dbprintf(const char *format,...);
int dispatch_f(char *fname,int(*f)(void *arg),void *f_arg);
+#define ERR_DISPATCH_F_SETEGID 3
+#define ERR_DISPATCH_F_SETEUID 2
+#define ERR_DISPATCH_F_FORK 1
+#define INTERFACE 0
#undef INTERFACE
#include <sqlite3.h>
typedef unsigned int uint;
-int subu_number(sqlite3 *db,uint **subu_number);
+int subu_number(sqlite3 *db,uint *subu_number);
int schema(sqlite3 *db,uint max_subu_number);
extern char config_file[];
+extern char config_file[];
#define ERR_CONFIG_FILE -1
#define INTERFACE 0
typedef unsigned int uint;
int schema(sqlite3 *db,uint max_subu_number);
extern char config_file[];
-int main();
-int main();
-int main(int argc,char **argv,char **env);
+extern char config_file[];
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
#undef INTERFACE
#include <sqlite3.h>
extern char config_file[];
+extern char config_file[];
int subu_mk_0(char *subuname,char *config_file);
-int main();
-int main();
-int main(int argc,char **argv,char **env);
+#define ERR_SUBU_MK_0_ARG_CNT 1
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
struct passwd *pw_record;
};
struct dispatch_useradd_ret_t dispatch_useradd(char **argv,char **envp);
+#include <unistd.h>
int dispatch_f_euid_egid(char *fname,int(*f)(void *arg),void *f_arg,uid_t euid,gid_t egid);
int dbprintf(const char *format,...);
#include <sqlite3.h>
extern char config_file[];
+extern char config_file[];
int subu_mk_0(char *subuname,char *config_file);
int masteru_makes_subuhome(void *arg);
int allowed_subuname(char *subuname);
-#define ERR_SUBU_MK_0_SETFACL 9
-#define ERR_SUBU_MK_0_FAILED_USERADD 8
-#define ERR_SUBU_MK_0_BUG_SSS 7
-#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 6
-#define ERR_SUBU_MK_0_MK_SUBUHOME 5
-#define ERR_SUBU_MK_0_MALLOC 4
-#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 3
-#define ERR_SUBU_MK_0_SETUID_ROOT 2
-#define ERR_SUBU_MK_0_CONFIG_FILE 1
+#define ERR_SUBU_MK_0_SETFACL 10
+#define ERR_SUBU_MK_0_FAILED_USERADD 9
+#define ERR_SUBU_MK_0_BUG_SSS 8
+#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 7
+#define ERR_SUBU_MK_0_MK_SUBUHOME 6
+#define ERR_SUBU_MK_0_MALLOC 5
+#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 4
+#define ERR_SUBU_MK_0_SETUID_ROOT 3
+#define ERR_SUBU_MK_0_CONFIG_FILE 2
+#define ERR_SUBU_MK_0_ARG_CNT 1
#define INTERFACE 0
#undef INTERFACE
#include <sqlite3.h>
typedef unsigned int uint;
-int subu_number(sqlite3 *db,uint **subu_number);
+int subu_number(sqlite3 *db,uint *subu_number);
#define ERR_CONFIG_FILE -1
extern char config_file[];
-int main();
-int main();
-int main(int argc,char **argv,char **env);
+extern char config_file[];
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
+int main(int argc,char **argv,char **envp);
+++ /dev/null
-
-
-filename.tag.extension
-
-extension:
- .c for C source
- .cc for C++ source
- .h for C header file
- .hh for C++ header file
- .o an object file
-
-tag:
- .lib. The resulting .o file to be placed in release library and is part of the
- programming interface.
- .aux. The resulting.o file not directly part of the programming interface, but
- it might be called by functions that are.
- .cli. The source file has a main call and is to be relased as part of the command line interface
- .loc. file has a main call to be made into a local uitlity function
-
-We carry the source file tag and extension to the .o file. We do not put tags
-nor extensions on command line executables.
-
-local_common.h should be included in all source files
command passed in, and wants better behavior, he or she can spin a special
version of dispatch for that command.
*/
+#define _GNU_SOURCE
#include "dispatch_f.lib.h"
+// we need the declaration for uid_t etc.
+#if INTERFACE
+#include <sys/types.h>
+#include <unistd.h>
+#define ERR_DISPATCH_F_FORK 1
+#define ERR_DISPATCH_F_SETEUID 2
+#define ERR_DISPATCH_F_SETEGID 3
+#endif
// without this #define execvpe is undefined
#define _GNU_SOURCE
-#include <sys/types.h>
-#include <unistd.h>
#include <wait.h>
#include <stdlib.h>
#include <stdio.h>
if( pid == -1 ){
perror(perror_src);
fprintf(stderr, "%s %s\n", perror_src, fname);
- return ERR_FORK;
+ return ERR_DISPATCH_F_FORK;
}
if( pid == 0 ){ // we are the child
int status = (*f)(f_arg);
if( pid == -1 ){
perror(perror_src);
fprintf(stderr, "%s %s %u %u\n", perror_src, fname, euid, egid);
- return ERR_FORK;
+ return ERR_DISPATCH_F_FORK;
}
if( pid == 0 ){ // we are the child
if( seteuid(euid) == -1 ){
perror(perror_src);
fprintf(stderr, "%s %s %u %u\n", perror_src, fname, euid, egid);
- return ERR_SETEUID;
+ return ERR_DISPATCH_F_SETEUID;
}
if( setegid(egid) == -1 ){
perror(perror_src);
fprintf(stderr, "%s %s %u %u\n", perror_src, fname, euid, egid);
- return ERR_SETEGID;
+ return ERR_DISPATCH_F_SETEGID;
}
int status = (*f)(f_arg);
exit(status);
SHELL=/bin/bash
SCRATCHDIR= 5_scratch # clean and others put things here
CC=gcc
-CFLAGS=-std=c11 -fPIC -I. -ggdb -DDEBUG
+CFLAGS=-std=c11 -fPIC -I. -ggdb -DDEBUG -Werror
LIB="libsubu.a"
-LIBPATH="." #no trailing slash
-LINKFLAGS="-L. -lsubu"
+LINKFLAGS=-L. -lsubu -lsqlite3
#these are the source files that exist
SOURCES_LIB= $(wildcard *.lib.c)
@echo "CC: " $(CC)
@echo "CFLAGS: " $(CFLAGS)
@echo "LIB: " $(LIB)
- @echo "LIBPATH: " $(LIBPATH)
@echo "LINKFLAGS: " $(LINKFLAGS)
@echo '______end make $@_____'
$(CC) $(CFLAGS) -MM $(SOURCES) 1> 2_makefile_deps
for i in $(EXECS) ; do\
$(ECHO) >> 2_makefile_deps;\
- $(ECHO) "$$i : $$i.cli.o $(LIBPATH)/$(LIB)" >> 2_makefile_deps;\
+ $(ECHO) "$$i : $$i.cli.o $(LIB)" >> 2_makefile_deps;\
$(ECHO) " $(CXX) -o $$i $$i.cli.o $(LINKFLAGS)" >> 2_makefile_deps;\
done
@echo '______end make $@_____'
sub_lib: $(LIB)
-execs: $(LIBPATH)/$(LIB)
+execs: $(LIB)
@echo '---- make $@:------------------------------------------------------------'
@echo `pwd`'>'
if [ ! -e 2_makefile_deps ]; then make deps; fi
clean:
@echo '---- make $@:------------------------------------------------------------'
@echo `pwd`'>'
+ if [ -f subu.db ]; then rm subu.db; fi
for i in $(wildcard *~); do mv $$i $(SCRATCHDIR); done
for i in $(wildcard *.lib.o) $(wildcard *.cli.o); do rm $$i; done
for i in $(HFILES); do mv $$i 5_scratch; done # just in case someone wrote a header file
return sqlite3_exec(db, sql, NULL, NULL, NULL);
}
-int subu_number(sqlite3 *db, uint **subu_number){
+static uint count = 0;
+
+static int callback(void *NotUsed, int argc, char **argv, char **col_name){
+ int i;
+ printf("count: %u\n", count++);
+ for(i=0; i<argc; i++){
+ printf("%s = %s\n", col_name[i], argv[i] ? argv[i] : "NULL");
+ }
+ printf("\n");
+ return 0;
+ }
+
+int subu_number(sqlite3 *db, uint *subu_number){
*subu_number = 0;
char *sql =
"BEGIN TRANSACTION;"
"UPDATE Key_Int SET value = value + 1 WHERE key = 'max_subu_number';"
"SELECT value FROM Key_Int WHERE key = 'max_subu_number';"
"COMMIT;";
- size_t sql_len = strlen(sql);
int ret;
- sqlite3_stmt *stmt;
- ret = sqlite3_prepare_v2(db, sql, sql_len, &stmt, NULL);
- if( ret != SQLITE_OK ){
- return ERR_CONFIG_FILE;
- }
- sqlite3_stmt *res;
- rc = sqlite3_step(res);
- if( rc == SQLITE_ROW ){
- printf("%u\n", sqlite3_column_int(res, NULL));
- }
- rc = sqlite3_step(res);
- if( rc == SQLITE_ROW ){
- printf("%u\n", sqlite3_column_int(res, NULL));
+ char *errmsg;
+ ret = sqlite3_exec(db, sql, callback, NULL, &errmsg);
+ if( errmsg ){
+ printf("exec failed: %s\n", errmsg);
+ sqlite3_free(errmsg);
}
- sqlite3_finalize(res);
}
#include "subu-init.cli.h"
#include <stdio.h>
-int main(){
+int main(int argc, char **argv, char **envp){
sqlite3 *db;
if(
sqlite3_open(config_file, &db)
subu-mk-0 command
*/
-
-#include <stdio.h>
#include "subu-mk-0.lib.h"
+#include <stdio.h>
-int main(int argc, char **argv, char **env){
+// char *config_file = "/etc/subu.db";
+char config_file[] = "subu.db";
+
+int main(int argc, char **argv, char **envp){
char *command = argv[0];
if( argc != 2 ){
fprintf(stderr, "usage: %s subu", command);
- return ERR_ARG_CNT;
+ return ERR_SUBU_MK_0_ARG_CNT;
}
- return subu_mk_0(argv[1]);
+ char *subuname = argv[1];
+
+ return subu_mk_0(subuname, config_file);
}
#include <stdbool.h>
#if INTERFACE
-#define ERR_SUBU_MK_0_CONFIG_FILE 1
-#define ERR_SUBU_MK_0_SETUID_ROOT 2
-#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 3
-#define ERR_SUBU_MK_0_MALLOC 4
-#define ERR_SUBU_MK_0_MK_SUBUHOME 5
-#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 6
-#define ERR_SUBU_MK_0_BUG_SSS 7
-#define ERR_SUBU_MK_0_FAILED_USERADD 8
-#define ERR_SUBU_MK_0_SETFACL 9
+#define ERR_SUBU_MK_0_ARG_CNT 1
+#define ERR_SUBU_MK_0_CONFIG_FILE 2
+#define ERR_SUBU_MK_0_SETUID_ROOT 3
+#define ERR_SUBU_MK_0_BAD_MASTERU_HOME 4
+#define ERR_SUBU_MK_0_MALLOC 5
+#define ERR_SUBU_MK_0_MK_SUBUHOME 6
+#define ERR_SUBU_MK_0_FAILED_MKDIR_SUBU 7
+#define ERR_SUBU_MK_0_BUG_SSS 8
+#define ERR_SUBU_MK_0_FAILED_USERADD 9
+#define ERR_SUBU_MK_0_SETFACL 10
#endif
#include "subu-number.cli.h"
#include <stdio.h>
-int main(){
+int main(int argc, char **argv, char **envp){
int ret;
sqlite3 *db;
ret = sqlite3_open(config_file, &db);
fprintf(stderr, "error exit, could not build schema\n");
return ERR_CONFIG_FILE;
}
- uint subu_number;
- ret = subu_number(db, subu_number);
+ uint msn;
+ ret = subu_number(db, &msn);
if( sqlite3_close(db) != SQLITE_OK ){
fprintf(stderr, "error exit, strange, we could not close the db\n");
return ERR_CONFIG_FILE;