# Making a port for ZoneMinder



## abishai (Mar 22, 2016)

I'm making the port for ZoneMinder, I have several questions:

1. I need writable directory under /usr/local/www/zoneminder
I try to do it in post-install section of Makefile

```
@${MKDIR} ${STAGEDIR}${WWWDIR}/images
@${CHOWN} ${WWWOWN}:${WWWGRP} ${STAGEDIR}${WWWDIR}/images
```
I see directory in stage folder with right permissions, however it installs with default one. How to change them?
2. Application introduces ELF in cgi-bin. Should I put it /usr/local/libexec as hier suggests or I can leave it in that folder? (webserver will run it).
3. Application generates HUGE number of files (it writes video as jpegs), however they should be in www root. Upstream has plans to move it elsewhere and now they make symlinks. However, this will break chrooted php-fpm installations, but HUGE number of files are candidates to /var according hier Can I keep them in www root ? I saw different approach where to keep big folders depending on slice layout. (/home, /var /usr). If someone want to move them, he can symlink my himself.
4. Application uses /dev/shm Should I suggest users to mount it as tempfs or change defaults to /tmp, mentioning that this will be perfomance hog if placed outside of tmpfs (but, it least it will work).
5. Betatesters, maybe ? This is my first port 

PS: Ohh, and they type FreeBSD incorrectly! https://github.com/ZoneMinder/ZoneMinder/blob/master/CMakeLists.txt#L185 Should I patch CMake files or emorage in their irc should be enough? :-D


----------



## Uniballer (Mar 22, 2016)

I did build and install the Zoneminder version in the ports tree (multimedia/zoneminder) on a FreeBSD system, but I never got it to work satisfactorily with my chosen cameras (Hikvision DS‑2CD2032‑I) .  Be aware that version of Zoneminder (1.25.0) is about 5 years old.  I found that Zoneminder 1.28.x on a Linux box worked much better than earlier versions, and am testing 1.29.x on one machine.


----------



## trumee (Mar 25, 2016)

abishai said:


> I'm making the port for ZoneMinder, I have several questions:
> 
> 5. Betatesters, maybe ? This is my first port



Thanks for picking this up. I will be happy to beta test.

I was able to build v1.29 in a FreeBSD 10.2 jail but wasn't able to make it work. Details are at https://github.com/ZoneMinder/ZoneMinder/issues/1288#issuecomment-186724300

Any idea where I went wrong?

Thanks


----------



## lme@ (Mar 25, 2016)

The last comment at https://github.com/ZoneMinder/ZoneMinder/issues/1288#issuecomment-186724300 states that ZoneMinder now "just works" on FreeBSD. Have you tried it?


----------



## abishai (Mar 26, 2016)

trumee said:


> Any idea where I went wrong?Thanks


Yes. pid under Linux can't exceed smallint datatype. This is wrong for FreeBSD. You must change pid datatype of Logs table to INT
Also, I gone www/nginx, I just dislike www/apache bloatware.


lme@ said:


> "just works" on FreeBSD. Have you tried it?


Yes, it just works, but we must package it and put stuff in right directories. My questions are mostly about correct packaging.

At the moment, I have

```
# $FreeBSD$

PORTNAME=   zoneminder
PORTVERSION=   1.29.0
DISTVERSION = ecb8c48
CATEGORIES=   multimedia

MAINTAINER=   foo@bar
COMMENT=   Complete security camera solution, fully web based with image analysis

LICENSE=   GPLv2

USE_GITHUB=   yes
GH_ACCOUNT=   FriendsOfCake:crud
GH_PROJECT=   crud:crud
GH_TAGNAME=   c3976f1:crud

WRKSRC= ${WRKDIR}/ZoneMinder-${DISTVERSION}

BUILD_DEPENDS=   p5-DBI>=0:${PORTSDIR}/databases/p5-DBI \
         p5-DBD-mysql>=0:${PORTSDIR}/databases/p5-DBD-mysql \
         p5-Date-Manip>=0:${PORTSDIR}/devel/p5-Date-Manip \
         p5-Test-LWP-UserAgent>=0:${PORTSDIR}/www/p5-Test-LWP-UserAgent \
         p5-Sys-Mmap>=0:${PORTSDIR}/devel/p5-Sys-Mmap \
         p5-LWP-Protocol-https>=0:${PORTSDIR}/www/p5-LWP-Protocol-https
LIB_DEPENDS=   libpolkit-gobject-1.so:${PORTSDIR}/sysutils/polkit
RUN_DEPENDS=   ${BUILD_DEPENDS}

USES=     cmake jpeg perl5 shebangfix
USE_MYSQL= yes

ZM_RUNDIR=   /var/run/zm
ZM_TMPDIR=   /var/tmp/zm

SHEBANG_FILES=   scripts/zmaudit.pl.in \
         scripts/zmcamtool.pl.in \
         scripts/zmcontrol.pl.in \
         scripts/zmdc.pl.in \
         scripts/zmfilter.pl.in \
         scripts/zmpkg.pl.in \
         scripts/zmsystemctl.pl.in \
         scripts/zmtelemetry.pl.in \
         scripts/zmtrack.pl.in \
         scripts/zmtrigger.pl.in \
         scripts/zmupdate.pl.in \
         scripts/zmvideo.pl.in \
         scripts/zmwatch.pl.in \
         scripts/zmx10.pl.in

CMAKE_ARGS+=   -DZM_PERL_MM_PARMS=INSTALLDIRS=site \
         -DZM_CONFIG_DIR=/usr/local/etc \
         -DZM_WEBDIR=${WWWDIR} \
         -DZM_CGIDIR=${WWWDIR}/cgi-bin \
         -DZM_CONTENTDIR=${WWWDIR}
#CMAKE_ARGS+=   -DCMAKE_VERBOSE_MAKEFILE=ON

post-extract:
   @${MV} ${WRKSRC_crud}/* ${WRKSRC}/web/api/app/Plugin/Crud

post-install:
   ${CP} ${STAGEDIR}${PREFIX}/etc/zm.conf ${STAGEDIR}${PREFIX}/etc/zm.conf.sample
   ${INSTALL} -d -g ${WWWGRP} -o ${WWWOWN} ${STAGEDIR}${WWWDIR}/images
   ${INSTALL} -d -g ${WWWGRP} -o ${WWWOWN} ${STAGEDIR}${WWWDIR}/events
   ${INSTALL} -d -g ${WWWGRP} -o ${WWWOWN} ${STAGEDIR}${ZM_RUNDIR}
   ${INSTALL} -d -g ${WWWGRP} -o ${WWWOWN} ${STAGEDIR}${ZM_TMPDIR}

.include <bsd.port.mk>
```
The most confusing thing about ${INSTALL}. It generates correct folders in stage dir, but resets permissions on install

```
abishai@freezm:~/zoneminder % ls -ld /home/abishai/zoneminder/work/stage/var/tmp/zm
drwxr-xr-x  2 www  www  512 Mar 26 12:55 /home/abishai/zoneminder/work/stage/var/tmp/zm
abishai@freezm:~/zoneminder % ls -ld /var/tmp/zm
drwxr-xr-x  2 root  wheel  512 Mar 26 12:55 /var/tmp/zm
```
So, I don't know how to create writable directories for ZM automatically.


----------



## trumee (Mar 26, 2016)

abishai said:


> Yes. pid under Linux can't exceed smallint datatype. This is wrong for FreeBSD. You must change pid datatype of Logs table to INT



How can iI make this change?


----------



## abishai (Mar 27, 2016)

trumee said:


> How can i make this change?


`mysql -u root -p
use zm;
alter table Logs modify column Pid int default null;`
Then, disable STRICT_TRANS_TABLES removing it from sql_mode line on /usr/local/my.cnf
And change index.php error_reporting( E_ALL ); to error_reporting( E_ALL & ~E_NOTICE);

At the moment I have working mostly ZM under www/nginx
1. IP camera in MJPEG works using built-in ZM capture.
2. Detection works
3. Event writing works.

Doesn't work:
1. Montage with more than 1 camera
2. Event replay (signal 4 in zms on attempt to playback)

Not tested:
1. V4L, I even not compiled it as I don't have USB or analog cameras.
2. multimedia/ffmpeg. Not compiled it as well.
3. H264 cameras using ZM capture daemon.
4. It will be nice to test capture with multimedia/ffmpeg backend as well.

I will share entire port files when someone enlighten me how to create directories with proper owner.


----------



## Uniballer (Mar 27, 2016)

FYI - the montage doesn't work quite right on the Linux system where I'm testing 1.29.0, so the problem you're seeing with that might not be FreeBSD specific.  The grid options don't seem to work right, although it will show multiple cameras.  Your code base is probably newer than mine (I installed pre-built images on 10-Feb-2016 from the ppa:iconnor/zoneminder repository).


----------



## trumee (Mar 30, 2016)

abishai said:


> Doesn't work:
> 1. Montage with more than 1 camera
> 2. Event replay (signal 4 in zms on attempt to playback)



Maybe you can create a bug report for this on zm git page.


----------



## abishai (Apr 1, 2016)

I've got an answer on mailing lists. The right way to create directory with custom owner is use plist features:

```
@dir(%%WWWOWN%%,%%WWWGRP%%,) /var/run/zm
```
https://www.freebsd.org/doc/en/books/porters-handbook/plist-keywords.html


----------



## trumee (Apr 15, 2016)

Were you able to take it further?


----------



## abishai (Apr 16, 2016)

https://github.com/abishai/Zoneminder-port
It builds and runs under nginx. Still has signal 4 on playback, opened an issue https://github.com/ZoneMinder/ZoneMinder/issues/1357


----------



## abishai (Apr 17, 2016)

Signal 4 workarounded - I disabled sendfile support as crash was in sendfile() function call - it should fall back to less optimal solution on playback, but at least it works. Now we have almost working Zoneminder here https://github.com/abishai/Zoneminder-port
Known issues are:
1. Only for x64 https://github.com/ZoneMinder/ZoneMinder/issues/1423
2. Stale sock files in /var/run/zm https://github.com/ZoneMinder/ZoneMinder/issues/1424
3. V4L support can't be compiled - no local or analog cameras.
4. vlc support not tested and disabled (someone need it?)
5. Some pl scripts are lacking some deps, but nothing critical, I'm lazy to check them yet.
6. Timestamp lacks of camera name - add leading % to timestamp mask manually in monitor settings. https://github.com/ZoneMinder/ZoneMinder/issues/1426
7. pid file contains pid and `killall` command errors - amazing! https://github.com/ZoneMinder/ZoneMinder/pull/1422

But, more or less, ZM core is working on my 10.3-BETA1: capture, analysis, record, playback. multimedia/ffmpeg backend tested and works.

Probably, I should write a manual how to get it working with www/nginx and other stuff as it has some tricks. It's a pity I do all of this alone - I'm not a programmer and this is my first port, I recon someone familiar with porting could do all of this and even more in one hour


----------



## trumee (Apr 18, 2016)

I was able to start ZM following your instructions. The web UI loaded up and i was able to add a remote camera. However, i got the following error in the web log:

```
ERR Memory map file '/dev/shm/zm.mmap.1' does not exist. zmc might not be running. zmwatch.pl 
FAT Can't open memory map file /dev/shm/zm.mmap.1, probably not enough space free: No such file or directory
```

My intention is to run ZM in a jail on my server. Can ZM work in a jail?

I also tried pass the following to the jail


```
export jail_zm_parameters="allow.sysvipc=1"
```

But it did not make a difference.


----------



## abishai (Apr 18, 2016)

That's one of the tricks. We don't have /dev/shm, but we can mount tempfs somewhere (I suggest /tmp). Stop `zoneminder` and point it to the path, webconfig should have this path somewhere. It can work even on 'normal' directory, however, it will kill your I/O if not tempfs.  I think, we should patch default path later and mention that in readme.


----------



## lme@ (Apr 18, 2016)

abishai said:


> Probably, I should write a manual how to get it working with www/nginx and other stuff as it has some tricks. It's a pity I do all of this alone - I'm not a programmer and this is my first port, I recon someone familiar with porting could do all of this and even more in one hour


Can you upload a sharfile of what you have so far?


----------



## abishai (Apr 18, 2016)

lme@ said:


> Can you upload a sharfile of what you have so far?


Why shar? Is github not enough? I thought it suits better when stuff is WIP.


----------



## trumee (Apr 19, 2016)

abishai said:


> That's one of the tricks. We don't have /dev/shm, but we can mount tempfs somewhere (I suggest /tmp). Stop `zoneminder` and point it to the path, webconfig should have this path somewhere. It can work even on 'normal' directory, however, it will kill your I/O if not tempfs.  I think, we should patch default path later and mention that in readme.



Ok, I created tmpfs on the host and mounted in the jail.

On Host

```
#cat /etc/fstab.zm
/usr/jails/basejail /usr/jails/zm/basejail nullfs ro 0 0
tmpfs /usr/jails/zm/tmp/shm tmpfs rw,mode=777 0 0
```

In the jail, the memory is available

```
root@zm:~ # ls -lad /tmp/shm
drwxrwxrwx  2 root  wheel  0 Apr 18 21:40 /tmp/shm
```

Changed the PATH_MAP variable in ZM webconfig, but I still get the error

```
Can't open memory map file /tmp/shm/zm.mmap.1, probably not enough space free: Permission denied
```
---------------------------------------------
UPDATE: After doing a chown www:www /tmp/shm, I can see mmap files being created. 'Modect' works fine and alarm frames do get created.


----------



## lme@ (Apr 19, 2016)

abishai said:


> Why shar? Is github not enough? I thought it suits better when stuff is WIP.


Shar is a "Shell archive". You usually pack the a new port's directory into a shar file and then create a PR that a committer can review and commit.
See the Porter's Handbook at https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/book.html#porting-submitting for details.


----------



## kpa (Apr 19, 2016)

The advantage of the shar archive is that anyone can just download and save it and then unpack it immediately by executing it, no extra tools needed.


----------



## abishai (Apr 19, 2016)

Attached shar


----------



## lme@ (Apr 20, 2016)

Uuuh, this is *evil*:


```
post-extract:
        @${MV} ${WRKSRC_crud}/* ${WRKSRC}/web/api/app/Plugin/Crud
```
${WRKSRC_crud} is not set, so running this as root moved /* to ${WRKSRC}/web/api/app/Plugin/Crud 

Apart from that it's not so bad.   I checked with `portlint -ac` and fixed all errors and warnings. The result is attached.
Note before that I haven't tested the port yet. I just fixed the messages, created a new distinfo and ran `make patch` which worked.

Please try to build and use it.


----------



## abishai (Apr 20, 2016)

lme@ said:


> Uuuh, this is *evil*:
> 
> ```
> post-extract:
> ...


This is documented in porters handbook. And it works on my system. How can it be?
https://www.freebsd.org/doc/en_US.I...html#makefile-master_sites-github-description
"*5.4.3.1. Fetching Multiple Files from GitHub*"


----------



## lme@ (Apr 20, 2016)

Oh, sorry, I didn't realize you're fetching multiple distfiles. I changed the GH variables, so it didn't work for me. Then you should re-add this in my version of the port.


----------



## abishai (Apr 20, 2016)

lme@ said:


> Oh, sorry, I didn't realize you're fetching multiple distfiles. I changed the GH variables, so it didn't work for me. Then you should re-add this in my version of the port.


See? I'm not bad after all 

I have an error during stagify:

```
===>  zoneminder-1.29.1.a.20160420 depends on package: perl5>=5.20<5.21 - found
install  -m 0644 /usr/home/abishai/zoneminder/work/stage/usr/local/etc/zm.conf /usr/home/abishai/zoneminder/work/stage/usr/local/etc/zm.conf.sample
install: /usr/home/abishai/zoneminder/work/stage/usr/local/etc/zm.conf: No such file or directory
```
I copied zm.conf to zm.conf.sample there for reference. Looks like ${INSTALL_DATA} makes something else?


----------



## kpa (Apr 20, 2016)

Is the file zm.conf copied to the stage directory at an earlier step? It looks to me that you should instead copy the file from ${WRKSRC} but I haven't taken closer look at the port yet.


----------



## abishai (Apr 20, 2016)

kpa said:


> Is the file zm.conf copied to the stage directory at an earlier step? It looks to me that you should instead copy the file from ${WRKSRC} but I haven't taken closer look at the port yet.


ZM's cmake handles this. My original copy was in post-install. 
I moved it back and `make` succeeded.

```
post-install:
  ${INSTALL_DATA} ${STAGEDIR}${PREFIX}/etc/zm.conf ${STAGEDIR}${PREFIX}/etc/zm.conf.sample
```


----------



## trumee (Apr 21, 2016)

I have hooked  two cameras 352x288 and a 1920x1080 cameras both using mjpg_streamer. Both camera work independently if I try to view them. However, if i try to view both at the same time in Montage view, i get the following error

```
2016-04-20 18:40:03.327655 zms 101702 ERR Unable to send raw frame 15: Broken pipe zm_event.cpp 1339
2016-04-20 18:39:54.378644 zms 101703 ERR Terminating, last frame sent time 1461195594.260157 secs more than maximum of 10.000000 zm_monitor.cpp 4392
2016-04-20 18:39:54.272880 zms 101703 ERR Unable to send stream frame: Broken pipe zm_monitor.cpp 4127
2016-04-20 18:39:54.228304 zms 100729 ERR Unable to send stream frame: Broken pipe zm_monitor.cpp 4127
```


----------



## abishai (Apr 21, 2016)

You need to increase number of fcgiwrap workers. The default is 1, so, obviously, you can stream only 1 camera at the same time.
In rc.conf

```
fcgiwrap_flags="-c 4"
```
Set it to the sane limit, say, the number of monitors in your system, as every worker needs some memory.


----------



## kpa (Apr 21, 2016)

I ran your port (from your github repo, commit ba8cb35) trough `poudriere testport` and it passed all the tests fine. You should make a habit of doing that yourself unless you are doing that already of course


----------



## abishai (Apr 23, 2016)

lme@ said:


> Oh, sorry, I didn't realize you're fetching multiple distfiles. I changed the GH variables, so it didn't work for me. Then you should re-add this in my version of the port.


I have an issue when build depends are removed and replaced with run depends only.
The test
1. remove `pkg delete p5-Sys-Mmap-0.17_1`
2. try to build zoneminder

```
- Could NOT find PerlModules (missing:  PERLMODULES_SYS_MMAP_MODULE)
CMake Error at CMakeLists.txt:629 (message):
  Not all required perl modules were found on your system
```
Return BUILD_DEPENDS back ?


----------



## abishai (Apr 23, 2016)

Right, from https://www.freebsd.org/doc/en/books/porters-handbook/makefile-depend.html _The dependency is checked from within the install target._
cmake checks it on _build_ time.


----------



## lme@ (Apr 24, 2016)

Good.  But be sure to run `portlint -ac` from time to time. It wasn't happy that RUN_DEPENDS were set to BUILD_DEPENDS.


----------



## abishai (Apr 24, 2016)

kpa said:


> I ran your port (from your github repo, commit ba8cb35) trough `poudriere testport` and it passed all the tests fine. You should make a habit of doing that yourself unless you are doing that already of course


I tried poudriere some time ago and it was too complex for me, so I just use snapshot rollback under qemu-kvm.
Is poudriere can simulate i386 arch under x64 host? One of the issues I have is failure to build ZM under i386 and I don't have i386 guest yet. Maybe, I should give pourdrere a chance.



lme@ said:


> Good.  But be sure to run `portlint -ac` from time to time. It wasn't happy that RUN_DEPENDS were set to BUILD_DEPENDS.


And I found why... USES knob pollutes RUN_DEPENDS with additional BUILD_DEPENDS it adds in this scheme.


----------



## kpa (Apr 24, 2016)

abishai said:


> I tried poudriere some time ago and it was too complex for me, so I just use snapshot rollback under qemu-kvm.
> Is poudriere can simulate i386 arch under x64 host? One of the issues I have is failure to build ZM under i386 and I don't have i386 guest yet. Maybe, I should give pourdrere a chance.



Yes, you can create an i386 jail with poudriere and it will be as close as possible to compiling the port on a real i386 host. You can also use Synth for testing ( `synth testport` I think it is) purposes if that's easier for you to use, it should be equally rigorous in finding problems in ports.


----------



## trumee (Apr 27, 2016)

Any plans of committing this to ports?


----------



## dasti (Apr 27, 2016)

Oh my, I already see me testing zoneminder on my freeNAS box in a jail !!
Definitively need that


----------



## abishai (Apr 27, 2016)

trumee said:


> Any plans of committing this to ports?


https://github.com/abishai/Zoneminder-port/issues/4


dasti said:


> Oh my, I already see me testing zoneminder on my freeNAS box in a jail !!
> Definitively need that


But you can fetch port files from GitHub and test it without any problems.


----------



## trumee (May 2, 2016)

Unfortunately ZM stopped working for me and i am getting the following error

```
FAT [Can't execute log entry 'insert into Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) values ( ?, ?, ?, ?, ?, ?, ?, NULL )': ]
```


----------



## abishai (May 2, 2016)

Have you changed Pid column type to INT ? This is mandatory for ZM to operate under FreeBSD.
Generally, it's hard to determine the problem without actual values it tries to insert. Try to find them digging deeper on the logs or check mysql logs.


----------



## trumee (May 9, 2016)

Were you able to get the zoneminder api working? I wanted to use the api for the zmNinja program. I created a temp path for the api

```
mkdir /tmp/zmtmp;  ln -s /tmp/zmtmp /usr/local/www/zoneminder/api/app/tmp; chown www:www /tmp/zmtmp
```
but http://zmserver/api/ gave me an CakePHP error

```
URL rewriting is not properly configured on your server. 1) Help me configure it 2) I don't / can't use URL rewriting
```

-------UPDATE-------
The errors didn't go away but the api works with the following in nginx


```
location /api/ {
  alias  /usr/local/www/zoneminder/api;
  rewrite ^/api(.+)$ /api/index.php?p=$1 last;
  }
```

Also, the package php56-gd needs to installed to show up up images in events. The CPU usage taken by zmc is quite high (100%) though when scrubbing an event.


----------



## abishai (May 25, 2016)

zmNinja works witn www/nginx config from port documentation.


----------



## abishai (Jun 4, 2016)

I opened a PR https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=210007
Should I poke commiters in mailing lists ? Port doesn't have a maintaner (ports@FreeBSD.org)


----------



## abishai (Jun 6, 2016)

http://www.freshports.org/multimedia/zoneminder/


----------



## lme@ (Jun 6, 2016)

Thanks a lot for working on this!


----------



## M F (Jun 13, 2016)

Thankyouthankyouthankyou!! I was working on trying to get the (very) old 1.25 port running, and just saw the new port, this port commit is very timely..

I noticed that the PHP MySQL PDO extension was not built by default, but it is needed for the latest zoneminder to work properly. All I had to do was install lang/php56-extensions and enable the PDO_MYSQL option.

Really appreciate your work updating this port -- obviously several of us out here who wanted a working security cam solution for FreeBSD, and the initial experimentation with the FreeBSD zoneminder port is encouraging -- thank you!!

-M


----------



## trumee (Jul 4, 2016)

zmNinja tries to run /cgi-bin/zms while the help message in the package gives a tip for setting up /cgi-bin/nph-zms in nginx.conf. I had to the additional config before zmNinja Montage worked

```
location = /cgi-bin/zms {
  include fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass  unix:/var/run/fcgiwrap/fcgiwrap.sock;
  }
```


----------



## abishai (May 29, 2017)

I have a problem updating ZM to latest version. Additional dep is required, APCu. Ports have 2 versions of APCu, one for php56, another is for 7.0. So I need a conditional check.
However, when I add .include <bsd.port.pre.mk> to Makefile, I receive

```
=======================<phase: check-sanity   >============================
/!\ zoneminder-1.30.4: Makefile errors /!\

you cannot include bsd.port[.pre].mk twice

Defining both PORTVERSION and DISTVERSION is wrong, only set one, if necessary,
set DISTNAME

*** Error code 1
```

What's the problem?

```
# $FreeBSD: head/multimedia/zoneminder/Makefile 440139 2017-05-05 03:17:31Z miwi $

PORTNAME=       zoneminder
PORTVERSION=    1.30.4
CATEGORIES=     multimedia

MAINTAINER=     bsd@abinet.ru
COMMENT=        Complete security camera solution, fully web based with image analysis

LICENSE=        GPLv2

CONFLICTS_INSTALL=      zoneminder-h264

ZM_DEPENDS=     p5-DBI>=0:databases/p5-DBI \
                p5-DBD-mysql>=0:databases/p5-DBD-mysql \
                p5-Date-Manip>=0:devel/p5-Date-Manip \
                p5-Test-LWP-UserAgent>=0:www/p5-Test-LWP-UserAgent \
                p5-Sys-Mmap>=0:devel/p5-Sys-Mmap \
                p5-LWP-Protocol-https>=0:www/p5-LWP-Protocol-https \
                p5-Sys-CPU>=0:devel/p5-Sys-Cpu \
                p5-Sys-MemInfo>=0:devel/p5-Sys-MemInfo \
                p5-Data-Dump>=0:devel/p5-Data-Dump \
                p5-SOAP-WSDL>=0:devel/p5-SOAP-WSDL \
                p5-Data-UUID>=0:devel/p5-Data-UUID \
                p5-IO-Socket-Multicast>=0:net/p5-IO-Socket-Multicast \
                ffmpeg:multimedia/ffmpeg
BUILD_DEPENDS=  ${ZM_DEPENDS}
RUN_DEPENDS=    ${ZM_DEPENDS} \
                sudo:security/sudo \
                p5-Device-SerialPort>=0:comms/p5-Device-SerialPort \
                zip:archivers/zip

USE_GITHUB=     yes
GH_PROJECT=     ZoneMinder
GH_TUPLE=       FriendsOfCake:crud:c3976f1:crud

USES=           cmake jpeg mysql perl5 php shebangfix ssl
USE_RC_SUBR=    zoneminder
USE_PHP=        json pdo_mysql session gd sockets ctype opcache

ONLY_FOR_ARCHS= amd64 i386

OPTIONS_DEFINE= NLS V4L DOCS
OPTIONS_SUB=    yes
NLS_USES=       gettext
NLS_CONFIGURE_ENABLE=   nls
V4L_BUILD_DEPENDS=      ${LOCALBASE}/include/linux/videodev2.h:multimedia/v4l_compat
V4L_LIB_DEPENDS=        libv4l2.so:multimedia/libv4l
OPTIONS_DEFAULT=        NLS

PLIST_SUB=      WWWOWN="${WWWOWN}" WWWGRP="${WWWGRP}"

SHEBANG_FILES=  scripts/zmaudit.pl.in \
                scripts/zmcamtool.pl.in \
                scripts/zmcontrol.pl.in \
                scripts/zmdc.pl.in \
                scripts/zmfilter.pl.in \
                scripts/zmpkg.pl.in \
                scripts/zmtelemetry.pl.in \
                scripts/zmtrack.pl.in \
                scripts/zmtrigger.pl.in \
                scripts/zmupdate.pl.in \
                scripts/zmvideo.pl.in \
                scripts/zmwatch.pl.in \
                scripts/zmx10.pl.in \
                onvif/scripts/zmonvif-probe.pl

PORTDOCS=       AUTHORS BUGS ChangeLog INSTALL NEWS README.FreeBSD TODO

CMAKE_ARGS+=    -DZM_PERL_MM_PARMS=INSTALLDIRS=site \
                -DZM_CONFIG_DIR=${PREFIX}/etc \
                -DZM_WEBDIR=${WWWDIR} \
                -DZM_CGIDIR=${WWWDIR}/cgi-bin \
                -DZM_CONTENTDIR=${WWWDIR} \
                -DHAVE_SENDFILE=0 \
                -DZM_NO_CURL=ON \
                -DZM_NO_LIBVLC=ON \
                -DPCRE_LIBRARIES=0 \
                -DGNUTLS_LIBRARIES=0 \
                -DCMAKE_REQUIRED_INCLUDES:STRING="${LOCALBASE}/include"

.include <bsd.port.pre.mk>

PKGMESSAGE=     ${WRKDIR}/pkg-message

post-extract:
        ${CP} -R ${WRKSRC_crud}/* ${WRKSRC}/web/api/app/Plugin/Crud
        ${CP} ${FILESDIR}/README.FreeBSD ${WRKSRC}
        ${CP} ${FILESDIR}/README.FreeBSD ${PKGMESSAGE}
        ${REINPLACE_CMD} -e 's|/dev/shm|/tmp|g' ${WRKSRC}/scripts/ZoneMinder/lib/ZoneMinder/ConfigData.pm.in
        ${REINPLACE_CMD} -e 's|E_ALL|E_ALL^E_NOTICE|g' ${WRKSRC}/web/index.php

pre-install:
        ${MKDIR} ${STAGEDIR}${WWWDIR}/images
        ${MKDIR} ${STAGEDIR}${WWWDIR}/events
        ${MKDIR} ${STAGEDIR}${WWWDIR}/temp
        ${MKDIR} ${STAGEDIR}${WWWDIR}/api/app/tmp
        ${MKDIR} ${STAGEDIR}/var/run/zm
        ${MKDIR} ${STAGEDIR}/var/tmp/zm

post-install:
        ${INSTALL_DATA} ${STAGEDIR}${PREFIX}/etc/zm.conf ${STAGEDIR}${PREFIX}/etc/zm.conf.sample

post-install-DOCS-on:
        ${MKDIR} ${STAGEDIR}${DOCSDIR}
        cd ${WRKSRC} && ${INSTALL_MAN} ${PORTDOCS} ${STAGEDIR}${DOCSDIR}

.include <bsd.port.mk>
```


----------



## SirDice (May 29, 2017)

abishai said:


> Ports have 2 versions of APCu, one for php56, another is for 7.0. So I need a conditional check.


No, you simply depend on USE_PHP and let the ports system figure out which of the versions is required.


----------



## abishai (May 29, 2017)

SirDice said:


> No, you simply depend on USE_PHP and let the ports system figure out which of the versions is required.


I found conditional check here https://svnweb.freebsd.org/ports/head/devel/phabricator/Makefile?revision=437829&view=markup
I don't know what to write in USE_PHP for APCu, it's pecl module.
Also, even if check is not required, I don't understand why `.include <bsd.port.pre.mk>` upsets poudriere.


----------



## SirDice (May 29, 2017)

abishai said:


> Also, even if check is not required, I don't understand why  .include <bsd.port.pre.mk> upsets poudriere.


It's already included and can't be included twice.


----------



## abishai (May 29, 2017)

SirDice said:


> It's already included and can't be included twice.


But where ?! I quoted Makefile.


----------



## SirDice (May 29, 2017)

Just after CMAKE_ARGS and before PKGMESSAGE.


----------



## abishai (May 29, 2017)

SirDice said:


> Just after CMAKE_ARGS and before PKGMESSAGE.


That's the first. Where is the second ? I quoted Makefile that throws error.


----------



## SirDice (May 29, 2017)

Ah, I figured you added more and included another line. In that case you probably need to change the last line from bsd.port.mk to bsd.port.post.mk.


----------



## abishai (May 30, 2017)

Awww, I'm blind like a bat. Thanks! It builds now.


----------



## trumee (Aug 13, 2017)

I would like to switch a camera from modect mode to monitor mode and vice-versa via cli. I have setup tmpfs in /tmp

```
# ls -1 /tmp/shm/*
/tmp/shm/zm.mmap.1
/tmp/shm/zm.mmap.2
/tmp/shm/zm.mmap.3
```

If  try to put camera 2 into monitor mode

```
#curl -d "username=somebody&password=mypassword&action=login&view=console" -c /tmp/cookies.txt http://myzm/index.php
#curl -b /tmp/cookies.txt -XPOST http://myzm/api/monitors/2.json -d "Monitor[Function]=Monitor&Monitor[Enabled]=1"
```

Instead of just changing camera 2, it kills all the cameras with a message that mmap file is not available

```
#cat zmwatch.log
08/13/17 09:41:50.160670 zmwatch[81702].ERR [Memory map file '/tmp/shm/zm.mmap.2' does not exist.  zmc might not be running.]
08/13/17 09:41:50.161066 zmwatch[81702].INF [Restarting capture daemon for Camera-2, shared data not valid]
08/13/17 09:41:58.617506 zmwatch[81520].INF [Restarting capture daemon for Camera-2, shared data not valid]
08/13/17 09:41:58.621416 zmwatch[81521].INF [Restarting capture daemon for Camera-2, shared data not valid]
08/13/17 09:41:58.927755 zmwatch[81521].INF [Restarting capture daemon for Camera-1, shared data not valid]
08/13/17 09:41:58.929024 zmwatch[81520].INF [Restarting capture daemon for Camera-1, shared data not valid]
08/13/17 09:42:09.198986 zmwatch[81521].ERR [Memory map file '/tmp/shm/zm.mmap.1' does not exist.  zmc might not be running.]
08/13/17 09:42:09.199464 zmwatch[81521].INF [Restarting capture daemon for Camera-1, shared data not valid]
08/13/17 09:42:14.272303 zmwatch[81521].ERR [Memory map file '/tmp/shm/zm.mmap.2' should have been 896 but was instead 0]
08/13/17 09:42:14.272720 zmwatch[81521].INF [Restarting capture daemon for Camera-2, shared data not valid]
08/13/17 09:42:14.538649 zmwatch[81521].ERR [Memory map file '/tmp/shm/zm.mmap.3' should have been 896 but was instead 0]
08/13/17 09:42:14.539052 zmwatch[81521].INF [Restarting capture daemon for Camera-3, shared data not valid]
08/13/17 09:42:15.329443 zmwatch[81520].ERR [Memory map file '/tmp/shm/zm.mmap.1' does not exist.  zmc might not be running.]
08/13/17 09:42:15.329909 zmwatch[81520].INF [Restarting capture daemon for Camera-1, shared data not valid]
```

Any idea why ZM complains about the mmap file?


----------



## abishai (Aug 17, 2017)

Is it works from web page? I'm not observing the issue switching from it.


----------



## trumee (Aug 17, 2017)

No, the web page indicates that Zoneminder is stopped. I have to change the state to 'Start' to bring it up again.


----------



## trumee (Aug 19, 2017)

abishai, I set a Ubuntu VM with bhyve with the exact camera configuration as the FreeBSD jail. The Ubuntu packages were installed using this. I dont see this issue in the Ubuntu jail. The API call does not cause the ZM to go into stopped state. The only difference between Ubuntu and FreeBSD is that the former runs apache instead of nginx.

Btw, my ZM mysql db is on hdd rather than ssd. And i see the following in mysql log

```
/usr/local/libexec/mysqld, Version: 5.6.37-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument
```

I suspect this a race condition between zoneminder and mysql.
-----------------------------------------------------------------------
I moved my mysql database files to SSD. In addition i made the following changes to the database and zfs data set, and it has helped a lot. I am to give API commands without zoneminder ging into Stopped state.


```
#/usr/local/etc/mysql/my.cnf
innodb_buffer_pool_size         = 20G
innodb_data_home_dir            = /var/db/mysql/innodb
innodb_log_group_home_dir       = /var/db/mysql/logs
innodb_data_file_path           = ibdata1:128M:autoextend
innodb_flush_method             = O_DIRECT
innodb_log_file_size            = 256M
innodb_log_buffer_size          = 16M
innodb_write_io_threads         = 8
innodb_read_io_threads          = 8
innodb_autoinc_lock_mode        = 2
innodb_flush_log_at_trx_commit=2
innodb_file_per_table = off
skip-innodb_doublewrite
```

For the mysql zfs dataset

```
zfs set primarycache=metadata tank/db
zfs set atime=off tank/db
zfs set recordsize=16k tank/db/innodb
zfs set recordsize=128k tank/db/logs
zfs set sync=disabled tank/db
```


----------



## abishai (Aug 20, 2017)

trumee said:


> the web page indicates that Zoneminder is stopped


Is zms and zmc daemons continue to run in this state? I observing this issue from time to time, but found no clue yet. ZM web page shows stopped state, but other stuff continue to operate.


----------



## Phishfry (Aug 21, 2017)

I am seeing lots of error logging just like above nmap for each camera. I do have events setup and they work good and can export OK.
Since i was worried I was missing something as I am new to Zoneminder so I fired up Debian9.2 and it works good. I still see some errors in the logs though much less red.
I am going to rebuild my FreeBSD zoneminder box fresh again. Seems like it is so close to working good.
My zoneminder service does not stop either. Just a very noisy log. Maybe felt sluggish.
2 IQeye 2MP cams, 2 Arecont 1.3MP,5MP, and an older Samsung B2335
Using different cameras has been challenging because they all offer different image sizes. Montage looks ok after some work.


----------



## Phishfry (Aug 21, 2017)

I pulled my FreeBSD disk back up. Here are the errors. I seem to see that nmap error for every monitor # even though the file exists.

```
root@NVR:~ # ls -1 /tmp/*
/tmp/feed1.ffm
/tmp/mysql.sock
/tmp/sess_4nt0ik2uo9t1egmhm5t8b0gh43
/tmp/sess_t8rqiqklk8phot422bk2795mf7
/tmp/zm.mmap.1
/tmp/zm.mmap.2
/tmp/zm.mmap.3
/tmp/zm.mmap.4
/tmp/zm.mmap.5
```


----------



## abishai (Aug 21, 2017)

I disabled database logging a long time ago. Re-enabled it with INFO level. When you get these errors ? Nothing suspicious upon restart http://i.imgur.com/ZnXf59P.png
Also, I don't recommend to restart ZM daemon at all (restart jail instead)
`zmdc.pl` code is unreliable. You can't `killall` perl scripts by name, their names are perl script_name.pl At least under FreeBSD, not sure about Linux, maybe systemd cleans them.

```
sub killAll
{
    my $delay = shift;
    sleep( $delay );
    my $killall;
    if ( '@HOST_OS@' eq 'BSD' )
    {
        $killall = 'killall -q -';
    } elsif ( '@HOST_OS@' eq 'solaris' ) {
        $killall = 'pkill -';
    } else {
        $killall = 'killall -q -s ';
    }
    foreach my $daemon ( @daemons )
    {
        my $cmd = $killall ."TERM $daemon";
        Debug( $cmd );
        qx( $cmd );
    }
    sleep( $delay );
    foreach my $daemon ( @daemons )
    {
        my $cmd = $killall."KILL $daemon";
        Debug( $cmd );
        qx( $cmd );
    }
}
```


----------



## trumee (Aug 29, 2017)

Unfortunately, I had to give up on running zoneminder in the FreeBSD jail for now. The API calls to the ZM server are not reliable and the server goes into a stopped state. I moved my config into a bhye virtual machine running debian and it is quite stable.


----------



## abishai (Nov 17, 2018)

Anyone wishes to test zoneminder 1.32.3 port ?


----------

