28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley/*
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * CDDL HEADER START
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley *
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * The contents of this file are subject to the terms
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * of the Common Development and Distribution License
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * (the "License"). You may not use this file except
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * in compliance with the License.
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley *
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * You can obtain a copy of the license at
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * src/OPENSOLARIS.LICENSE
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * or http://www.opensolaris.org/os/licensing.
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * See the License for the specific language governing
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * permissions and limitations under the License.
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley *
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * When distributing Covered Code, include this CDDL
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * HEADER in each file and include the License file at
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * usr/src/OPENSOLARIS.LICENSE. If applicable,
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * add the following below this CDDL HEADER, with the
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * fields enclosed by brackets "[]" replaced with your
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * own identifying information: Portions Copyright [yyyy]
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * [name of copyright owner]
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley *
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * CDDL HEADER END
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley */
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley/*
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * Use is subject to license terms.
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley */
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley/*
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley * test file locking
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley */
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley#include <unistd.h>
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley#include <stdlib.h>
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley#include <stdio.h>
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley#include <string.h>
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley#include <errno.h>
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley#include <fcntl.h>
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley#include "libmicro.h"
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleystatic int file;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleyint
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleyblock(int index)
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley{
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley struct flock fl;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_type = F_WRLCK;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_whence = SEEK_SET;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_start = index;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_len = 1;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley return (fcntl(file, F_SETLKW, &fl) == -1);
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley}
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleyint
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleyunblock(int index)
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley{
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley struct flock fl;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_type = F_UNLCK;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_whence = SEEK_SET;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_start = index;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley fl.l_len = 1;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley return (fcntl(file, F_SETLK, &fl) == -1);
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley}
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleyint
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleybenchmark_init()
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley{
1f2756f065e789ec9e9c9f6f11f3bfc8208ae7bfLukas Slebodnik char fname[80];
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley int errors = 0;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley (void) sprintf(fname, "/tmp/oneflock.%ld", getpid());
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley file = open(fname, O_CREAT | O_TRUNC | O_RDWR, 0600);
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley if (file == -1) {
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley errors++;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley }
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley if (unlink(fname)) {
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley errors++;
1f2756f065e789ec9e9c9f6f11f3bfc8208ae7bfLukas Slebodnik }
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley lm_tsdsize = 0;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley return (errors);
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley}
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley/*ARGSUSED*/
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleyint
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elleybenchmark(void *tsd, result_t *res)
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley{
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley int i;
1f2756f065e789ec9e9c9f6f11f3bfc8208ae7bfLukas Slebodnik int e = 0;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley for (i = 0; i < lm_optB; i ++) {
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley e += block(0);
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley e += unblock(0);
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley }
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley res->re_count = i;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley res->re_errors = e;
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley return (0);
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley}
28c155e20d3ebf53581821572c6c3fe1724582c9Yassir Elley