= NetBSD = NetBSD is a free, fast, secure, and highly portable Unix-like Open Source operating system. It is available for a wide range of platforms, from large-scale servers and powerful desktop systems to handheld and embedded devices. https://www.netbsd.org/ https://www.netbsd.org/docs/guide/en/index.html == Install == {{{ VirtualBox installation Start VirtualBox New Name NetBSD 7.0.1 Choose type BSD version NetBSD 64 bit RAM 512 MB Create virtual disk dynamic VDI with Fixed size 8 GB Create Start VM Choose ISO NetBSD-7.0.1-amd64.iso Start 1 - Install NetBSD a - installation messages in english q - keyboard type portuguese a - install netbsd to hard disk shall we continue? (b) yes which disk? a) wd0 a - this is the correct geometry b - use the entire disk install netbsd bootcode? a) yes b - use existing partition sizes x - partition sizes ok name harddisk: VBOX HARDDISK shall we continue? yes a) use bios console x) exit a) full installation install from: a) cd-rom Hit enter to continue a) configure net a) wm0 autoselect autoconfig? yes hostname: netbsd domain name: they are ok? yes install in /etc? yes b) timezone Portugal c) root shell /bin/sh d) root password ******** e) enable installation x) f) fetch and unpack x) g) enable sshd yes h) enable ntpd yes i) run ntpdate at boot yes j) enable mdnsd no k) enable xdm no l) enable cgd yes m) enable lvm no n) enable raidframe yes o) add a user vitor add to group wheel, user shell sh x) finished configuring Hit enter to continue release ISO reboot login as root }}} {{{#!highlight sh startx # it starts OK setxkbmap pt cc --version pkg_info putty vitor@localhost -P 2222 pkg_info # pkg_install-20160410nb1 Package management and administration tools for pkgsrc # libarchive-3.2.1nb1 Library to read/create different archive formats # pkgin-0.9.4nb2 Apt / yum like tool for managing pkgsrc binary packages # https://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/README-all.html pkg_add openjdk8-1.8.102 PKG_PATH="ftp://ftp.netbsd.org/pub/pkgsrc/packages/NetBSD/amd64/7.0.1/All/" export PKG_PATH PATH="/usr/pkg/sbin:$PATH" export PATH pkg_add openjdk8-1.8.92nb2 cd /usr/pkg/java/openjdk8/bin ./java -version pkg_add python27-2.7.11nb2 # Execute this command to extract and rehash all CA root certificates # distributed by the Mozilla Project, so that they can be used by third # party applications using OpenSSL. It also creates a single file # certificate bundle in PEM format which can be used by applications using # GnuTLS. # mozilla-rootcerts install # To mark these certificates as trusted for users of gnupg2, do # the following (assuming default PKG_SYSCONFBASE and a Bourne shell): # # mkdir -p /usr/pkg/etc/gnupg # # cd /usr/pkg/etc/gnupg # # for c in /etc/openssl/certs/*.pem; do # > openssl x509 -in $c -noout -fingerprint|sed 's|^.*=\(.*\)|\1 S|' # > done > trustlist.txt python2.7 openjdk8-java -version ntpdate pt.pool.ntp.org }}} https://www.netbsd.org/docs/guide/en/chap-cons.html#chap-cons-wscons-wskbd-keymaps * 8.1.2.1.1. Hacking wscons to add a keymap http://julio.meroh.net/2004/07/playing-with-netbsd-keymaps.html === etc/profile === {{{#!highlight sh # System-wide .profile file for sh(1). export LANG="pt_PT.UTF-8" export LC_CTYPE="pt_PT.UTF-8" export LC_ALL="" }}} {{{#!highlight sh #change shell to have utf-8 support, ksh has chsh -s /bin/ksh vitor chsh -s /bin/ksh root }}} == Custom service == Add to /etc/rc.conf {{{#!highlight sh beat=YES }}} === /etc/rc.d/beat === {{{#!highlight sh #!/bin/sh # # PROVIDE: beat $_rc_subr_loaded . /etc/rc.subr name="beat" rcvar=$name pidfile="/var/run/${name}.pid" command="/usr/pkg/bin/python2.7" command_args="/home/vitor/${name}.py &" load_rc_config $name run_rc_command "$1" }}} === /home/vitor/beat.py === {{{#!highlight python #!/usr/bin/python import threading import time import os import syslog import datetime import sys import signal def termHandler(signal,frame): print('Signal term caught') sys.exit(0) if __name__=="__main__": f=open('/var/run/beat.pid','wa') f.write('%d'%(os.getpid())) f.close() signal.signal(signal.SIGTERM,termHandler) while True: syslog.syslog(syslog.LOG_INFO, "Beat %s"%(datetime.datetime.now()) ) time.sleep(5) }}} {{{#!highlight sh /etc/rc.d/beat start tail -f /var/log/messages /etc/rc.d/beat status /etc/rc.d/beat stop }}} In /root/.profile uncomment the following to allow binary packages installations * export PKG_PATH=ftp://ftp.NetBSD.org/pub/pkgsrc/packages/NetBSD/$(uname -m)/7.0/All == Change prompt == === ~/.profile === {{{#!highlight sh PS1="$(date +'%Y-%m-%dT%H:%M:%S') $(whoami)@${HOST}:${PWD}\$ " }}} == cbeat service == === cbeat.c === {{{#!highlight c // cc cbeat.c -o cbeat #include #include #include #include #include #define TRUE 1 void signalHandler(int signal){ if(signal == SIGTERM){ syslog(LOG_INFO , "Caught SIGTERM signal. Going to exit ..."); exit(0); } } int main(){ int pid = getpid(); FILE* handle = fopen("/var/run/cbeat.pid","wb"); fprintf(handle, "%d" ,pid); fclose(handle); // callback to signalHandler function signal(SIGTERM , signalHandler ); while(TRUE){ syslog(LOG_INFO,"cbeat test"); sleep(5); } return 0; } }}} === /etc/rc.d/cbeat === {{{#!highlight sh #!/bin/sh # # PROVIDE: cbeat $_rc_subr_loaded . /etc/rc.subr name="cbeat" rcvar=$name pidfile="/var/run/${name}.pid" command="/root/cbeat" command_args="&" load_rc_config $name run_rc_command "$1" }}} Add in /etc/rc.conf {{{#!highlight sh cbeat=YES }}} == POSIX threads == POSIX threads in NetBSD with signal handling. {{{#!highlight sh kill -s TERM kill -s USR1 kill -s USR2 }}} {{{#!highlight c /* cc pthread.c -o pthread -lpthread ./pthread ./pthread */ #include #include #include #include #include #include typedef struct data { char msg[128]; int count; } threadData; void signalHandler(int signal){ if(signal == SIGTERM){ printf("Caught SIGTERM signal. Going to exit ...\n"); exit(0); } if(signal == SIGUSR1){ printf("USR1 caught \n"); } if(signal == SIGUSR2){ printf("USR2 caught \n"); } } void *threadCallback( void *ptr ) { char *message = (char *) ((threadData *) ptr)->msg; int count = ((threadData *) ptr)->count; while(count<10) { printf("%s count %d thread id %u \n", message,count , pthread_self() ); sleep(1); count++; } } int main() { struct sigaction signalAction; memset (&signalAction, '\0', sizeof(signalAction)); signalAction.sa_handler = &signalHandler; pthread_t thread1Id; int thread1Ret; threadData thread1Data; strcpy(thread1Data.msg,"Message 1234"); thread1Data.count=5; pthread_t thread2Id; int thread2Ret; threadData thread2Data; strcpy(thread2Data.msg,"Message 54321"); thread2Data.count=3; printf("Main thread id %u \n" , pthread_self() ); thread1Ret = pthread_create( &thread1Id, NULL, threadCallback, (void*) &thread1Data); thread2Ret = pthread_create( &thread2Id, NULL, threadCallback, (void*) &thread2Data); if(thread1Ret || thread2Ret) { fprintf(stderr,"Error creating threads "); exit(EXIT_FAILURE); } // callback to signalHandler function sigaction(SIGTERM , &signalAction, NULL); sigaction(SIGUSR1 , &signalAction, NULL); sigaction(SIGUSR2 , &signalAction, NULL); pthread_join( thread1Id, NULL); pthread_join( thread2Id, NULL); printf("Normal exit\n"); exit(EXIT_SUCCESS); } }}} == Bash == {{{#!highlight sh export PKG_PATH=ftp://ftp.NetBSD.org/pub/pkgsrc/packages/NetBSD/$(uname -m)/7.0/All pkg_add bash }}} === ~/.profile === {{{#!highlight sh #normal user PS1="\D{%Y-%m-%d}T\A \u@\H:\w\r\n\$" # for root PS1="\D{%Y-%m-%d}T\A \u@\H:\w\r\n\043" #normal user PS1="\e[0;33m\]\D{%Y-%m-%d}T\A\[\e[m\] \e[0;32m\]\u@\H:\w\r\n\$\[\e[m\] " #root PS1="\e[0;33m\]\D{%Y-%m-%d}T\A\[\e[m\] \e[0;32m\]\u@\H:\w\r\n\043\[\e[m\] " }}} == C - read and write text == {{{#!highlight c #include #define SIZE 5 #define FILENAME "test.txt" void clear(char* buffer){ for(int i=0;i